1/*
2 *
3 * Copyright 2014 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
22	"errors"
23	"io"
24	"math"
25	"strconv"
26	"sync"
27	"time"
28
29	"golang.org/x/net/context"
30	"golang.org/x/net/trace"
31	"google.golang.org/grpc/balancer"
32	"google.golang.org/grpc/codes"
33	"google.golang.org/grpc/encoding"
34	"google.golang.org/grpc/grpclog"
35	"google.golang.org/grpc/internal/channelz"
36	"google.golang.org/grpc/internal/grpcrand"
37	"google.golang.org/grpc/internal/transport"
38	"google.golang.org/grpc/metadata"
39	"google.golang.org/grpc/stats"
40	"google.golang.org/grpc/status"
41)
42
43// StreamHandler defines the handler called by gRPC server to complete the
44// execution of a streaming RPC. If a StreamHandler returns an error, it
45// should be produced by the status package, or else gRPC will use
46// codes.Unknown as the status code and err.Error() as the status message
47// of the RPC.
48type StreamHandler func(srv interface{}, stream ServerStream) error
49
50// StreamDesc represents a streaming RPC service's method specification.
51type StreamDesc struct {
52	StreamName string
53	Handler    StreamHandler
54
55	// At least one of these is true.
56	ServerStreams bool
57	ClientStreams bool
58}
59
60// Stream defines the common interface a client or server stream has to satisfy.
61//
62// Deprecated: See ClientStream and ServerStream documentation instead.
63type Stream interface {
64	// Deprecated: See ClientStream and ServerStream documentation instead.
65	Context() context.Context
66	// Deprecated: See ClientStream and ServerStream documentation instead.
67	SendMsg(m interface{}) error
68	// Deprecated: See ClientStream and ServerStream documentation instead.
69	RecvMsg(m interface{}) error
70}
71
72// ClientStream defines the client-side behavior of a streaming RPC.
73//
74// All errors returned from ClientStream methods are compatible with the
75// status package.
76type ClientStream interface {
77	// Header returns the header metadata received from the server if there
78	// is any. It blocks if the metadata is not ready to read.
79	Header() (metadata.MD, error)
80	// Trailer returns the trailer metadata from the server, if there is any.
81	// It must only be called after stream.CloseAndRecv has returned, or
82	// stream.Recv has returned a non-nil error (including io.EOF).
83	Trailer() metadata.MD
84	// CloseSend closes the send direction of the stream. It closes the stream
85	// when non-nil error is met.
86	CloseSend() error
87	// Context returns the context for this stream.
88	//
89	// It should not be called until after Header or RecvMsg has returned. Once
90	// called, subsequent client-side retries are disabled.
91	Context() context.Context
92	// SendMsg is generally called by generated code. On error, SendMsg aborts
93	// the stream. If the error was generated by the client, the status is
94	// returned directly; otherwise, io.EOF is returned and the status of
95	// the stream may be discovered using RecvMsg.
96	//
97	// SendMsg blocks until:
98	//   - There is sufficient flow control to schedule m with the transport, or
99	//   - The stream is done, or
100	//   - The stream breaks.
101	//
102	// SendMsg does not wait until the message is received by the server. An
103	// untimely stream closure may result in lost messages. To ensure delivery,
104	// users should ensure the RPC completed successfully using RecvMsg.
105	//
106	// It is safe to have a goroutine calling SendMsg and another goroutine
107	// calling RecvMsg on the same stream at the same time, but it is not safe
108	// to call SendMsg on the same stream in different goroutines.
109	SendMsg(m interface{}) error
110	// RecvMsg blocks until it receives a message into m or the stream is
111	// done. It returns io.EOF when the stream completes successfully. On
112	// any other error, the stream is aborted and the error contains the RPC
113	// status.
114	//
115	// It is safe to have a goroutine calling SendMsg and another goroutine
116	// calling RecvMsg on the same stream at the same time, but it is not
117	// safe to call RecvMsg on the same stream in different goroutines.
118	RecvMsg(m interface{}) error
119}
120
121// NewStream creates a new Stream for the client side. This is typically
122// called by generated code. ctx is used for the lifetime of the stream.
123//
124// To ensure resources are not leaked due to the stream returned, one of the following
125// actions must be performed:
126//
127//      1. Call Close on the ClientConn.
128//      2. Cancel the context provided.
129//      3. Call RecvMsg until a non-nil error is returned. A protobuf-generated
130//         client-streaming RPC, for instance, might use the helper function
131//         CloseAndRecv (note that CloseSend does not Recv, therefore is not
132//         guaranteed to release all resources).
133//      4. Receive a non-nil, non-io.EOF error from Header or SendMsg.
134//
135// If none of the above happen, a goroutine and a context will be leaked, and grpc
136// will not call the optionally-configured stats handler with a stats.End message.
137func (cc *ClientConn) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
138	// allow interceptor to see all applicable call options, which means those
139	// configured as defaults from dial option as well as per-call options
140	opts = combine(cc.dopts.callOptions, opts)
141
142	if cc.dopts.streamInt != nil {
143		return cc.dopts.streamInt(ctx, desc, cc, method, newClientStream, opts...)
144	}
145	return newClientStream(ctx, desc, cc, method, opts...)
146}
147
148// NewClientStream is a wrapper for ClientConn.NewStream.
149func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
150	return cc.NewStream(ctx, desc, method, opts...)
151}
152
153func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
154	if channelz.IsOn() {
155		cc.incrCallsStarted()
156		defer func() {
157			if err != nil {
158				cc.incrCallsFailed()
159			}
160		}()
161	}
162	c := defaultCallInfo()
163	mc := cc.GetMethodConfig(method)
164	if mc.WaitForReady != nil {
165		c.failFast = !*mc.WaitForReady
166	}
167
168	// Possible context leak:
169	// The cancel function for the child context we create will only be called
170	// when RecvMsg returns a non-nil error, if the ClientConn is closed, or if
171	// an error is generated by SendMsg.
172	// https://github.com/grpc/grpc-go/issues/1818.
173	var cancel context.CancelFunc
174	if mc.Timeout != nil && *mc.Timeout >= 0 {
175		ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
176	} else {
177		ctx, cancel = context.WithCancel(ctx)
178	}
179	defer func() {
180		if err != nil {
181			cancel()
182		}
183	}()
184
185	for _, o := range opts {
186		if err := o.before(c); err != nil {
187			return nil, toRPCErr(err)
188		}
189	}
190	c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
191	c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
192	if err := setCallInfoCodec(c); err != nil {
193		return nil, err
194	}
195
196	callHdr := &transport.CallHdr{
197		Host:           cc.authority,
198		Method:         method,
199		ContentSubtype: c.contentSubtype,
200	}
201
202	// Set our outgoing compression according to the UseCompressor CallOption, if
203	// set.  In that case, also find the compressor from the encoding package.
204	// Otherwise, use the compressor configured by the WithCompressor DialOption,
205	// if set.
206	var cp Compressor
207	var comp encoding.Compressor
208	if ct := c.compressorType; ct != "" {
209		callHdr.SendCompress = ct
210		if ct != encoding.Identity {
211			comp = encoding.GetCompressor(ct)
212			if comp == nil {
213				return nil, status.Errorf(codes.Internal, "grpc: Compressor is not installed for requested grpc-encoding %q", ct)
214			}
215		}
216	} else if cc.dopts.cp != nil {
217		callHdr.SendCompress = cc.dopts.cp.Type()
218		cp = cc.dopts.cp
219	}
220	if c.creds != nil {
221		callHdr.Creds = c.creds
222	}
223	var trInfo traceInfo
224	if EnableTracing {
225		trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
226		trInfo.firstLine.client = true
227		if deadline, ok := ctx.Deadline(); ok {
228			trInfo.firstLine.deadline = deadline.Sub(time.Now())
229		}
230		trInfo.tr.LazyLog(&trInfo.firstLine, false)
231		ctx = trace.NewContext(ctx, trInfo.tr)
232	}
233	ctx = newContextWithRPCInfo(ctx, c.failFast)
234	sh := cc.dopts.copts.StatsHandler
235	var beginTime time.Time
236	if sh != nil {
237		ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
238		beginTime = time.Now()
239		begin := &stats.Begin{
240			Client:    true,
241			BeginTime: beginTime,
242			FailFast:  c.failFast,
243		}
244		sh.HandleRPC(ctx, begin)
245	}
246
247	cs := &clientStream{
248		callHdr:      callHdr,
249		ctx:          ctx,
250		methodConfig: &mc,
251		opts:         opts,
252		callInfo:     c,
253		cc:           cc,
254		desc:         desc,
255		codec:        c.codec,
256		cp:           cp,
257		comp:         comp,
258		cancel:       cancel,
259		beginTime:    beginTime,
260		firstAttempt: true,
261	}
262	if !cc.dopts.disableRetry {
263		cs.retryThrottler = cc.retryThrottler.Load().(*retryThrottler)
264	}
265
266	cs.callInfo.stream = cs
267	// Only this initial attempt has stats/tracing.
268	// TODO(dfawley): move to newAttempt when per-attempt stats are implemented.
269	if err := cs.newAttemptLocked(sh, trInfo); err != nil {
270		cs.finish(err)
271		return nil, err
272	}
273
274	op := func(a *csAttempt) error { return a.newStream() }
275	if err := cs.withRetry(op, func() { cs.bufferForRetryLocked(0, op) }); err != nil {
276		cs.finish(err)
277		return nil, err
278	}
279
280	if desc != unaryStreamDesc {
281		// Listen on cc and stream contexts to cleanup when the user closes the
282		// ClientConn or cancels the stream context.  In all other cases, an error
283		// should already be injected into the recv buffer by the transport, which
284		// the client will eventually receive, and then we will cancel the stream's
285		// context in clientStream.finish.
286		go func() {
287			select {
288			case <-cc.ctx.Done():
289				cs.finish(ErrClientConnClosing)
290			case <-ctx.Done():
291				cs.finish(toRPCErr(ctx.Err()))
292			}
293		}()
294	}
295	return cs, nil
296}
297
298func (cs *clientStream) newAttemptLocked(sh stats.Handler, trInfo traceInfo) error {
299	cs.attempt = &csAttempt{
300		cs:           cs,
301		dc:           cs.cc.dopts.dc,
302		statsHandler: sh,
303		trInfo:       trInfo,
304	}
305
306	if err := cs.ctx.Err(); err != nil {
307		return toRPCErr(err)
308	}
309	t, done, err := cs.cc.getTransport(cs.ctx, cs.callInfo.failFast, cs.callHdr.Method)
310	if err != nil {
311		return err
312	}
313	cs.attempt.t = t
314	cs.attempt.done = done
315	return nil
316}
317
318func (a *csAttempt) newStream() error {
319	cs := a.cs
320	cs.callHdr.PreviousAttempts = cs.numRetries
321	s, err := a.t.NewStream(cs.ctx, cs.callHdr)
322	if err != nil {
323		return toRPCErr(err)
324	}
325	cs.attempt.s = s
326	cs.attempt.p = &parser{r: s}
327	return nil
328}
329
330// clientStream implements a client side Stream.
331type clientStream struct {
332	callHdr  *transport.CallHdr
333	opts     []CallOption
334	callInfo *callInfo
335	cc       *ClientConn
336	desc     *StreamDesc
337
338	codec baseCodec
339	cp    Compressor
340	comp  encoding.Compressor
341
342	cancel context.CancelFunc // cancels all attempts
343
344	sentLast  bool // sent an end stream
345	beginTime time.Time
346
347	methodConfig *MethodConfig
348
349	ctx context.Context // the application's context, wrapped by stats/tracing
350
351	retryThrottler *retryThrottler // The throttler active when the RPC began.
352
353	mu                      sync.Mutex
354	firstAttempt            bool       // if true, transparent retry is valid
355	numRetries              int        // exclusive of transparent retry attempt(s)
356	numRetriesSincePushback int        // retries since pushback; to reset backoff
357	finished                bool       // TODO: replace with atomic cmpxchg or sync.Once?
358	attempt                 *csAttempt // the active client stream attempt
359	// TODO(hedging): hedging will have multiple attempts simultaneously.
360	committed  bool                       // active attempt committed for retry?
361	buffer     []func(a *csAttempt) error // operations to replay on retry
362	bufferSize int                        // current size of buffer
363}
364
365// csAttempt implements a single transport stream attempt within a
366// clientStream.
367type csAttempt struct {
368	cs   *clientStream
369	t    transport.ClientTransport
370	s    *transport.Stream
371	p    *parser
372	done func(balancer.DoneInfo)
373
374	finished  bool
375	dc        Decompressor
376	decomp    encoding.Compressor
377	decompSet bool
378
379	mu sync.Mutex // guards trInfo.tr
380	// trInfo.tr is set when created (if EnableTracing is true),
381	// and cleared when the finish method is called.
382	trInfo traceInfo
383
384	statsHandler stats.Handler
385}
386
387func (cs *clientStream) commitAttemptLocked() {
388	cs.committed = true
389	cs.buffer = nil
390}
391
392func (cs *clientStream) commitAttempt() {
393	cs.mu.Lock()
394	cs.commitAttemptLocked()
395	cs.mu.Unlock()
396}
397
398// shouldRetry returns nil if the RPC should be retried; otherwise it returns
399// the error that should be returned by the operation.
400func (cs *clientStream) shouldRetry(err error) error {
401	if cs.attempt.s == nil && !cs.callInfo.failFast {
402		// In the event of any error from NewStream (attempt.s == nil), we
403		// never attempted to write anything to the wire, so we can retry
404		// indefinitely for non-fail-fast RPCs.
405		return nil
406	}
407	if cs.finished || cs.committed {
408		// RPC is finished or committed; cannot retry.
409		return err
410	}
411	// Wait for the trailers.
412	if cs.attempt.s != nil {
413		<-cs.attempt.s.Done()
414	}
415	if cs.firstAttempt && !cs.callInfo.failFast && (cs.attempt.s == nil || cs.attempt.s.Unprocessed()) {
416		// First attempt, wait-for-ready, stream unprocessed: transparently retry.
417		cs.firstAttempt = false
418		return nil
419	}
420	cs.firstAttempt = false
421	if cs.cc.dopts.disableRetry {
422		return err
423	}
424
425	pushback := 0
426	hasPushback := false
427	if cs.attempt.s != nil {
428		if to, toErr := cs.attempt.s.TrailersOnly(); toErr != nil {
429			// Context error; stop now.
430			return toErr
431		} else if !to {
432			return err
433		}
434
435		// TODO(retry): Move down if the spec changes to not check server pushback
436		// before considering this a failure for throttling.
437		sps := cs.attempt.s.Trailer()["grpc-retry-pushback-ms"]
438		if len(sps) == 1 {
439			var e error
440			if pushback, e = strconv.Atoi(sps[0]); e != nil || pushback < 0 {
441				grpclog.Infof("Server retry pushback specified to abort (%q).", sps[0])
442				cs.retryThrottler.throttle() // This counts as a failure for throttling.
443				return err
444			}
445			hasPushback = true
446		} else if len(sps) > 1 {
447			grpclog.Warningf("Server retry pushback specified multiple values (%q); not retrying.", sps)
448			cs.retryThrottler.throttle() // This counts as a failure for throttling.
449			return err
450		}
451	}
452
453	var code codes.Code
454	if cs.attempt.s != nil {
455		code = cs.attempt.s.Status().Code()
456	} else {
457		code = status.Convert(err).Code()
458	}
459
460	rp := cs.methodConfig.retryPolicy
461	if rp == nil || !rp.retryableStatusCodes[code] {
462		return err
463	}
464
465	// Note: the ordering here is important; we count this as a failure
466	// only if the code matched a retryable code.
467	if cs.retryThrottler.throttle() {
468		return err
469	}
470	if cs.numRetries+1 >= rp.maxAttempts {
471		return err
472	}
473
474	var dur time.Duration
475	if hasPushback {
476		dur = time.Millisecond * time.Duration(pushback)
477		cs.numRetriesSincePushback = 0
478	} else {
479		fact := math.Pow(rp.backoffMultiplier, float64(cs.numRetriesSincePushback))
480		cur := float64(rp.initialBackoff) * fact
481		if max := float64(rp.maxBackoff); cur > max {
482			cur = max
483		}
484		dur = time.Duration(grpcrand.Int63n(int64(cur)))
485		cs.numRetriesSincePushback++
486	}
487
488	// TODO(dfawley): we could eagerly fail here if dur puts us past the
489	// deadline, but unsure if it is worth doing.
490	t := time.NewTimer(dur)
491	select {
492	case <-t.C:
493		cs.numRetries++
494		return nil
495	case <-cs.ctx.Done():
496		t.Stop()
497		return status.FromContextError(cs.ctx.Err()).Err()
498	}
499}
500
501// Returns nil if a retry was performed and succeeded; error otherwise.
502func (cs *clientStream) retryLocked(lastErr error) error {
503	for {
504		cs.attempt.finish(lastErr)
505		if err := cs.shouldRetry(lastErr); err != nil {
506			cs.commitAttemptLocked()
507			return err
508		}
509		if err := cs.newAttemptLocked(nil, traceInfo{}); err != nil {
510			return err
511		}
512		if lastErr = cs.replayBufferLocked(); lastErr == nil {
513			return nil
514		}
515	}
516}
517
518func (cs *clientStream) Context() context.Context {
519	cs.commitAttempt()
520	// No need to lock before using attempt, since we know it is committed and
521	// cannot change.
522	return cs.attempt.s.Context()
523}
524
525func (cs *clientStream) withRetry(op func(a *csAttempt) error, onSuccess func()) error {
526	cs.mu.Lock()
527	for {
528		if cs.committed {
529			cs.mu.Unlock()
530			return op(cs.attempt)
531		}
532		a := cs.attempt
533		cs.mu.Unlock()
534		err := op(a)
535		cs.mu.Lock()
536		if a != cs.attempt {
537			// We started another attempt already.
538			continue
539		}
540		if err == io.EOF {
541			<-a.s.Done()
542		}
543		if err == nil || (err == io.EOF && a.s.Status().Code() == codes.OK) {
544			onSuccess()
545			cs.mu.Unlock()
546			return err
547		}
548		if err := cs.retryLocked(err); err != nil {
549			cs.mu.Unlock()
550			return err
551		}
552	}
553}
554
555func (cs *clientStream) Header() (metadata.MD, error) {
556	var m metadata.MD
557	err := cs.withRetry(func(a *csAttempt) error {
558		var err error
559		m, err = a.s.Header()
560		return toRPCErr(err)
561	}, cs.commitAttemptLocked)
562	if err != nil {
563		cs.finish(err)
564	}
565	return m, err
566}
567
568func (cs *clientStream) Trailer() metadata.MD {
569	// On RPC failure, we never need to retry, because usage requires that
570	// RecvMsg() returned a non-nil error before calling this function is valid.
571	// We would have retried earlier if necessary.
572	//
573	// Commit the attempt anyway, just in case users are not following those
574	// directions -- it will prevent races and should not meaningfully impact
575	// performance.
576	cs.commitAttempt()
577	if cs.attempt.s == nil {
578		return nil
579	}
580	return cs.attempt.s.Trailer()
581}
582
583func (cs *clientStream) replayBufferLocked() error {
584	a := cs.attempt
585	for _, f := range cs.buffer {
586		if err := f(a); err != nil {
587			return err
588		}
589	}
590	return nil
591}
592
593func (cs *clientStream) bufferForRetryLocked(sz int, op func(a *csAttempt) error) {
594	// Note: we still will buffer if retry is disabled (for transparent retries).
595	if cs.committed {
596		return
597	}
598	cs.bufferSize += sz
599	if cs.bufferSize > cs.callInfo.maxRetryRPCBufferSize {
600		cs.commitAttemptLocked()
601		return
602	}
603	cs.buffer = append(cs.buffer, op)
604}
605
606func (cs *clientStream) SendMsg(m interface{}) (err error) {
607	defer func() {
608		if err != nil && err != io.EOF {
609			// Call finish on the client stream for errors generated by this SendMsg
610			// call, as these indicate problems created by this client.  (Transport
611			// errors are converted to an io.EOF error in csAttempt.sendMsg; the real
612			// error will be returned from RecvMsg eventually in that case, or be
613			// retried.)
614			cs.finish(err)
615		}
616	}()
617	if cs.sentLast {
618		return status.Errorf(codes.Internal, "SendMsg called after CloseSend")
619	}
620	if !cs.desc.ClientStreams {
621		cs.sentLast = true
622	}
623	data, err := encode(cs.codec, m)
624	if err != nil {
625		return err
626	}
627	compData, err := compress(data, cs.cp, cs.comp)
628	if err != nil {
629		return err
630	}
631	hdr, payload := msgHeader(data, compData)
632	// TODO(dfawley): should we be checking len(data) instead?
633	if len(payload) > *cs.callInfo.maxSendMessageSize {
634		return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(payload), *cs.callInfo.maxSendMessageSize)
635	}
636	op := func(a *csAttempt) error {
637		err := a.sendMsg(m, hdr, payload, data)
638		// nil out the message and uncomp when replaying; they are only needed for
639		// stats which is disabled for subsequent attempts.
640		m, data = nil, nil
641		return err
642	}
643	return cs.withRetry(op, func() { cs.bufferForRetryLocked(len(hdr)+len(payload), op) })
644}
645
646func (cs *clientStream) RecvMsg(m interface{}) error {
647	err := cs.withRetry(func(a *csAttempt) error {
648		return a.recvMsg(m)
649	}, cs.commitAttemptLocked)
650	if err != nil || !cs.desc.ServerStreams {
651		// err != nil or non-server-streaming indicates end of stream.
652		cs.finish(err)
653	}
654	return err
655}
656
657func (cs *clientStream) CloseSend() error {
658	if cs.sentLast {
659		// TODO: return an error and finish the stream instead, due to API misuse?
660		return nil
661	}
662	cs.sentLast = true
663	op := func(a *csAttempt) error { return a.t.Write(a.s, nil, nil, &transport.Options{Last: true}) }
664	cs.withRetry(op, func() { cs.bufferForRetryLocked(0, op) })
665	// We never returned an error here for reasons.
666	return nil
667}
668
669func (cs *clientStream) finish(err error) {
670	if err == io.EOF {
671		// Ending a stream with EOF indicates a success.
672		err = nil
673	}
674	cs.mu.Lock()
675	if cs.finished {
676		cs.mu.Unlock()
677		return
678	}
679	cs.finished = true
680	cs.commitAttemptLocked()
681	cs.mu.Unlock()
682	if err == nil {
683		cs.retryThrottler.successfulRPC()
684	}
685	if channelz.IsOn() {
686		if err != nil {
687			cs.cc.incrCallsFailed()
688		} else {
689			cs.cc.incrCallsSucceeded()
690		}
691	}
692	if cs.attempt != nil {
693		cs.attempt.finish(err)
694	}
695	// after functions all rely upon having a stream.
696	if cs.attempt.s != nil {
697		for _, o := range cs.opts {
698			o.after(cs.callInfo)
699		}
700	}
701	cs.cancel()
702}
703
704func (a *csAttempt) sendMsg(m interface{}, hdr, payld, data []byte) error {
705	cs := a.cs
706	if EnableTracing {
707		a.mu.Lock()
708		if a.trInfo.tr != nil {
709			a.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
710		}
711		a.mu.Unlock()
712	}
713	if err := a.t.Write(a.s, hdr, payld, &transport.Options{Last: !cs.desc.ClientStreams}); err != nil {
714		if !cs.desc.ClientStreams {
715			// For non-client-streaming RPCs, we return nil instead of EOF on error
716			// because the generated code requires it.  finish is not called; RecvMsg()
717			// will call it with the stream's status independently.
718			return nil
719		}
720		return io.EOF
721	}
722	if a.statsHandler != nil {
723		a.statsHandler.HandleRPC(cs.ctx, outPayload(true, m, data, payld, time.Now()))
724	}
725	if channelz.IsOn() {
726		a.t.IncrMsgSent()
727	}
728	return nil
729}
730
731func (a *csAttempt) recvMsg(m interface{}) (err error) {
732	cs := a.cs
733	var inPayload *stats.InPayload
734	if a.statsHandler != nil {
735		inPayload = &stats.InPayload{
736			Client: true,
737		}
738	}
739	if !a.decompSet {
740		// Block until we receive headers containing received message encoding.
741		if ct := a.s.RecvCompress(); ct != "" && ct != encoding.Identity {
742			if a.dc == nil || a.dc.Type() != ct {
743				// No configured decompressor, or it does not match the incoming
744				// message encoding; attempt to find a registered compressor that does.
745				a.dc = nil
746				a.decomp = encoding.GetCompressor(ct)
747			}
748		} else {
749			// No compression is used; disable our decompressor.
750			a.dc = nil
751		}
752		// Only initialize this state once per stream.
753		a.decompSet = true
754	}
755	err = recv(a.p, cs.codec, a.s, a.dc, m, *cs.callInfo.maxReceiveMessageSize, inPayload, a.decomp)
756	if err != nil {
757		if err == io.EOF {
758			if statusErr := a.s.Status().Err(); statusErr != nil {
759				return statusErr
760			}
761			return io.EOF // indicates successful end of stream.
762		}
763		return toRPCErr(err)
764	}
765	if EnableTracing {
766		a.mu.Lock()
767		if a.trInfo.tr != nil {
768			a.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
769		}
770		a.mu.Unlock()
771	}
772	if inPayload != nil {
773		a.statsHandler.HandleRPC(cs.ctx, inPayload)
774	}
775	if channelz.IsOn() {
776		a.t.IncrMsgRecv()
777	}
778	if cs.desc.ServerStreams {
779		// Subsequent messages should be received by subsequent RecvMsg calls.
780		return nil
781	}
782
783	// Special handling for non-server-stream rpcs.
784	// This recv expects EOF or errors, so we don't collect inPayload.
785	err = recv(a.p, cs.codec, a.s, a.dc, m, *cs.callInfo.maxReceiveMessageSize, nil, a.decomp)
786	if err == nil {
787		return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
788	}
789	if err == io.EOF {
790		return a.s.Status().Err() // non-server streaming Recv returns nil on success
791	}
792	return toRPCErr(err)
793}
794
795func (a *csAttempt) finish(err error) {
796	a.mu.Lock()
797	if a.finished {
798		a.mu.Unlock()
799		return
800	}
801	a.finished = true
802	if err == io.EOF {
803		// Ending a stream with EOF indicates a success.
804		err = nil
805	}
806	if a.s != nil {
807		a.t.CloseStream(a.s, err)
808	}
809
810	if a.done != nil {
811		br := false
812		if a.s != nil {
813			br = a.s.BytesReceived()
814		}
815		a.done(balancer.DoneInfo{
816			Err:           err,
817			BytesSent:     a.s != nil,
818			BytesReceived: br,
819		})
820	}
821	if a.statsHandler != nil {
822		end := &stats.End{
823			Client:    true,
824			BeginTime: a.cs.beginTime,
825			EndTime:   time.Now(),
826			Error:     err,
827		}
828		a.statsHandler.HandleRPC(a.cs.ctx, end)
829	}
830	if a.trInfo.tr != nil {
831		if err == nil {
832			a.trInfo.tr.LazyPrintf("RPC: [OK]")
833		} else {
834			a.trInfo.tr.LazyPrintf("RPC: [%v]", err)
835			a.trInfo.tr.SetError()
836		}
837		a.trInfo.tr.Finish()
838		a.trInfo.tr = nil
839	}
840	a.mu.Unlock()
841}
842
843// ServerStream defines the server-side behavior of a streaming RPC.
844//
845// All errors returned from ServerStream methods are compatible with the
846// status package.
847type ServerStream interface {
848	// SetHeader sets the header metadata. It may be called multiple times.
849	// When call multiple times, all the provided metadata will be merged.
850	// All the metadata will be sent out when one of the following happens:
851	//  - ServerStream.SendHeader() is called;
852	//  - The first response is sent out;
853	//  - An RPC status is sent out (error or success).
854	SetHeader(metadata.MD) error
855	// SendHeader sends the header metadata.
856	// The provided md and headers set by SetHeader() will be sent.
857	// It fails if called multiple times.
858	SendHeader(metadata.MD) error
859	// SetTrailer sets the trailer metadata which will be sent with the RPC status.
860	// When called more than once, all the provided metadata will be merged.
861	SetTrailer(metadata.MD)
862	// Context returns the context for this stream.
863	Context() context.Context
864	// SendMsg sends a message. On error, SendMsg aborts the stream and the
865	// error is returned directly.
866	//
867	// SendMsg blocks until:
868	//   - There is sufficient flow control to schedule m with the transport, or
869	//   - The stream is done, or
870	//   - The stream breaks.
871	//
872	// SendMsg does not wait until the message is received by the client. An
873	// untimely stream closure may result in lost messages.
874	//
875	// It is safe to have a goroutine calling SendMsg and another goroutine
876	// calling RecvMsg on the same stream at the same time, but it is not safe
877	// to call SendMsg on the same stream in different goroutines.
878	SendMsg(m interface{}) error
879	// RecvMsg blocks until it receives a message into m or the stream is
880	// done. It returns io.EOF when the client has performed a CloseSend. On
881	// any non-EOF error, the stream is aborted and the error contains the
882	// RPC status.
883	//
884	// It is safe to have a goroutine calling SendMsg and another goroutine
885	// calling RecvMsg on the same stream at the same time, but it is not
886	// safe to call RecvMsg on the same stream in different goroutines.
887	RecvMsg(m interface{}) error
888}
889
890// serverStream implements a server side Stream.
891type serverStream struct {
892	ctx   context.Context
893	t     transport.ServerTransport
894	s     *transport.Stream
895	p     *parser
896	codec baseCodec
897
898	cp     Compressor
899	dc     Decompressor
900	comp   encoding.Compressor
901	decomp encoding.Compressor
902
903	maxReceiveMessageSize int
904	maxSendMessageSize    int
905	trInfo                *traceInfo
906
907	statsHandler stats.Handler
908
909	mu sync.Mutex // protects trInfo.tr after the service handler runs.
910}
911
912func (ss *serverStream) Context() context.Context {
913	return ss.ctx
914}
915
916func (ss *serverStream) SetHeader(md metadata.MD) error {
917	if md.Len() == 0 {
918		return nil
919	}
920	return ss.s.SetHeader(md)
921}
922
923func (ss *serverStream) SendHeader(md metadata.MD) error {
924	return ss.t.WriteHeader(ss.s, md)
925}
926
927func (ss *serverStream) SetTrailer(md metadata.MD) {
928	if md.Len() == 0 {
929		return
930	}
931	ss.s.SetTrailer(md)
932}
933
934func (ss *serverStream) SendMsg(m interface{}) (err error) {
935	defer func() {
936		if ss.trInfo != nil {
937			ss.mu.Lock()
938			if ss.trInfo.tr != nil {
939				if err == nil {
940					ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
941				} else {
942					ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
943					ss.trInfo.tr.SetError()
944				}
945			}
946			ss.mu.Unlock()
947		}
948		if err != nil && err != io.EOF {
949			st, _ := status.FromError(toRPCErr(err))
950			ss.t.WriteStatus(ss.s, st)
951		}
952		if channelz.IsOn() && err == nil {
953			ss.t.IncrMsgSent()
954		}
955	}()
956	data, err := encode(ss.codec, m)
957	if err != nil {
958		return err
959	}
960	compData, err := compress(data, ss.cp, ss.comp)
961	if err != nil {
962		return err
963	}
964	hdr, payload := msgHeader(data, compData)
965	// TODO(dfawley): should we be checking len(data) instead?
966	if len(payload) > ss.maxSendMessageSize {
967		return status.Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(payload), ss.maxSendMessageSize)
968	}
969	if err := ss.t.Write(ss.s, hdr, payload, &transport.Options{Last: false}); err != nil {
970		return toRPCErr(err)
971	}
972	if ss.statsHandler != nil {
973		ss.statsHandler.HandleRPC(ss.s.Context(), outPayload(false, m, data, payload, time.Now()))
974	}
975	return nil
976}
977
978func (ss *serverStream) RecvMsg(m interface{}) (err error) {
979	defer func() {
980		if ss.trInfo != nil {
981			ss.mu.Lock()
982			if ss.trInfo.tr != nil {
983				if err == nil {
984					ss.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
985				} else if err != io.EOF {
986					ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
987					ss.trInfo.tr.SetError()
988				}
989			}
990			ss.mu.Unlock()
991		}
992		if err != nil && err != io.EOF {
993			st, _ := status.FromError(toRPCErr(err))
994			ss.t.WriteStatus(ss.s, st)
995		}
996		if channelz.IsOn() && err == nil {
997			ss.t.IncrMsgRecv()
998		}
999	}()
1000	var inPayload *stats.InPayload
1001	if ss.statsHandler != nil {
1002		inPayload = &stats.InPayload{}
1003	}
1004	if err := recv(ss.p, ss.codec, ss.s, ss.dc, m, ss.maxReceiveMessageSize, inPayload, ss.decomp); err != nil {
1005		if err == io.EOF {
1006			return err
1007		}
1008		if err == io.ErrUnexpectedEOF {
1009			err = status.Errorf(codes.Internal, io.ErrUnexpectedEOF.Error())
1010		}
1011		return toRPCErr(err)
1012	}
1013	if inPayload != nil {
1014		ss.statsHandler.HandleRPC(ss.s.Context(), inPayload)
1015	}
1016	return nil
1017}
1018
1019// MethodFromServerStream returns the method string for the input stream.
1020// The returned string is in the format of "/service/method".
1021func MethodFromServerStream(stream ServerStream) (string, bool) {
1022	return Method(stream.Context())
1023}
1024