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.
19 // Package transport defines and implements message oriented communication
20 // channel to complete various transactions (e.g., an RPC).
21 package transport // import "google.golang.org/grpc/transport"
29 "golang.org/x/net/context"
30 "golang.org/x/net/http2"
31 "google.golang.org/grpc/codes"
32 "google.golang.org/grpc/credentials"
33 "google.golang.org/grpc/keepalive"
34 "google.golang.org/grpc/metadata"
35 "google.golang.org/grpc/stats"
36 "google.golang.org/grpc/status"
37 "google.golang.org/grpc/tap"
40 // recvMsg represents the received msg from the transport. All transport
41 // protocol specific info has been removed.
44 // nil: received some data
45 // io.EOF: stream is completed. data is nil.
46 // other non-nil error: transport failure. data is nil.
50 // recvBuffer is an unbounded channel of recvMsg structs.
51 // Note recvBuffer differs from controlBuffer only in that recvBuffer
52 // holds a channel of only recvMsg structs instead of objects implementing "item" interface.
53 // recvBuffer is written to much more often than
54 // controlBuffer and using strict recvMsg structs helps avoid allocation in "recvBuffer.put"
55 type recvBuffer struct {
61 func newRecvBuffer() *recvBuffer {
63 c: make(chan recvMsg, 1),
68 func (b *recvBuffer) put(r recvMsg) {
71 if len(b.backlog) == 0 {
78 b.backlog = append(b.backlog, r)
81 func (b *recvBuffer) load() {
84 if len(b.backlog) > 0 {
86 case b.c <- b.backlog[0]:
87 b.backlog[0] = recvMsg{}
88 b.backlog = b.backlog[1:]
94 // get returns the channel that receives a recvMsg in the buffer.
96 // Upon receipt of a recvMsg, the caller should call load to send another
97 // recvMsg onto the channel if there is any.
98 func (b *recvBuffer) get() <-chan recvMsg {
102 // recvBufferReader implements io.Reader interface to read the data from
104 type recvBufferReader struct {
108 last []byte // Stores the remaining data in the previous calls.
112 // Read reads the next len(p) bytes from last. If last is drained, it tries to
113 // read additional data from recv. It blocks if there no additional data available
114 // in recv. If Read returns any non-nil error, it will continue to return that error.
115 func (r *recvBufferReader) Read(p []byte) (n int, err error) {
123 func (r *recvBufferReader) read(p []byte) (n int, err error) {
124 if r.last != nil && len(r.last) > 0 {
125 // Read remaining data left in last call.
126 copied := copy(p, r.last)
127 r.last = r.last[copied:]
132 return 0, ContextErr(r.ctx.Err())
134 return 0, ErrStreamDrain
135 case m := <-r.recv.get():
140 copied := copy(p, m.data)
141 r.last = m.data[copied:]
146 // All items in an out of a controlBuffer should be the same type.
147 type item interface {
151 // controlBuffer is an unbounded channel of item.
152 type controlBuffer struct {
158 func newControlBuffer() *controlBuffer {
160 c: make(chan item, 1),
165 func (b *controlBuffer) put(r item) {
168 if len(b.backlog) == 0 {
175 b.backlog = append(b.backlog, r)
178 func (b *controlBuffer) load() {
181 if len(b.backlog) > 0 {
183 case b.c <- b.backlog[0]:
185 b.backlog = b.backlog[1:]
191 // get returns the channel that receives an item in the buffer.
193 // Upon receipt of an item, the caller should call load to send another
194 // item onto the channel if there is any.
195 func (b *controlBuffer) get() <-chan item {
199 type streamState uint8
202 streamActive streamState = iota
203 streamWriteDone // EndStream sent
204 streamReadDone // EndStream received
205 streamDone // the entire stream is finished.
208 // Stream represents an RPC in the transport layer.
211 // nil for client side Stream.
213 // ctx is the associated context of the stream.
215 // cancel is always nil for client side Stream.
216 cancel context.CancelFunc
217 // done is closed when the final status arrives.
219 // goAway is closed when the server sent GoAways signal before this stream was initiated.
221 // method records the associated RPC method of the stream.
230 // TODO: Remote this unused variable.
231 // The accumulated inbound quota pending for window update.
234 // Callback to state application's intentions to read data. This
235 // is used to adjust flow control, if need be.
236 requestRead func(int)
238 sendQuotaPool *quotaPool
239 // Close headerChan to indicate the end of reception of header metadata.
240 headerChan chan struct{}
241 // header caches the received header metadata.
243 // The key-value map of trailer metadata.
246 mu sync.RWMutex // guard the following
247 // headerOK becomes true from the first header is about to send.
250 // true iff headerChan is closed. Used to avoid closing headerChan
253 // the status error received from the server.
254 status *status.Status
255 // rstStream indicates whether a RST_STREAM frame needs to be sent
256 // to the server to signify that this stream is closing.
258 // rstError is the error that needs to be sent along with the RST_STREAM frame.
259 rstError http2.ErrCode
260 // bytesSent and bytesReceived indicates whether any bytes have been sent or
261 // received on this stream.
266 // RecvCompress returns the compression algorithm applied to the inbound
267 // message. It is empty string if there is no compression applied.
268 func (s *Stream) RecvCompress() string {
269 return s.recvCompress
272 // SetSendCompress sets the compression algorithm to the stream.
273 func (s *Stream) SetSendCompress(str string) {
277 // Done returns a chanel which is closed when it receives the final status
279 func (s *Stream) Done() <-chan struct{} {
283 // GoAway returns a channel which is closed when the server sent GoAways signal
284 // before this stream was initiated.
285 func (s *Stream) GoAway() <-chan struct{} {
289 // Header acquires the key-value pairs of header metadata once it
290 // is available. It blocks until i) the metadata is ready or ii) there is no
291 // header metadata or iii) the stream is canceled/expired.
292 func (s *Stream) Header() (metadata.MD, error) {
296 err = ContextErr(s.ctx.Err())
300 return s.header.Copy(), nil
302 // Even if the stream is closed, header is returned if available.
305 return s.header.Copy(), nil
311 // Trailer returns the cached trailer metedata. Note that if it is not called
312 // after the entire stream is done, it could return an empty MD. Client
314 func (s *Stream) Trailer() metadata.MD {
317 return s.trailer.Copy()
320 // ServerTransport returns the underlying ServerTransport for the stream.
321 // The client side stream always returns nil.
322 func (s *Stream) ServerTransport() ServerTransport {
326 // Context returns the context of the stream.
327 func (s *Stream) Context() context.Context {
331 // Method returns the method for the stream.
332 func (s *Stream) Method() string {
336 // Status returns the status received from the server.
337 func (s *Stream) Status() *status.Status {
341 // SetHeader sets the header metadata. This can be called multiple times.
343 func (s *Stream) SetHeader(md metadata.MD) error {
346 if s.headerOk || s.state == streamDone {
347 return ErrIllegalHeaderWrite
352 s.header = metadata.Join(s.header, md)
356 // SetTrailer sets the trailer metadata which will be sent with the RPC status
357 // by the server. This can be called multiple times. Server side only.
358 func (s *Stream) SetTrailer(md metadata.MD) error {
364 s.trailer = metadata.Join(s.trailer, md)
368 func (s *Stream) write(m recvMsg) {
372 // Read reads all p bytes from the wire for this stream.
373 func (s *Stream) Read(p []byte) (n int, err error) {
374 // Don't request a read if there was an error earlier
375 if er := s.trReader.(*transportReader).er; er != nil {
378 s.requestRead(len(p))
379 return io.ReadFull(s.trReader, p)
382 // tranportReader reads all the data available for this Stream from the transport and
383 // passes them into the decoder, which converts them into a gRPC message stream.
384 // The error is io.EOF when the stream is done or another non-nil error if
386 type transportReader struct {
388 // The handler to control the window update procedure for both this
389 // particular stream and the associated transport.
390 windowHandler func(int)
394 func (t *transportReader) Read(p []byte) (n int, err error) {
395 n, err = t.reader.Read(p)
404 // finish sets the stream's state and status, and closes the done channel.
405 // s.mu must be held by the caller. st must always be non-nil.
406 func (s *Stream) finish(st *status.Status) {
412 // BytesSent indicates whether any bytes have been sent on this stream.
413 func (s *Stream) BytesSent() bool {
419 // BytesReceived indicates whether any bytes have been received on this stream.
420 func (s *Stream) BytesReceived() bool {
423 return s.bytesReceived
426 // GoString is implemented by Stream so context.String() won't
427 // race when printing %#v.
428 func (s *Stream) GoString() string {
429 return fmt.Sprintf("<stream: %p, %v>", s, s.method)
432 // The key to save transport.Stream in the context.
433 type streamKey struct{}
435 // newContextWithStream creates a new context from ctx and attaches stream
437 func newContextWithStream(ctx context.Context, stream *Stream) context.Context {
438 return context.WithValue(ctx, streamKey{}, stream)
441 // StreamFromContext returns the stream saved in ctx.
442 func StreamFromContext(ctx context.Context) (s *Stream, ok bool) {
443 s, ok = ctx.Value(streamKey{}).(*Stream)
447 // state of transport
448 type transportState int
451 reachable transportState = iota
457 // ServerConfig consists of all the configurations to establish a server transport.
458 type ServerConfig struct {
460 AuthInfo credentials.AuthInfo
461 InTapHandle tap.ServerInHandle
462 StatsHandler stats.Handler
463 KeepaliveParams keepalive.ServerParameters
464 KeepalivePolicy keepalive.EnforcementPolicy
465 InitialWindowSize int32
466 InitialConnWindowSize int32
469 // NewServerTransport creates a ServerTransport with conn or non-nil error
471 func NewServerTransport(protocol string, conn net.Conn, config *ServerConfig) (ServerTransport, error) {
472 return newHTTP2Server(conn, config)
475 // ConnectOptions covers all relevant options for communicating with the server.
476 type ConnectOptions struct {
477 // UserAgent is the application user agent.
479 // Authority is the :authority pseudo-header to use. This field has no effect if
480 // TransportCredentials is set.
482 // Dialer specifies how to dial a network address.
483 Dialer func(context.Context, string) (net.Conn, error)
484 // FailOnNonTempDialError specifies if gRPC fails on non-temporary dial errors.
485 FailOnNonTempDialError bool
486 // PerRPCCredentials stores the PerRPCCredentials required to issue RPCs.
487 PerRPCCredentials []credentials.PerRPCCredentials
488 // TransportCredentials stores the Authenticator required to setup a client connection.
489 TransportCredentials credentials.TransportCredentials
490 // KeepaliveParams stores the keepalive parameters.
491 KeepaliveParams keepalive.ClientParameters
492 // StatsHandler stores the handler for stats.
493 StatsHandler stats.Handler
494 // InitialWindowSize sets the intial window size for a stream.
495 InitialWindowSize int32
496 // InitialConnWindowSize sets the intial window size for a connection.
497 InitialConnWindowSize int32
500 // TargetInfo contains the information of the target such as network address and metadata.
501 type TargetInfo struct {
506 // NewClientTransport establishes the transport with the required ConnectOptions
507 // and returns it to the caller.
508 func NewClientTransport(ctx context.Context, target TargetInfo, opts ConnectOptions) (ClientTransport, error) {
509 return newHTTP2Client(ctx, target, opts)
512 // Options provides additional hints and information for message
514 type Options struct {
515 // Last indicates whether this write is the last piece for
519 // Delay is a hint to the transport implementation for whether
520 // the data could be buffered for a batching write. The
521 // Transport implementation may ignore the hint.
525 // CallHdr carries the information of a particular RPC.
526 type CallHdr struct {
527 // Host specifies the peer's host.
530 // Method specifies the operation to perform.
533 // RecvCompress specifies the compression algorithm applied on
537 // SendCompress specifies the compression algorithm applied on
541 // Creds specifies credentials.PerRPCCredentials for a call.
542 Creds credentials.PerRPCCredentials
544 // Flush indicates whether a new stream command should be sent
545 // to the peer without waiting for the first data. This is
547 // If it's true, the transport may modify the flush decision
548 // for performance purposes.
549 // If it's false, new stream will never be flushed.
553 // ClientTransport is the common interface for all gRPC client-side transport
555 type ClientTransport interface {
556 // Close tears down this transport. Once it returns, the transport
557 // should not be accessed any more. The caller must make sure this
558 // is called only once.
561 // GracefulClose starts to tear down the transport. It stops accepting
562 // new RPCs and wait the completion of the pending RPCs.
563 GracefulClose() error
565 // Write sends the data for the given stream. A nil stream indicates
566 // the write is to be performed on the transport as a whole.
567 Write(s *Stream, data []byte, opts *Options) error
569 // NewStream creates a Stream for an RPC.
570 NewStream(ctx context.Context, callHdr *CallHdr) (*Stream, error)
572 // CloseStream clears the footprint of a stream when the stream is
573 // not needed any more. The err indicates the error incurred when
574 // CloseStream is called. Must be called when a stream is finished
575 // unless the associated transport is closing.
576 CloseStream(stream *Stream, err error)
578 // Error returns a channel that is closed when some I/O error
579 // happens. Typically the caller should have a goroutine to monitor
580 // this in order to take action (e.g., close the current transport
581 // and create a new one) in error case. It should not return nil
582 // once the transport is initiated.
583 Error() <-chan struct{}
585 // GoAway returns a channel that is closed when ClientTransport
586 // receives the draining signal from the server (e.g., GOAWAY frame in
588 GoAway() <-chan struct{}
590 // GetGoAwayReason returns the reason why GoAway frame was received.
591 GetGoAwayReason() GoAwayReason
594 // ServerTransport is the common interface for all gRPC server-side transport
597 // Methods may be called concurrently from multiple goroutines, but
598 // Write methods for a given Stream will be called serially.
599 type ServerTransport interface {
600 // HandleStreams receives incoming streams using the given handler.
601 HandleStreams(func(*Stream), func(context.Context, string) context.Context)
603 // WriteHeader sends the header metadata for the given stream.
604 // WriteHeader may not be called on all streams.
605 WriteHeader(s *Stream, md metadata.MD) error
607 // Write sends the data for the given stream.
608 // Write may not be called on all streams.
609 Write(s *Stream, data []byte, opts *Options) error
611 // WriteStatus sends the status of a stream to the client. WriteStatus is
612 // the final call made on a stream and always occurs.
613 WriteStatus(s *Stream, st *status.Status) error
615 // Close tears down the transport. Once it is called, the transport
616 // should not be accessed any more. All the pending streams and their
617 // handlers will be terminated asynchronously.
620 // RemoteAddr returns the remote network address.
621 RemoteAddr() net.Addr
623 // Drain notifies the client this ServerTransport stops accepting new RPCs.
627 // streamErrorf creates an StreamError with the specified error code and description.
628 func streamErrorf(c codes.Code, format string, a ...interface{}) StreamError {
631 Desc: fmt.Sprintf(format, a...),
635 // connectionErrorf creates an ConnectionError with the specified error description.
636 func connectionErrorf(temp bool, e error, format string, a ...interface{}) ConnectionError {
637 return ConnectionError{
638 Desc: fmt.Sprintf(format, a...),
644 // ConnectionError is an error that results in the termination of the
645 // entire connection and the retry of all the active streams.
646 type ConnectionError struct {
652 func (e ConnectionError) Error() string {
653 return fmt.Sprintf("connection error: desc = %q", e.Desc)
656 // Temporary indicates if this connection error is temporary or fatal.
657 func (e ConnectionError) Temporary() bool {
661 // Origin returns the original error of this connection error.
662 func (e ConnectionError) Origin() error {
663 // Never return nil error here.
664 // If the original error is nil, return itself.
672 // ErrConnClosing indicates that the transport is closing.
673 ErrConnClosing = connectionErrorf(true, nil, "transport is closing")
674 // ErrStreamDrain indicates that the stream is rejected by the server because
675 // the server stops accepting new RPCs.
676 ErrStreamDrain = streamErrorf(codes.Unavailable, "the server stops accepting new RPCs")
679 // TODO: See if we can replace StreamError with status package errors.
681 // StreamError is an error that only affects one stream within a connection.
682 type StreamError struct {
687 func (e StreamError) Error() string {
688 return fmt.Sprintf("stream error: code = %s desc = %q", e.Code, e.Desc)
691 // wait blocks until it can receive from ctx.Done, closing, or proceed.
692 // If it receives from ctx.Done, it returns 0, the StreamError for ctx.Err.
693 // If it receives from done, it returns 0, io.EOF if ctx is not done; otherwise
694 // it return the StreamError for ctx.Err.
695 // If it receives from goAway, it returns 0, ErrStreamDrain.
696 // If it receives from closing, it returns 0, ErrConnClosing.
697 // If it receives from proceed, it returns the received integer, nil.
698 func wait(ctx context.Context, done, goAway, closing <-chan struct{}, proceed <-chan int) (int, error) {
701 return 0, ContextErr(ctx.Err())
703 // User cancellation has precedence.
706 return 0, ContextErr(ctx.Err())
711 return 0, ErrStreamDrain
713 return 0, ErrConnClosing
719 // GoAwayReason contains the reason for the GoAway frame received.
720 type GoAwayReason uint8
723 // Invalid indicates that no GoAway frame is received.
724 Invalid GoAwayReason = 0
725 // NoReason is the default value when GoAway frame is received.
726 NoReason GoAwayReason = 1
727 // TooManyPings indicates that a GoAway frame with ErrCodeEnhanceYourCalm
728 // was recieved and that the debug data said "too_many_pings".
729 TooManyPings GoAwayReason = 2