3 * Copyright 2014 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 "golang.org/x/net/context"
29 "golang.org/x/net/trace"
30 "google.golang.org/grpc/balancer"
31 "google.golang.org/grpc/codes"
32 "google.golang.org/grpc/metadata"
33 "google.golang.org/grpc/peer"
34 "google.golang.org/grpc/stats"
35 "google.golang.org/grpc/status"
36 "google.golang.org/grpc/transport"
39 // StreamHandler defines the handler called by gRPC server to complete the
40 // execution of a streaming RPC.
41 type StreamHandler func(srv interface{}, stream ServerStream) error
43 // StreamDesc represents a streaming RPC service's method specification.
44 type StreamDesc struct {
48 // At least one of these is true.
53 // Stream defines the common interface a client or server stream has to satisfy.
54 type Stream interface {
55 // Context returns the context for this stream.
56 Context() context.Context
57 // SendMsg blocks until it sends m, the stream is done or the stream
59 // On error, it aborts the stream and returns an RPC status on client
60 // side. On server side, it simply returns the error to the caller.
61 // SendMsg is called by generated code. Also Users can call SendMsg
62 // directly when it is really needed in their use cases.
63 // It's safe to have a goroutine calling SendMsg and another goroutine calling
64 // recvMsg on the same stream at the same time.
65 // But it is not safe to call SendMsg on the same stream in different goroutines.
66 SendMsg(m interface{}) error
67 // RecvMsg blocks until it receives a message or the stream is
68 // done. On client side, it returns io.EOF when the stream is done. On
69 // any other error, it aborts the stream and returns an RPC status. On
70 // server side, it simply returns the error to the caller.
71 // It's safe to have a goroutine calling SendMsg and another goroutine calling
72 // recvMsg on the same stream at the same time.
73 // But it is not safe to call RecvMsg on the same stream in different goroutines.
74 RecvMsg(m interface{}) error
77 // ClientStream defines the interface a client stream has to satisfy.
78 type ClientStream interface {
79 // Header returns the header metadata received from the server if there
80 // is any. It blocks if the metadata is not ready to read.
81 Header() (metadata.MD, error)
82 // Trailer returns the trailer metadata from the server, if there is any.
83 // It must only be called after stream.CloseAndRecv has returned, or
84 // stream.Recv has returned a non-nil error (including io.EOF).
86 // CloseSend closes the send direction of the stream. It closes the stream
87 // when non-nil error is met.
89 // Stream.SendMsg() may return a non-nil error when something wrong happens sending
90 // the request. The returned error indicates the status of this sending, not the final
92 // Always call Stream.RecvMsg() to get the final status if you care about the status of
97 // NewStream creates a new Stream for the client side. This is typically
98 // called by generated code.
99 func (cc *ClientConn) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
100 if cc.dopts.streamInt != nil {
101 return cc.dopts.streamInt(ctx, desc, cc, method, newClientStream, opts...)
103 return newClientStream(ctx, desc, cc, method, opts...)
106 // NewClientStream creates a new Stream for the client side. This is typically
107 // called by generated code.
109 // DEPRECATED: Use ClientConn.NewStream instead.
110 func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
111 return cc.NewStream(ctx, desc, method, opts...)
114 func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (_ ClientStream, err error) {
116 t transport.ClientTransport
118 done func(balancer.DoneInfo)
119 cancel context.CancelFunc
121 c := defaultCallInfo()
122 mc := cc.GetMethodConfig(method)
123 if mc.WaitForReady != nil {
124 c.failFast = !*mc.WaitForReady
127 if mc.Timeout != nil {
128 ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
136 opts = append(cc.dopts.callOptions, opts...)
137 for _, o := range opts {
138 if err := o.before(c); err != nil {
139 return nil, toRPCErr(err)
142 c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize)
143 c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
145 callHdr := &transport.CallHdr{
148 // If it's not client streaming, we should already have the request to be sent,
149 // so we don't flush the header.
150 // If it's client streaming, the user may never send a request or send it any
151 // time soon, so we ask the transport to flush the header.
152 Flush: desc.ClientStreams,
154 if cc.dopts.cp != nil {
155 callHdr.SendCompress = cc.dopts.cp.Type()
158 callHdr.Creds = c.creds
162 trInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method)
163 trInfo.firstLine.client = true
164 if deadline, ok := ctx.Deadline(); ok {
165 trInfo.firstLine.deadline = deadline.Sub(time.Now())
167 trInfo.tr.LazyLog(&trInfo.firstLine, false)
168 ctx = trace.NewContext(ctx, trInfo.tr)
171 // Need to call tr.finish() if error is returned.
172 // Because tr will not be returned to caller.
173 trInfo.tr.LazyPrintf("RPC: [%v]", err)
179 ctx = newContextWithRPCInfo(ctx, c.failFast)
180 sh := cc.dopts.copts.StatsHandler
182 ctx = sh.TagRPC(ctx, &stats.RPCTagInfo{FullMethodName: method, FailFast: c.failFast})
183 begin := &stats.Begin{
185 BeginTime: time.Now(),
186 FailFast: c.failFast,
188 sh.HandleRPC(ctx, begin)
191 // Only handle end stats if err != nil.
196 sh.HandleRPC(ctx, end)
201 t, done, err = cc.getTransport(ctx, c.failFast)
203 // TODO(zhaoq): Probably revisit the error handling.
204 if _, ok := status.FromError(err); ok {
207 if err == errConnClosing || err == errConnUnavailable {
209 return nil, Errorf(codes.Unavailable, "%v", err)
213 // All the other errors are treated as Internal errors.
214 return nil, Errorf(codes.Internal, "%v", err)
217 s, err = t.NewStream(ctx, callHdr)
219 if _, ok := err.(transport.ConnectionError); ok && done != nil {
220 // If error is connection error, transport was sending data on wire,
221 // and we are not sure if anything has been sent on wire.
222 // If error is not connection error, we are sure nothing has been sent.
223 updateRPCInfoInContext(ctx, rpcInfo{bytesSent: true, bytesReceived: false})
226 done(balancer.DoneInfo{Err: err})
229 if _, ok := err.(transport.ConnectionError); (ok || err == transport.ErrStreamDrain) && !c.failFast {
232 return nil, toRPCErr(err)
236 // Set callInfo.peer object from stream's context.
237 if peer, ok := peer.FromContext(s.Context()); ok {
244 codec: cc.dopts.codec,
254 tracing: EnableTracing,
258 statsHandler: cc.dopts.copts.StatsHandler,
260 // Listen on ctx.Done() to detect cancellation and s.Done() to detect normal termination
261 // when there is no pending I/O operations on this stream.
265 // Incur transport error, simply exit.
266 case <-cc.ctx.Done():
267 cs.finish(ErrClientConnClosing)
268 cs.closeTransportStream(ErrClientConnClosing)
270 // TODO: The trace of the RPC is terminated here when there is no pending
271 // I/O, which is probably not the optimal solution.
272 cs.finish(s.Status().Err())
273 cs.closeTransportStream(nil)
275 cs.finish(errConnDrain)
276 cs.closeTransportStream(errConnDrain)
277 case <-s.Context().Done():
278 err := s.Context().Err()
280 cs.closeTransportStream(transport.ContextErr(err))
286 // clientStream implements a client side Stream.
287 type clientStream struct {
290 t transport.ClientTransport
297 cancel context.CancelFunc
299 tracing bool // set to EnableTracing when the clientStream is created.
302 done func(balancer.DoneInfo)
305 // trInfo.tr is set when the clientStream is created (if EnableTracing is true),
306 // and is set to nil when the clientStream's finish method is called.
309 // statsCtx keeps the user context for stats handling.
310 // All stats collection should use the statsCtx (instead of the stream context)
311 // so that all the generated stats for a particular RPC can be associated in the processing phase.
312 statsCtx context.Context
313 statsHandler stats.Handler
316 func (cs *clientStream) Context() context.Context {
317 return cs.s.Context()
320 func (cs *clientStream) Header() (metadata.MD, error) {
321 m, err := cs.s.Header()
323 if _, ok := err.(transport.ConnectionError); !ok {
324 cs.closeTransportStream(err)
330 func (cs *clientStream) Trailer() metadata.MD {
331 return cs.s.Trailer()
334 func (cs *clientStream) SendMsg(m interface{}) (err error) {
337 if cs.trInfo.tr != nil {
338 cs.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
342 // TODO Investigate how to signal the stats handling party.
343 // generate error stats if err != nil && err != io.EOF?
352 // Specialize the process for server streaming. SendMsg is only called
353 // once when creating the stream object. io.EOF needs to be skipped when
354 // the rpc is early finished (before the stream object is created.).
355 // TODO: It is probably better to move this into the generated code.
356 if !cs.desc.ClientStreams && cs.desc.ServerStreams {
361 if _, ok := err.(transport.ConnectionError); !ok {
362 cs.closeTransportStream(err)
366 var outPayload *stats.OutPayload
367 if cs.statsHandler != nil {
368 outPayload = &stats.OutPayload{
372 hdr, data, err := encode(cs.codec, m, cs.cp, bytes.NewBuffer([]byte{}), outPayload)
376 if cs.c.maxSendMessageSize == nil {
377 return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
379 if len(data) > *cs.c.maxSendMessageSize {
380 return Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(data), *cs.c.maxSendMessageSize)
382 err = cs.t.Write(cs.s, hdr, data, &transport.Options{Last: false})
383 if err == nil && outPayload != nil {
384 outPayload.SentTime = time.Now()
385 cs.statsHandler.HandleRPC(cs.statsCtx, outPayload)
390 func (cs *clientStream) RecvMsg(m interface{}) (err error) {
391 var inPayload *stats.InPayload
392 if cs.statsHandler != nil {
393 inPayload = &stats.InPayload{
397 if cs.c.maxReceiveMessageSize == nil {
398 return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
400 err = recv(cs.p, cs.codec, cs.s, cs.dc, m, *cs.c.maxReceiveMessageSize, inPayload)
402 // err != nil indicates the termination of the stream.
410 if cs.trInfo.tr != nil {
411 cs.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
415 if inPayload != nil {
416 cs.statsHandler.HandleRPC(cs.statsCtx, inPayload)
418 if !cs.desc.ClientStreams || cs.desc.ServerStreams {
421 // Special handling for client streaming rpc.
422 // This recv expects EOF or errors, so we don't collect inPayload.
423 if cs.c.maxReceiveMessageSize == nil {
424 return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)")
426 err = recv(cs.p, cs.codec, cs.s, cs.dc, m, *cs.c.maxReceiveMessageSize, nil)
427 cs.closeTransportStream(err)
429 return toRPCErr(errors.New("grpc: client streaming protocol violation: get <nil>, want <EOF>"))
432 if se := cs.s.Status().Err(); se != nil {
440 if _, ok := err.(transport.ConnectionError); !ok {
441 cs.closeTransportStream(err)
444 if statusErr := cs.s.Status().Err(); statusErr != nil {
447 // Returns io.EOF to indicate the end of the stream.
453 func (cs *clientStream) CloseSend() (err error) {
454 err = cs.t.Write(cs.s, nil, nil, &transport.Options{Last: true})
460 if err == nil || err == io.EOF {
463 if _, ok := err.(transport.ConnectionError); !ok {
464 cs.closeTransportStream(err)
470 func (cs *clientStream) closeTransportStream(err error) {
478 cs.t.CloseStream(cs.s, err)
481 func (cs *clientStream) finish(err error) {
489 if cs.cancel != nil {
493 for _, o := range cs.opts {
497 updateRPCInfoInContext(cs.s.Context(), rpcInfo{
498 bytesSent: cs.s.BytesSent(),
499 bytesReceived: cs.s.BytesReceived(),
501 cs.done(balancer.DoneInfo{Err: err})
504 if cs.statsHandler != nil {
510 // end.Error is nil if the RPC finished successfully.
511 end.Error = toRPCErr(err)
513 cs.statsHandler.HandleRPC(cs.statsCtx, end)
518 if cs.trInfo.tr != nil {
519 if err == nil || err == io.EOF {
520 cs.trInfo.tr.LazyPrintf("RPC: [OK]")
522 cs.trInfo.tr.LazyPrintf("RPC: [%v]", err)
523 cs.trInfo.tr.SetError()
525 cs.trInfo.tr.Finish()
530 // ServerStream defines the interface a server stream has to satisfy.
531 type ServerStream interface {
532 // SetHeader sets the header metadata. It may be called multiple times.
533 // When call multiple times, all the provided metadata will be merged.
534 // All the metadata will be sent out when one of the following happens:
535 // - ServerStream.SendHeader() is called;
536 // - The first response is sent out;
537 // - An RPC status is sent out (error or success).
538 SetHeader(metadata.MD) error
539 // SendHeader sends the header metadata.
540 // The provided md and headers set by SetHeader() will be sent.
541 // It fails if called multiple times.
542 SendHeader(metadata.MD) error
543 // SetTrailer sets the trailer metadata which will be sent with the RPC status.
544 // When called more than once, all the provided metadata will be merged.
545 SetTrailer(metadata.MD)
549 // serverStream implements a server side Stream.
550 type serverStream struct {
551 t transport.ServerTransport
557 maxReceiveMessageSize int
558 maxSendMessageSize int
561 statsHandler stats.Handler
563 mu sync.Mutex // protects trInfo.tr after the service handler runs.
566 func (ss *serverStream) Context() context.Context {
567 return ss.s.Context()
570 func (ss *serverStream) SetHeader(md metadata.MD) error {
574 return ss.s.SetHeader(md)
577 func (ss *serverStream) SendHeader(md metadata.MD) error {
578 return ss.t.WriteHeader(ss.s, md)
581 func (ss *serverStream) SetTrailer(md metadata.MD) {
589 func (ss *serverStream) SendMsg(m interface{}) (err error) {
591 if ss.trInfo != nil {
593 if ss.trInfo.tr != nil {
595 ss.trInfo.tr.LazyLog(&payload{sent: true, msg: m}, true)
597 ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
598 ss.trInfo.tr.SetError()
603 if err != nil && err != io.EOF {
604 st, _ := status.FromError(toRPCErr(err))
605 ss.t.WriteStatus(ss.s, st)
608 var outPayload *stats.OutPayload
609 if ss.statsHandler != nil {
610 outPayload = &stats.OutPayload{}
612 hdr, data, err := encode(ss.codec, m, ss.cp, bytes.NewBuffer([]byte{}), outPayload)
616 if len(data) > ss.maxSendMessageSize {
617 return Errorf(codes.ResourceExhausted, "trying to send message larger than max (%d vs. %d)", len(data), ss.maxSendMessageSize)
619 if err := ss.t.Write(ss.s, hdr, data, &transport.Options{Last: false}); err != nil {
622 if outPayload != nil {
623 outPayload.SentTime = time.Now()
624 ss.statsHandler.HandleRPC(ss.s.Context(), outPayload)
629 func (ss *serverStream) RecvMsg(m interface{}) (err error) {
631 if ss.trInfo != nil {
633 if ss.trInfo.tr != nil {
635 ss.trInfo.tr.LazyLog(&payload{sent: false, msg: m}, true)
636 } else if err != io.EOF {
637 ss.trInfo.tr.LazyLog(&fmtStringer{"%v", []interface{}{err}}, true)
638 ss.trInfo.tr.SetError()
643 if err != nil && err != io.EOF {
644 st, _ := status.FromError(toRPCErr(err))
645 ss.t.WriteStatus(ss.s, st)
648 var inPayload *stats.InPayload
649 if ss.statsHandler != nil {
650 inPayload = &stats.InPayload{}
652 if err := recv(ss.p, ss.codec, ss.s, ss.dc, m, ss.maxReceiveMessageSize, inPayload); err != nil {
656 if err == io.ErrUnexpectedEOF {
657 err = Errorf(codes.Internal, io.ErrUnexpectedEOF.Error())
661 if inPayload != nil {
662 ss.statsHandler.HandleRPC(ss.s.Context(), inPayload)
667 // MethodFromServerStream returns the method string for the input stream.
668 // The returned string is in the format of "/service/method".
669 func MethodFromServerStream(stream ServerStream) (string, bool) {
670 s, ok := transport.StreamFromContext(stream.Context())
674 return s.Method(), ok