http2_client.go

   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 transport
  20
  21import (
  22	"context"
  23	"fmt"
  24	"io"
  25	"math"
  26	"net"
  27	"net/http"
  28	"path/filepath"
  29	"strconv"
  30	"strings"
  31	"sync"
  32	"sync/atomic"
  33	"time"
  34
  35	"golang.org/x/net/http2"
  36	"golang.org/x/net/http2/hpack"
  37	"google.golang.org/grpc/codes"
  38	"google.golang.org/grpc/credentials"
  39	"google.golang.org/grpc/internal"
  40	"google.golang.org/grpc/internal/channelz"
  41	icredentials "google.golang.org/grpc/internal/credentials"
  42	"google.golang.org/grpc/internal/grpclog"
  43	"google.golang.org/grpc/internal/grpcsync"
  44	"google.golang.org/grpc/internal/grpcutil"
  45	imetadata "google.golang.org/grpc/internal/metadata"
  46	"google.golang.org/grpc/internal/proxyattributes"
  47	istatus "google.golang.org/grpc/internal/status"
  48	isyscall "google.golang.org/grpc/internal/syscall"
  49	"google.golang.org/grpc/internal/transport/networktype"
  50	"google.golang.org/grpc/keepalive"
  51	"google.golang.org/grpc/mem"
  52	"google.golang.org/grpc/metadata"
  53	"google.golang.org/grpc/peer"
  54	"google.golang.org/grpc/resolver"
  55	"google.golang.org/grpc/stats"
  56	"google.golang.org/grpc/status"
  57)
  58
  59// clientConnectionCounter counts the number of connections a client has
  60// initiated (equal to the number of http2Clients created). Must be accessed
  61// atomically.
  62var clientConnectionCounter uint64
  63
  64var goAwayLoopyWriterTimeout = 5 * time.Second
  65
  66var metadataFromOutgoingContextRaw = internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))
  67
  68// http2Client implements the ClientTransport interface with HTTP2.
  69type http2Client struct {
  70	lastRead  int64 // Keep this field 64-bit aligned. Accessed atomically.
  71	ctx       context.Context
  72	cancel    context.CancelFunc
  73	ctxDone   <-chan struct{} // Cache the ctx.Done() chan.
  74	userAgent string
  75	// address contains the resolver returned address for this transport.
  76	// If the `ServerName` field is set, it takes precedence over `CallHdr.Host`
  77	// passed to `NewStream`, when determining the :authority header.
  78	address    resolver.Address
  79	md         metadata.MD
  80	conn       net.Conn // underlying communication channel
  81	loopy      *loopyWriter
  82	remoteAddr net.Addr
  83	localAddr  net.Addr
  84	authInfo   credentials.AuthInfo // auth info about the connection
  85
  86	readerDone chan struct{} // sync point to enable testing.
  87	writerDone chan struct{} // sync point to enable testing.
  88	// goAway is closed to notify the upper layer (i.e., addrConn.transportMonitor)
  89	// that the server sent GoAway on this transport.
  90	goAway        chan struct{}
  91	keepaliveDone chan struct{} // Closed when the keepalive goroutine exits.
  92	framer        *framer
  93	// controlBuf delivers all the control related tasks (e.g., window
  94	// updates, reset streams, and various settings) to the controller.
  95	// Do not access controlBuf with mu held.
  96	controlBuf *controlBuffer
  97	fc         *trInFlow
  98	// The scheme used: https if TLS is on, http otherwise.
  99	scheme string
 100
 101	isSecure bool
 102
 103	perRPCCreds []credentials.PerRPCCredentials
 104
 105	kp               keepalive.ClientParameters
 106	keepaliveEnabled bool
 107
 108	statsHandlers []stats.Handler
 109
 110	initialWindowSize int32
 111
 112	// configured by peer through SETTINGS_MAX_HEADER_LIST_SIZE
 113	maxSendHeaderListSize *uint32
 114
 115	bdpEst *bdpEstimator
 116
 117	maxConcurrentStreams  uint32
 118	streamQuota           int64
 119	streamsQuotaAvailable chan struct{}
 120	waitingStreams        uint32
 121	registeredCompressors string
 122
 123	// Do not access controlBuf with mu held.
 124	mu            sync.Mutex // guard the following variables
 125	nextID        uint32
 126	state         transportState
 127	activeStreams map[uint32]*ClientStream
 128	// prevGoAway ID records the Last-Stream-ID in the previous GOAway frame.
 129	prevGoAwayID uint32
 130	// goAwayReason records the http2.ErrCode and debug data received with the
 131	// GoAway frame.
 132	goAwayReason GoAwayReason
 133	// goAwayDebugMessage contains a detailed human readable string about a
 134	// GoAway frame, useful for error messages.
 135	goAwayDebugMessage string
 136	// A condition variable used to signal when the keepalive goroutine should
 137	// go dormant. The condition for dormancy is based on the number of active
 138	// streams and the `PermitWithoutStream` keepalive client parameter. And
 139	// since the number of active streams is guarded by the above mutex, we use
 140	// the same for this condition variable as well.
 141	kpDormancyCond *sync.Cond
 142	// A boolean to track whether the keepalive goroutine is dormant or not.
 143	// This is checked before attempting to signal the above condition
 144	// variable.
 145	kpDormant bool
 146
 147	channelz *channelz.Socket
 148
 149	onClose func(GoAwayReason)
 150
 151	bufferPool mem.BufferPool
 152
 153	connectionID uint64
 154	logger       *grpclog.PrefixLogger
 155}
 156
 157func dial(ctx context.Context, fn func(context.Context, string) (net.Conn, error), addr resolver.Address, grpcUA string) (net.Conn, error) {
 158	address := addr.Addr
 159	networkType, ok := networktype.Get(addr)
 160	if fn != nil {
 161		// Special handling for unix scheme with custom dialer. Back in the day,
 162		// we did not have a unix resolver and therefore targets with a unix
 163		// scheme would end up using the passthrough resolver. So, user's used a
 164		// custom dialer in this case and expected the original dial target to
 165		// be passed to the custom dialer. Now, we have a unix resolver. But if
 166		// a custom dialer is specified, we want to retain the old behavior in
 167		// terms of the address being passed to the custom dialer.
 168		if networkType == "unix" && !strings.HasPrefix(address, "\x00") {
 169			// Supported unix targets are either "unix://absolute-path" or
 170			// "unix:relative-path".
 171			if filepath.IsAbs(address) {
 172				return fn(ctx, "unix://"+address)
 173			}
 174			return fn(ctx, "unix:"+address)
 175		}
 176		return fn(ctx, address)
 177	}
 178	if !ok {
 179		networkType, address = parseDialTarget(address)
 180	}
 181	if opts, present := proxyattributes.Get(addr); present {
 182		return proxyDial(ctx, addr, grpcUA, opts)
 183	}
 184	return internal.NetDialerWithTCPKeepalive().DialContext(ctx, networkType, address)
 185}
 186
 187func isTemporary(err error) bool {
 188	switch err := err.(type) {
 189	case interface {
 190		Temporary() bool
 191	}:
 192		return err.Temporary()
 193	case interface {
 194		Timeout() bool
 195	}:
 196		// Timeouts may be resolved upon retry, and are thus treated as
 197		// temporary.
 198		return err.Timeout()
 199	}
 200	return true
 201}
 202
 203// NewHTTP2Client constructs a connected ClientTransport to addr based on HTTP2
 204// and starts to receive messages on it. Non-nil error returns if construction
 205// fails.
 206func NewHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts ConnectOptions, onClose func(GoAwayReason)) (_ ClientTransport, err error) {
 207	scheme := "http"
 208	ctx, cancel := context.WithCancel(ctx)
 209	defer func() {
 210		if err != nil {
 211			cancel()
 212		}
 213	}()
 214
 215	// gRPC, resolver, balancer etc. can specify arbitrary data in the
 216	// Attributes field of resolver.Address, which is shoved into connectCtx
 217	// and passed to the dialer and credential handshaker. This makes it possible for
 218	// address specific arbitrary data to reach custom dialers and credential handshakers.
 219	connectCtx = icredentials.NewClientHandshakeInfoContext(connectCtx, credentials.ClientHandshakeInfo{Attributes: addr.Attributes})
 220
 221	conn, err := dial(connectCtx, opts.Dialer, addr, opts.UserAgent)
 222	if err != nil {
 223		if opts.FailOnNonTempDialError {
 224			return nil, connectionErrorf(isTemporary(err), err, "transport: error while dialing: %v", err)
 225		}
 226		return nil, connectionErrorf(true, err, "transport: Error while dialing: %v", err)
 227	}
 228
 229	// Any further errors will close the underlying connection
 230	defer func(conn net.Conn) {
 231		if err != nil {
 232			conn.Close()
 233		}
 234	}(conn)
 235
 236	// The following defer and goroutine monitor the connectCtx for cancellation
 237	// and deadline.  On context expiration, the connection is hard closed and
 238	// this function will naturally fail as a result.  Otherwise, the defer
 239	// waits for the goroutine to exit to prevent the context from being
 240	// monitored (and to prevent the connection from ever being closed) after
 241	// returning from this function.
 242	ctxMonitorDone := grpcsync.NewEvent()
 243	newClientCtx, newClientDone := context.WithCancel(connectCtx)
 244	defer func() {
 245		newClientDone()         // Awaken the goroutine below if connectCtx hasn't expired.
 246		<-ctxMonitorDone.Done() // Wait for the goroutine below to exit.
 247	}()
 248	go func(conn net.Conn) {
 249		defer ctxMonitorDone.Fire() // Signal this goroutine has exited.
 250		<-newClientCtx.Done()       // Block until connectCtx expires or the defer above executes.
 251		if err := connectCtx.Err(); err != nil {
 252			// connectCtx expired before exiting the function.  Hard close the connection.
 253			if logger.V(logLevel) {
 254				logger.Infof("Aborting due to connect deadline expiring: %v", err)
 255			}
 256			conn.Close()
 257		}
 258	}(conn)
 259
 260	kp := opts.KeepaliveParams
 261	// Validate keepalive parameters.
 262	if kp.Time == 0 {
 263		kp.Time = defaultClientKeepaliveTime
 264	}
 265	if kp.Timeout == 0 {
 266		kp.Timeout = defaultClientKeepaliveTimeout
 267	}
 268	keepaliveEnabled := false
 269	if kp.Time != infinity {
 270		if err = isyscall.SetTCPUserTimeout(conn, kp.Timeout); err != nil {
 271			return nil, connectionErrorf(false, err, "transport: failed to set TCP_USER_TIMEOUT: %v", err)
 272		}
 273		keepaliveEnabled = true
 274	}
 275	var (
 276		isSecure bool
 277		authInfo credentials.AuthInfo
 278	)
 279	transportCreds := opts.TransportCredentials
 280	perRPCCreds := opts.PerRPCCredentials
 281
 282	if b := opts.CredsBundle; b != nil {
 283		if t := b.TransportCredentials(); t != nil {
 284			transportCreds = t
 285		}
 286		if t := b.PerRPCCredentials(); t != nil {
 287			perRPCCreds = append(perRPCCreds, t)
 288		}
 289	}
 290	if transportCreds != nil {
 291		conn, authInfo, err = transportCreds.ClientHandshake(connectCtx, addr.ServerName, conn)
 292		if err != nil {
 293			return nil, connectionErrorf(isTemporary(err), err, "transport: authentication handshake failed: %v", err)
 294		}
 295		for _, cd := range perRPCCreds {
 296			if cd.RequireTransportSecurity() {
 297				if ci, ok := authInfo.(interface {
 298					GetCommonAuthInfo() credentials.CommonAuthInfo
 299				}); ok {
 300					secLevel := ci.GetCommonAuthInfo().SecurityLevel
 301					if secLevel != credentials.InvalidSecurityLevel && secLevel < credentials.PrivacyAndIntegrity {
 302						return nil, connectionErrorf(true, nil, "transport: cannot send secure credentials on an insecure connection")
 303					}
 304				}
 305			}
 306		}
 307		isSecure = true
 308		if transportCreds.Info().SecurityProtocol == "tls" {
 309			scheme = "https"
 310		}
 311	}
 312	dynamicWindow := true
 313	icwz := int32(initialWindowSize)
 314	if opts.InitialConnWindowSize >= defaultWindowSize {
 315		icwz = opts.InitialConnWindowSize
 316		dynamicWindow = false
 317	}
 318	writeBufSize := opts.WriteBufferSize
 319	readBufSize := opts.ReadBufferSize
 320	maxHeaderListSize := defaultClientMaxHeaderListSize
 321	if opts.MaxHeaderListSize != nil {
 322		maxHeaderListSize = *opts.MaxHeaderListSize
 323	}
 324
 325	t := &http2Client{
 326		ctx:                   ctx,
 327		ctxDone:               ctx.Done(), // Cache Done chan.
 328		cancel:                cancel,
 329		userAgent:             opts.UserAgent,
 330		registeredCompressors: grpcutil.RegisteredCompressors(),
 331		address:               addr,
 332		conn:                  conn,
 333		remoteAddr:            conn.RemoteAddr(),
 334		localAddr:             conn.LocalAddr(),
 335		authInfo:              authInfo,
 336		readerDone:            make(chan struct{}),
 337		writerDone:            make(chan struct{}),
 338		goAway:                make(chan struct{}),
 339		keepaliveDone:         make(chan struct{}),
 340		framer:                newFramer(conn, writeBufSize, readBufSize, opts.SharedWriteBuffer, maxHeaderListSize),
 341		fc:                    &trInFlow{limit: uint32(icwz)},
 342		scheme:                scheme,
 343		activeStreams:         make(map[uint32]*ClientStream),
 344		isSecure:              isSecure,
 345		perRPCCreds:           perRPCCreds,
 346		kp:                    kp,
 347		statsHandlers:         opts.StatsHandlers,
 348		initialWindowSize:     initialWindowSize,
 349		nextID:                1,
 350		maxConcurrentStreams:  defaultMaxStreamsClient,
 351		streamQuota:           defaultMaxStreamsClient,
 352		streamsQuotaAvailable: make(chan struct{}, 1),
 353		keepaliveEnabled:      keepaliveEnabled,
 354		bufferPool:            opts.BufferPool,
 355		onClose:               onClose,
 356	}
 357	var czSecurity credentials.ChannelzSecurityValue
 358	if au, ok := authInfo.(credentials.ChannelzSecurityInfo); ok {
 359		czSecurity = au.GetSecurityValue()
 360	}
 361	t.channelz = channelz.RegisterSocket(
 362		&channelz.Socket{
 363			SocketType:       channelz.SocketTypeNormal,
 364			Parent:           opts.ChannelzParent,
 365			SocketMetrics:    channelz.SocketMetrics{},
 366			EphemeralMetrics: t.socketMetrics,
 367			LocalAddr:        t.localAddr,
 368			RemoteAddr:       t.remoteAddr,
 369			SocketOptions:    channelz.GetSocketOption(t.conn),
 370			Security:         czSecurity,
 371		})
 372	t.logger = prefixLoggerForClientTransport(t)
 373	// Add peer information to the http2client context.
 374	t.ctx = peer.NewContext(t.ctx, t.getPeer())
 375
 376	if md, ok := addr.Metadata.(*metadata.MD); ok {
 377		t.md = *md
 378	} else if md := imetadata.Get(addr); md != nil {
 379		t.md = md
 380	}
 381	t.controlBuf = newControlBuffer(t.ctxDone)
 382	if opts.InitialWindowSize >= defaultWindowSize {
 383		t.initialWindowSize = opts.InitialWindowSize
 384		dynamicWindow = false
 385	}
 386	if dynamicWindow {
 387		t.bdpEst = &bdpEstimator{
 388			bdp:               initialWindowSize,
 389			updateFlowControl: t.updateFlowControl,
 390		}
 391	}
 392	for _, sh := range t.statsHandlers {
 393		t.ctx = sh.TagConn(t.ctx, &stats.ConnTagInfo{
 394			RemoteAddr: t.remoteAddr,
 395			LocalAddr:  t.localAddr,
 396		})
 397		connBegin := &stats.ConnBegin{
 398			Client: true,
 399		}
 400		sh.HandleConn(t.ctx, connBegin)
 401	}
 402	if t.keepaliveEnabled {
 403		t.kpDormancyCond = sync.NewCond(&t.mu)
 404		go t.keepalive()
 405	}
 406
 407	// Start the reader goroutine for incoming messages. Each transport has a
 408	// dedicated goroutine which reads HTTP2 frames from the network. Then it
 409	// dispatches the frame to the corresponding stream entity.  When the
 410	// server preface is received, readerErrCh is closed.  If an error occurs
 411	// first, an error is pushed to the channel.  This must be checked before
 412	// returning from this function.
 413	readerErrCh := make(chan error, 1)
 414	go t.reader(readerErrCh)
 415	defer func() {
 416		if err != nil {
 417			// writerDone should be closed since the loopy goroutine
 418			// wouldn't have started in the case this function returns an error.
 419			close(t.writerDone)
 420			t.Close(err)
 421		}
 422	}()
 423
 424	// Send connection preface to server.
 425	n, err := t.conn.Write(clientPreface)
 426	if err != nil {
 427		err = connectionErrorf(true, err, "transport: failed to write client preface: %v", err)
 428		return nil, err
 429	}
 430	if n != len(clientPreface) {
 431		err = connectionErrorf(true, nil, "transport: preface mismatch, wrote %d bytes; want %d", n, len(clientPreface))
 432		return nil, err
 433	}
 434	var ss []http2.Setting
 435
 436	if t.initialWindowSize != defaultWindowSize {
 437		ss = append(ss, http2.Setting{
 438			ID:  http2.SettingInitialWindowSize,
 439			Val: uint32(t.initialWindowSize),
 440		})
 441	}
 442	if opts.MaxHeaderListSize != nil {
 443		ss = append(ss, http2.Setting{
 444			ID:  http2.SettingMaxHeaderListSize,
 445			Val: *opts.MaxHeaderListSize,
 446		})
 447	}
 448	err = t.framer.fr.WriteSettings(ss...)
 449	if err != nil {
 450		err = connectionErrorf(true, err, "transport: failed to write initial settings frame: %v", err)
 451		return nil, err
 452	}
 453	// Adjust the connection flow control window if needed.
 454	if delta := uint32(icwz - defaultWindowSize); delta > 0 {
 455		if err := t.framer.fr.WriteWindowUpdate(0, delta); err != nil {
 456			err = connectionErrorf(true, err, "transport: failed to write window update: %v", err)
 457			return nil, err
 458		}
 459	}
 460
 461	t.connectionID = atomic.AddUint64(&clientConnectionCounter, 1)
 462
 463	if err := t.framer.writer.Flush(); err != nil {
 464		return nil, err
 465	}
 466	// Block until the server preface is received successfully or an error occurs.
 467	if err = <-readerErrCh; err != nil {
 468		return nil, err
 469	}
 470	go func() {
 471		t.loopy = newLoopyWriter(clientSide, t.framer, t.controlBuf, t.bdpEst, t.conn, t.logger, t.outgoingGoAwayHandler, t.bufferPool)
 472		if err := t.loopy.run(); !isIOError(err) {
 473			// Immediately close the connection, as the loopy writer returns
 474			// when there are no more active streams and we were draining (the
 475			// server sent a GOAWAY).  For I/O errors, the reader will hit it
 476			// after draining any remaining incoming data.
 477			t.conn.Close()
 478		}
 479		close(t.writerDone)
 480	}()
 481	return t, nil
 482}
 483
 484func (t *http2Client) newStream(ctx context.Context, callHdr *CallHdr) *ClientStream {
 485	// TODO(zhaoq): Handle uint32 overflow of Stream.id.
 486	s := &ClientStream{
 487		Stream: &Stream{
 488			method:         callHdr.Method,
 489			sendCompress:   callHdr.SendCompress,
 490			buf:            newRecvBuffer(),
 491			contentSubtype: callHdr.ContentSubtype,
 492		},
 493		ct:         t,
 494		done:       make(chan struct{}),
 495		headerChan: make(chan struct{}),
 496		doneFunc:   callHdr.DoneFunc,
 497	}
 498	s.wq = newWriteQuota(defaultWriteQuota, s.done)
 499	s.requestRead = func(n int) {
 500		t.adjustWindow(s, uint32(n))
 501	}
 502	// The client side stream context should have exactly the same life cycle with the user provided context.
 503	// That means, s.ctx should be read-only. And s.ctx is done iff ctx is done.
 504	// So we use the original context here instead of creating a copy.
 505	s.ctx = ctx
 506	s.trReader = &transportReader{
 507		reader: &recvBufferReader{
 508			ctx:     s.ctx,
 509			ctxDone: s.ctx.Done(),
 510			recv:    s.buf,
 511			closeStream: func(err error) {
 512				s.Close(err)
 513			},
 514		},
 515		windowHandler: func(n int) {
 516			t.updateWindow(s, uint32(n))
 517		},
 518	}
 519	return s
 520}
 521
 522func (t *http2Client) getPeer() *peer.Peer {
 523	return &peer.Peer{
 524		Addr:      t.remoteAddr,
 525		AuthInfo:  t.authInfo, // Can be nil
 526		LocalAddr: t.localAddr,
 527	}
 528}
 529
 530// OutgoingGoAwayHandler writes a GOAWAY to the connection.  Always returns (false, err) as we want the GoAway
 531// to be the last frame loopy writes to the transport.
 532func (t *http2Client) outgoingGoAwayHandler(g *goAway) (bool, error) {
 533	t.mu.Lock()
 534	maxStreamID := t.nextID - 2
 535	t.mu.Unlock()
 536	if err := t.framer.fr.WriteGoAway(maxStreamID, http2.ErrCodeNo, g.debugData); err != nil {
 537		return false, err
 538	}
 539	return false, g.closeConn
 540}
 541
 542func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr) ([]hpack.HeaderField, error) {
 543	aud := t.createAudience(callHdr)
 544	ri := credentials.RequestInfo{
 545		Method:   callHdr.Method,
 546		AuthInfo: t.authInfo,
 547	}
 548	ctxWithRequestInfo := icredentials.NewRequestInfoContext(ctx, ri)
 549	authData, err := t.getTrAuthData(ctxWithRequestInfo, aud)
 550	if err != nil {
 551		return nil, err
 552	}
 553	callAuthData, err := t.getCallAuthData(ctxWithRequestInfo, aud, callHdr)
 554	if err != nil {
 555		return nil, err
 556	}
 557	// TODO(mmukhi): Benchmark if the performance gets better if count the metadata and other header fields
 558	// first and create a slice of that exact size.
 559	// Make the slice of certain predictable size to reduce allocations made by append.
 560	hfLen := 7 // :method, :scheme, :path, :authority, content-type, user-agent, te
 561	hfLen += len(authData) + len(callAuthData)
 562	headerFields := make([]hpack.HeaderField, 0, hfLen)
 563	headerFields = append(headerFields, hpack.HeaderField{Name: ":method", Value: "POST"})
 564	headerFields = append(headerFields, hpack.HeaderField{Name: ":scheme", Value: t.scheme})
 565	headerFields = append(headerFields, hpack.HeaderField{Name: ":path", Value: callHdr.Method})
 566	headerFields = append(headerFields, hpack.HeaderField{Name: ":authority", Value: callHdr.Host})
 567	headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: grpcutil.ContentType(callHdr.ContentSubtype)})
 568	headerFields = append(headerFields, hpack.HeaderField{Name: "user-agent", Value: t.userAgent})
 569	headerFields = append(headerFields, hpack.HeaderField{Name: "te", Value: "trailers"})
 570	if callHdr.PreviousAttempts > 0 {
 571		headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-previous-rpc-attempts", Value: strconv.Itoa(callHdr.PreviousAttempts)})
 572	}
 573
 574	registeredCompressors := t.registeredCompressors
 575	if callHdr.SendCompress != "" {
 576		headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-encoding", Value: callHdr.SendCompress})
 577		// Include the outgoing compressor name when compressor is not registered
 578		// via encoding.RegisterCompressor. This is possible when client uses
 579		// WithCompressor dial option.
 580		if !grpcutil.IsCompressorNameRegistered(callHdr.SendCompress) {
 581			if registeredCompressors != "" {
 582				registeredCompressors += ","
 583			}
 584			registeredCompressors += callHdr.SendCompress
 585		}
 586	}
 587
 588	if registeredCompressors != "" {
 589		headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-accept-encoding", Value: registeredCompressors})
 590	}
 591	if dl, ok := ctx.Deadline(); ok {
 592		// Send out timeout regardless its value. The server can detect timeout context by itself.
 593		// TODO(mmukhi): Perhaps this field should be updated when actually writing out to the wire.
 594		timeout := time.Until(dl)
 595		headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-timeout", Value: grpcutil.EncodeDuration(timeout)})
 596	}
 597	for k, v := range authData {
 598		headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
 599	}
 600	for k, v := range callAuthData {
 601		headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
 602	}
 603
 604	if md, added, ok := metadataFromOutgoingContextRaw(ctx); ok {
 605		var k string
 606		for k, vv := range md {
 607			// HTTP doesn't allow you to set pseudoheaders after non pseudoheaders were set.
 608			if isReservedHeader(k) {
 609				continue
 610			}
 611			for _, v := range vv {
 612				headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
 613			}
 614		}
 615		for _, vv := range added {
 616			for i, v := range vv {
 617				if i%2 == 0 {
 618					k = strings.ToLower(v)
 619					continue
 620				}
 621				// HTTP doesn't allow you to set pseudoheaders after non pseudoheaders were set.
 622				if isReservedHeader(k) {
 623					continue
 624				}
 625				headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
 626			}
 627		}
 628	}
 629	for k, vv := range t.md {
 630		if isReservedHeader(k) {
 631			continue
 632		}
 633		for _, v := range vv {
 634			headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
 635		}
 636	}
 637	return headerFields, nil
 638}
 639
 640func (t *http2Client) createAudience(callHdr *CallHdr) string {
 641	// Create an audience string only if needed.
 642	if len(t.perRPCCreds) == 0 && callHdr.Creds == nil {
 643		return ""
 644	}
 645	// Construct URI required to get auth request metadata.
 646	// Omit port if it is the default one.
 647	host := strings.TrimSuffix(callHdr.Host, ":443")
 648	pos := strings.LastIndex(callHdr.Method, "/")
 649	if pos == -1 {
 650		pos = len(callHdr.Method)
 651	}
 652	return "https://" + host + callHdr.Method[:pos]
 653}
 654
 655func (t *http2Client) getTrAuthData(ctx context.Context, audience string) (map[string]string, error) {
 656	if len(t.perRPCCreds) == 0 {
 657		return nil, nil
 658	}
 659	authData := map[string]string{}
 660	for _, c := range t.perRPCCreds {
 661		data, err := c.GetRequestMetadata(ctx, audience)
 662		if err != nil {
 663			if st, ok := status.FromError(err); ok {
 664				// Restrict the code to the list allowed by gRFC A54.
 665				if istatus.IsRestrictedControlPlaneCode(st) {
 666					err = status.Errorf(codes.Internal, "transport: received per-RPC creds error with illegal status: %v", err)
 667				}
 668				return nil, err
 669			}
 670
 671			return nil, status.Errorf(codes.Unauthenticated, "transport: per-RPC creds failed due to error: %v", err)
 672		}
 673		for k, v := range data {
 674			// Capital header names are illegal in HTTP/2.
 675			k = strings.ToLower(k)
 676			authData[k] = v
 677		}
 678	}
 679	return authData, nil
 680}
 681
 682func (t *http2Client) getCallAuthData(ctx context.Context, audience string, callHdr *CallHdr) (map[string]string, error) {
 683	var callAuthData map[string]string
 684	// Check if credentials.PerRPCCredentials were provided via call options.
 685	// Note: if these credentials are provided both via dial options and call
 686	// options, then both sets of credentials will be applied.
 687	if callCreds := callHdr.Creds; callCreds != nil {
 688		if callCreds.RequireTransportSecurity() {
 689			ri, _ := credentials.RequestInfoFromContext(ctx)
 690			if !t.isSecure || credentials.CheckSecurityLevel(ri.AuthInfo, credentials.PrivacyAndIntegrity) != nil {
 691				return nil, status.Error(codes.Unauthenticated, "transport: cannot send secure credentials on an insecure connection")
 692			}
 693		}
 694		data, err := callCreds.GetRequestMetadata(ctx, audience)
 695		if err != nil {
 696			if st, ok := status.FromError(err); ok {
 697				// Restrict the code to the list allowed by gRFC A54.
 698				if istatus.IsRestrictedControlPlaneCode(st) {
 699					err = status.Errorf(codes.Internal, "transport: received per-RPC creds error with illegal status: %v", err)
 700				}
 701				return nil, err
 702			}
 703			return nil, status.Errorf(codes.Internal, "transport: per-RPC creds failed due to error: %v", err)
 704		}
 705		callAuthData = make(map[string]string, len(data))
 706		for k, v := range data {
 707			// Capital header names are illegal in HTTP/2
 708			k = strings.ToLower(k)
 709			callAuthData[k] = v
 710		}
 711	}
 712	return callAuthData, nil
 713}
 714
 715// NewStreamError wraps an error and reports additional information.  Typically
 716// NewStream errors result in transparent retry, as they mean nothing went onto
 717// the wire.  However, there are two notable exceptions:
 718//
 719//  1. If the stream headers violate the max header list size allowed by the
 720//     server.  It's possible this could succeed on another transport, even if
 721//     it's unlikely, but do not transparently retry.
 722//  2. If the credentials errored when requesting their headers.  In this case,
 723//     it's possible a retry can fix the problem, but indefinitely transparently
 724//     retrying is not appropriate as it is likely the credentials, if they can
 725//     eventually succeed, would need I/O to do so.
 726type NewStreamError struct {
 727	Err error
 728
 729	AllowTransparentRetry bool
 730}
 731
 732func (e NewStreamError) Error() string {
 733	return e.Err.Error()
 734}
 735
 736// NewStream creates a stream and registers it into the transport as "active"
 737// streams.  All non-nil errors returned will be *NewStreamError.
 738func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (*ClientStream, error) {
 739	ctx = peer.NewContext(ctx, t.getPeer())
 740
 741	// ServerName field of the resolver returned address takes precedence over
 742	// Host field of CallHdr to determine the :authority header. This is because,
 743	// the ServerName field takes precedence for server authentication during
 744	// TLS handshake, and the :authority header should match the value used
 745	// for server authentication.
 746	if t.address.ServerName != "" {
 747		newCallHdr := *callHdr
 748		newCallHdr.Host = t.address.ServerName
 749		callHdr = &newCallHdr
 750	}
 751
 752	headerFields, err := t.createHeaderFields(ctx, callHdr)
 753	if err != nil {
 754		return nil, &NewStreamError{Err: err, AllowTransparentRetry: false}
 755	}
 756	s := t.newStream(ctx, callHdr)
 757	cleanup := func(err error) {
 758		if s.swapState(streamDone) == streamDone {
 759			// If it was already done, return.
 760			return
 761		}
 762		// The stream was unprocessed by the server.
 763		s.unprocessed.Store(true)
 764		s.write(recvMsg{err: err})
 765		close(s.done)
 766		// If headerChan isn't closed, then close it.
 767		if atomic.CompareAndSwapUint32(&s.headerChanClosed, 0, 1) {
 768			close(s.headerChan)
 769		}
 770	}
 771	hdr := &headerFrame{
 772		hf:        headerFields,
 773		endStream: false,
 774		initStream: func(uint32) error {
 775			t.mu.Lock()
 776			// TODO: handle transport closure in loopy instead and remove this
 777			// initStream is never called when transport is draining.
 778			if t.state == closing {
 779				t.mu.Unlock()
 780				cleanup(ErrConnClosing)
 781				return ErrConnClosing
 782			}
 783			if channelz.IsOn() {
 784				t.channelz.SocketMetrics.StreamsStarted.Add(1)
 785				t.channelz.SocketMetrics.LastLocalStreamCreatedTimestamp.Store(time.Now().UnixNano())
 786			}
 787			// If the keepalive goroutine has gone dormant, wake it up.
 788			if t.kpDormant {
 789				t.kpDormancyCond.Signal()
 790			}
 791			t.mu.Unlock()
 792			return nil
 793		},
 794		onOrphaned: cleanup,
 795		wq:         s.wq,
 796	}
 797	firstTry := true
 798	var ch chan struct{}
 799	transportDrainRequired := false
 800	checkForStreamQuota := func() bool {
 801		if t.streamQuota <= 0 { // Can go negative if server decreases it.
 802			if firstTry {
 803				t.waitingStreams++
 804			}
 805			ch = t.streamsQuotaAvailable
 806			return false
 807		}
 808		if !firstTry {
 809			t.waitingStreams--
 810		}
 811		t.streamQuota--
 812
 813		t.mu.Lock()
 814		if t.state == draining || t.activeStreams == nil { // Can be niled from Close().
 815			t.mu.Unlock()
 816			return false // Don't create a stream if the transport is already closed.
 817		}
 818
 819		hdr.streamID = t.nextID
 820		t.nextID += 2
 821		// Drain client transport if nextID > MaxStreamID which signals gRPC that
 822		// the connection is closed and a new one must be created for subsequent RPCs.
 823		transportDrainRequired = t.nextID > MaxStreamID
 824
 825		s.id = hdr.streamID
 826		s.fc = &inFlow{limit: uint32(t.initialWindowSize)}
 827		t.activeStreams[s.id] = s
 828		t.mu.Unlock()
 829
 830		if t.streamQuota > 0 && t.waitingStreams > 0 {
 831			select {
 832			case t.streamsQuotaAvailable <- struct{}{}:
 833			default:
 834			}
 835		}
 836		return true
 837	}
 838	var hdrListSizeErr error
 839	checkForHeaderListSize := func() bool {
 840		if t.maxSendHeaderListSize == nil {
 841			return true
 842		}
 843		var sz int64
 844		for _, f := range hdr.hf {
 845			if sz += int64(f.Size()); sz > int64(*t.maxSendHeaderListSize) {
 846				hdrListSizeErr = status.Errorf(codes.Internal, "header list size to send violates the maximum size (%d bytes) set by server", *t.maxSendHeaderListSize)
 847				return false
 848			}
 849		}
 850		return true
 851	}
 852	for {
 853		success, err := t.controlBuf.executeAndPut(func() bool {
 854			return checkForHeaderListSize() && checkForStreamQuota()
 855		}, hdr)
 856		if err != nil {
 857			// Connection closed.
 858			return nil, &NewStreamError{Err: err, AllowTransparentRetry: true}
 859		}
 860		if success {
 861			break
 862		}
 863		if hdrListSizeErr != nil {
 864			return nil, &NewStreamError{Err: hdrListSizeErr}
 865		}
 866		firstTry = false
 867		select {
 868		case <-ch:
 869		case <-ctx.Done():
 870			return nil, &NewStreamError{Err: ContextErr(ctx.Err())}
 871		case <-t.goAway:
 872			return nil, &NewStreamError{Err: errStreamDrain, AllowTransparentRetry: true}
 873		case <-t.ctx.Done():
 874			return nil, &NewStreamError{Err: ErrConnClosing, AllowTransparentRetry: true}
 875		}
 876	}
 877	if len(t.statsHandlers) != 0 {
 878		header, ok := metadata.FromOutgoingContext(ctx)
 879		if ok {
 880			header.Set("user-agent", t.userAgent)
 881		} else {
 882			header = metadata.Pairs("user-agent", t.userAgent)
 883		}
 884		for _, sh := range t.statsHandlers {
 885			// Note: The header fields are compressed with hpack after this call returns.
 886			// No WireLength field is set here.
 887			// Note: Creating a new stats object to prevent pollution.
 888			outHeader := &stats.OutHeader{
 889				Client:      true,
 890				FullMethod:  callHdr.Method,
 891				RemoteAddr:  t.remoteAddr,
 892				LocalAddr:   t.localAddr,
 893				Compression: callHdr.SendCompress,
 894				Header:      header,
 895			}
 896			sh.HandleRPC(s.ctx, outHeader)
 897		}
 898	}
 899	if transportDrainRequired {
 900		if t.logger.V(logLevel) {
 901			t.logger.Infof("Draining transport: t.nextID > MaxStreamID")
 902		}
 903		t.GracefulClose()
 904	}
 905	return s, nil
 906}
 907
 908func (t *http2Client) closeStream(s *ClientStream, err error, rst bool, rstCode http2.ErrCode, st *status.Status, mdata map[string][]string, eosReceived bool) {
 909	// Set stream status to done.
 910	if s.swapState(streamDone) == streamDone {
 911		// If it was already done, return.  If multiple closeStream calls
 912		// happen simultaneously, wait for the first to finish.
 913		<-s.done
 914		return
 915	}
 916	// status and trailers can be updated here without any synchronization because the stream goroutine will
 917	// only read it after it sees an io.EOF error from read or write and we'll write those errors
 918	// only after updating this.
 919	s.status = st
 920	if len(mdata) > 0 {
 921		s.trailer = mdata
 922	}
 923	if err != nil {
 924		// This will unblock reads eventually.
 925		s.write(recvMsg{err: err})
 926	}
 927	// If headerChan isn't closed, then close it.
 928	if atomic.CompareAndSwapUint32(&s.headerChanClosed, 0, 1) {
 929		s.noHeaders = true
 930		close(s.headerChan)
 931	}
 932	cleanup := &cleanupStream{
 933		streamID: s.id,
 934		onWrite: func() {
 935			t.mu.Lock()
 936			if t.activeStreams != nil {
 937				delete(t.activeStreams, s.id)
 938			}
 939			t.mu.Unlock()
 940			if channelz.IsOn() {
 941				if eosReceived {
 942					t.channelz.SocketMetrics.StreamsSucceeded.Add(1)
 943				} else {
 944					t.channelz.SocketMetrics.StreamsFailed.Add(1)
 945				}
 946			}
 947		},
 948		rst:     rst,
 949		rstCode: rstCode,
 950	}
 951	addBackStreamQuota := func() bool {
 952		t.streamQuota++
 953		if t.streamQuota > 0 && t.waitingStreams > 0 {
 954			select {
 955			case t.streamsQuotaAvailable <- struct{}{}:
 956			default:
 957			}
 958		}
 959		return true
 960	}
 961	t.controlBuf.executeAndPut(addBackStreamQuota, cleanup)
 962	// This will unblock write.
 963	close(s.done)
 964	if s.doneFunc != nil {
 965		s.doneFunc()
 966	}
 967}
 968
 969// Close kicks off the shutdown process of the transport. This should be called
 970// only once on a transport. Once it is called, the transport should not be
 971// accessed anymore.
 972func (t *http2Client) Close(err error) {
 973	t.conn.SetWriteDeadline(time.Now().Add(time.Second * 10))
 974	t.mu.Lock()
 975	// Make sure we only close once.
 976	if t.state == closing {
 977		t.mu.Unlock()
 978		return
 979	}
 980	if t.logger.V(logLevel) {
 981		t.logger.Infof("Closing: %v", err)
 982	}
 983	// Call t.onClose ASAP to prevent the client from attempting to create new
 984	// streams.
 985	if t.state != draining {
 986		t.onClose(GoAwayInvalid)
 987	}
 988	t.state = closing
 989	streams := t.activeStreams
 990	t.activeStreams = nil
 991	if t.kpDormant {
 992		// If the keepalive goroutine is blocked on this condition variable, we
 993		// should unblock it so that the goroutine eventually exits.
 994		t.kpDormancyCond.Signal()
 995	}
 996	// Append info about previous goaways if there were any, since this may be important
 997	// for understanding the root cause for this connection to be closed.
 998	goAwayDebugMessage := t.goAwayDebugMessage
 999	t.mu.Unlock()
1000
1001	// Per HTTP/2 spec, a GOAWAY frame must be sent before closing the
1002	// connection. See https://httpwg.org/specs/rfc7540.html#GOAWAY. It
1003	// also waits for loopyWriter to be closed with a timer to avoid the
1004	// long blocking in case the connection is blackholed, i.e. TCP is
1005	// just stuck.
1006	t.controlBuf.put(&goAway{code: http2.ErrCodeNo, debugData: []byte("client transport shutdown"), closeConn: err})
1007	timer := time.NewTimer(goAwayLoopyWriterTimeout)
1008	defer timer.Stop()
1009	select {
1010	case <-t.writerDone: // success
1011	case <-timer.C:
1012		t.logger.Infof("Failed to write a GOAWAY frame as part of connection close after %s. Giving up and closing the transport.", goAwayLoopyWriterTimeout)
1013	}
1014	t.cancel()
1015	t.conn.Close()
1016	// Waits for the reader and keepalive goroutines to exit before returning to
1017	// ensure all resources are cleaned up before Close can return.
1018	<-t.readerDone
1019	if t.keepaliveEnabled {
1020		<-t.keepaliveDone
1021	}
1022	channelz.RemoveEntry(t.channelz.ID)
1023	var st *status.Status
1024	if len(goAwayDebugMessage) > 0 {
1025		st = status.Newf(codes.Unavailable, "closing transport due to: %v, received prior goaway: %v", err, goAwayDebugMessage)
1026		err = st.Err()
1027	} else {
1028		st = status.New(codes.Unavailable, err.Error())
1029	}
1030
1031	// Notify all active streams.
1032	for _, s := range streams {
1033		t.closeStream(s, err, false, http2.ErrCodeNo, st, nil, false)
1034	}
1035	for _, sh := range t.statsHandlers {
1036		connEnd := &stats.ConnEnd{
1037			Client: true,
1038		}
1039		sh.HandleConn(t.ctx, connEnd)
1040	}
1041}
1042
1043// GracefulClose sets the state to draining, which prevents new streams from
1044// being created and causes the transport to be closed when the last active
1045// stream is closed.  If there are no active streams, the transport is closed
1046// immediately.  This does nothing if the transport is already draining or
1047// closing.
1048func (t *http2Client) GracefulClose() {
1049	t.mu.Lock()
1050	// Make sure we move to draining only from active.
1051	if t.state == draining || t.state == closing {
1052		t.mu.Unlock()
1053		return
1054	}
1055	if t.logger.V(logLevel) {
1056		t.logger.Infof("GracefulClose called")
1057	}
1058	t.onClose(GoAwayInvalid)
1059	t.state = draining
1060	active := len(t.activeStreams)
1061	t.mu.Unlock()
1062	if active == 0 {
1063		t.Close(connectionErrorf(true, nil, "no active streams left to process while draining"))
1064		return
1065	}
1066	t.controlBuf.put(&incomingGoAway{})
1067}
1068
1069// Write formats the data into HTTP2 data frame(s) and sends it out. The caller
1070// should proceed only if Write returns nil.
1071func (t *http2Client) write(s *ClientStream, hdr []byte, data mem.BufferSlice, opts *WriteOptions) error {
1072	reader := data.Reader()
1073
1074	if opts.Last {
1075		// If it's the last message, update stream state.
1076		if !s.compareAndSwapState(streamActive, streamWriteDone) {
1077			_ = reader.Close()
1078			return errStreamDone
1079		}
1080	} else if s.getState() != streamActive {
1081		_ = reader.Close()
1082		return errStreamDone
1083	}
1084	df := &dataFrame{
1085		streamID:  s.id,
1086		endStream: opts.Last,
1087		h:         hdr,
1088		reader:    reader,
1089	}
1090	if hdr != nil || df.reader.Remaining() != 0 { // If it's not an empty data frame, check quota.
1091		if err := s.wq.get(int32(len(hdr) + df.reader.Remaining())); err != nil {
1092			_ = reader.Close()
1093			return err
1094		}
1095	}
1096	if err := t.controlBuf.put(df); err != nil {
1097		_ = reader.Close()
1098		return err
1099	}
1100	t.incrMsgSent()
1101	return nil
1102}
1103
1104func (t *http2Client) getStream(f http2.Frame) *ClientStream {
1105	t.mu.Lock()
1106	s := t.activeStreams[f.Header().StreamID]
1107	t.mu.Unlock()
1108	return s
1109}
1110
1111// adjustWindow sends out extra window update over the initial window size
1112// of stream if the application is requesting data larger in size than
1113// the window.
1114func (t *http2Client) adjustWindow(s *ClientStream, n uint32) {
1115	if w := s.fc.maybeAdjust(n); w > 0 {
1116		t.controlBuf.put(&outgoingWindowUpdate{streamID: s.id, increment: w})
1117	}
1118}
1119
1120// updateWindow adjusts the inbound quota for the stream.
1121// Window updates will be sent out when the cumulative quota
1122// exceeds the corresponding threshold.
1123func (t *http2Client) updateWindow(s *ClientStream, n uint32) {
1124	if w := s.fc.onRead(n); w > 0 {
1125		t.controlBuf.put(&outgoingWindowUpdate{streamID: s.id, increment: w})
1126	}
1127}
1128
1129// updateFlowControl updates the incoming flow control windows
1130// for the transport and the stream based on the current bdp
1131// estimation.
1132func (t *http2Client) updateFlowControl(n uint32) {
1133	updateIWS := func() bool {
1134		t.initialWindowSize = int32(n)
1135		t.mu.Lock()
1136		for _, s := range t.activeStreams {
1137			s.fc.newLimit(n)
1138		}
1139		t.mu.Unlock()
1140		return true
1141	}
1142	t.controlBuf.executeAndPut(updateIWS, &outgoingWindowUpdate{streamID: 0, increment: t.fc.newLimit(n)})
1143	t.controlBuf.put(&outgoingSettings{
1144		ss: []http2.Setting{
1145			{
1146				ID:  http2.SettingInitialWindowSize,
1147				Val: n,
1148			},
1149		},
1150	})
1151}
1152
1153func (t *http2Client) handleData(f *http2.DataFrame) {
1154	size := f.Header().Length
1155	var sendBDPPing bool
1156	if t.bdpEst != nil {
1157		sendBDPPing = t.bdpEst.add(size)
1158	}
1159	// Decouple connection's flow control from application's read.
1160	// An update on connection's flow control should not depend on
1161	// whether user application has read the data or not. Such a
1162	// restriction is already imposed on the stream's flow control,
1163	// and therefore the sender will be blocked anyways.
1164	// Decoupling the connection flow control will prevent other
1165	// active(fast) streams from starving in presence of slow or
1166	// inactive streams.
1167	//
1168	if w := t.fc.onData(size); w > 0 {
1169		t.controlBuf.put(&outgoingWindowUpdate{
1170			streamID:  0,
1171			increment: w,
1172		})
1173	}
1174	if sendBDPPing {
1175		// Avoid excessive ping detection (e.g. in an L7 proxy)
1176		// by sending a window update prior to the BDP ping.
1177
1178		if w := t.fc.reset(); w > 0 {
1179			t.controlBuf.put(&outgoingWindowUpdate{
1180				streamID:  0,
1181				increment: w,
1182			})
1183		}
1184
1185		t.controlBuf.put(bdpPing)
1186	}
1187	// Select the right stream to dispatch.
1188	s := t.getStream(f)
1189	if s == nil {
1190		return
1191	}
1192	if size > 0 {
1193		if err := s.fc.onData(size); err != nil {
1194			t.closeStream(s, io.EOF, true, http2.ErrCodeFlowControl, status.New(codes.Internal, err.Error()), nil, false)
1195			return
1196		}
1197		if f.Header().Flags.Has(http2.FlagDataPadded) {
1198			if w := s.fc.onRead(size - uint32(len(f.Data()))); w > 0 {
1199				t.controlBuf.put(&outgoingWindowUpdate{s.id, w})
1200			}
1201		}
1202		// TODO(bradfitz, zhaoq): A copy is required here because there is no
1203		// guarantee f.Data() is consumed before the arrival of next frame.
1204		// Can this copy be eliminated?
1205		if len(f.Data()) > 0 {
1206			pool := t.bufferPool
1207			if pool == nil {
1208				// Note that this is only supposed to be nil in tests. Otherwise, stream is
1209				// always initialized with a BufferPool.
1210				pool = mem.DefaultBufferPool()
1211			}
1212			s.write(recvMsg{buffer: mem.Copy(f.Data(), pool)})
1213		}
1214	}
1215	// The server has closed the stream without sending trailers.  Record that
1216	// the read direction is closed, and set the status appropriately.
1217	if f.StreamEnded() {
1218		t.closeStream(s, io.EOF, false, http2.ErrCodeNo, status.New(codes.Internal, "server closed the stream without sending trailers"), nil, true)
1219	}
1220}
1221
1222func (t *http2Client) handleRSTStream(f *http2.RSTStreamFrame) {
1223	s := t.getStream(f)
1224	if s == nil {
1225		return
1226	}
1227	if f.ErrCode == http2.ErrCodeRefusedStream {
1228		// The stream was unprocessed by the server.
1229		s.unprocessed.Store(true)
1230	}
1231	statusCode, ok := http2ErrConvTab[f.ErrCode]
1232	if !ok {
1233		if t.logger.V(logLevel) {
1234			t.logger.Infof("Received a RST_STREAM frame with code %q, but found no mapped gRPC status", f.ErrCode)
1235		}
1236		statusCode = codes.Unknown
1237	}
1238	if statusCode == codes.Canceled {
1239		if d, ok := s.ctx.Deadline(); ok && !d.After(time.Now()) {
1240			// Our deadline was already exceeded, and that was likely the cause
1241			// of this cancellation.  Alter the status code accordingly.
1242			statusCode = codes.DeadlineExceeded
1243		}
1244	}
1245	t.closeStream(s, io.EOF, false, http2.ErrCodeNo, status.Newf(statusCode, "stream terminated by RST_STREAM with error code: %v", f.ErrCode), nil, false)
1246}
1247
1248func (t *http2Client) handleSettings(f *http2.SettingsFrame, isFirst bool) {
1249	if f.IsAck() {
1250		return
1251	}
1252	var maxStreams *uint32
1253	var ss []http2.Setting
1254	var updateFuncs []func()
1255	f.ForeachSetting(func(s http2.Setting) error {
1256		switch s.ID {
1257		case http2.SettingMaxConcurrentStreams:
1258			maxStreams = new(uint32)
1259			*maxStreams = s.Val
1260		case http2.SettingMaxHeaderListSize:
1261			updateFuncs = append(updateFuncs, func() {
1262				t.maxSendHeaderListSize = new(uint32)
1263				*t.maxSendHeaderListSize = s.Val
1264			})
1265		default:
1266			ss = append(ss, s)
1267		}
1268		return nil
1269	})
1270	if isFirst && maxStreams == nil {
1271		maxStreams = new(uint32)
1272		*maxStreams = math.MaxUint32
1273	}
1274	sf := &incomingSettings{
1275		ss: ss,
1276	}
1277	if maxStreams != nil {
1278		updateStreamQuota := func() {
1279			delta := int64(*maxStreams) - int64(t.maxConcurrentStreams)
1280			t.maxConcurrentStreams = *maxStreams
1281			t.streamQuota += delta
1282			if delta > 0 && t.waitingStreams > 0 {
1283				close(t.streamsQuotaAvailable) // wake all of them up.
1284				t.streamsQuotaAvailable = make(chan struct{}, 1)
1285			}
1286		}
1287		updateFuncs = append(updateFuncs, updateStreamQuota)
1288	}
1289	t.controlBuf.executeAndPut(func() bool {
1290		for _, f := range updateFuncs {
1291			f()
1292		}
1293		return true
1294	}, sf)
1295}
1296
1297func (t *http2Client) handlePing(f *http2.PingFrame) {
1298	if f.IsAck() {
1299		// Maybe it's a BDP ping.
1300		if t.bdpEst != nil {
1301			t.bdpEst.calculate(f.Data)
1302		}
1303		return
1304	}
1305	pingAck := &ping{ack: true}
1306	copy(pingAck.data[:], f.Data[:])
1307	t.controlBuf.put(pingAck)
1308}
1309
1310func (t *http2Client) handleGoAway(f *http2.GoAwayFrame) error {
1311	t.mu.Lock()
1312	if t.state == closing {
1313		t.mu.Unlock()
1314		return nil
1315	}
1316	if f.ErrCode == http2.ErrCodeEnhanceYourCalm && string(f.DebugData()) == "too_many_pings" {
1317		// When a client receives a GOAWAY with error code ENHANCE_YOUR_CALM and debug
1318		// data equal to ASCII "too_many_pings", it should log the occurrence at a log level that is
1319		// enabled by default and double the configure KEEPALIVE_TIME used for new connections
1320		// on that channel.
1321		logger.Errorf("Client received GoAway with error code ENHANCE_YOUR_CALM and debug data equal to ASCII \"too_many_pings\".")
1322	}
1323	id := f.LastStreamID
1324	if id > 0 && id%2 == 0 {
1325		t.mu.Unlock()
1326		return connectionErrorf(true, nil, "received goaway with non-zero even-numbered stream id: %v", id)
1327	}
1328	// A client can receive multiple GoAways from the server (see
1329	// https://github.com/grpc/grpc-go/issues/1387).  The idea is that the first
1330	// GoAway will be sent with an ID of MaxInt32 and the second GoAway will be
1331	// sent after an RTT delay with the ID of the last stream the server will
1332	// process.
1333	//
1334	// Therefore, when we get the first GoAway we don't necessarily close any
1335	// streams. While in case of second GoAway we close all streams created after
1336	// the GoAwayId. This way streams that were in-flight while the GoAway from
1337	// server was being sent don't get killed.
1338	select {
1339	case <-t.goAway: // t.goAway has been closed (i.e.,multiple GoAways).
1340		// If there are multiple GoAways the first one should always have an ID greater than the following ones.
1341		if id > t.prevGoAwayID {
1342			t.mu.Unlock()
1343			return connectionErrorf(true, nil, "received goaway with stream id: %v, which exceeds stream id of previous goaway: %v", id, t.prevGoAwayID)
1344		}
1345	default:
1346		t.setGoAwayReason(f)
1347		close(t.goAway)
1348		defer t.controlBuf.put(&incomingGoAway{}) // Defer as t.mu is currently held.
1349		// Notify the clientconn about the GOAWAY before we set the state to
1350		// draining, to allow the client to stop attempting to create streams
1351		// before disallowing new streams on this connection.
1352		if t.state != draining {
1353			t.onClose(t.goAwayReason)
1354			t.state = draining
1355		}
1356	}
1357	// All streams with IDs greater than the GoAwayId
1358	// and smaller than the previous GoAway ID should be killed.
1359	upperLimit := t.prevGoAwayID
1360	if upperLimit == 0 { // This is the first GoAway Frame.
1361		upperLimit = math.MaxUint32 // Kill all streams after the GoAway ID.
1362	}
1363
1364	t.prevGoAwayID = id
1365	if len(t.activeStreams) == 0 {
1366		t.mu.Unlock()
1367		return connectionErrorf(true, nil, "received goaway and there are no active streams")
1368	}
1369
1370	streamsToClose := make([]*ClientStream, 0)
1371	for streamID, stream := range t.activeStreams {
1372		if streamID > id && streamID <= upperLimit {
1373			// The stream was unprocessed by the server.
1374			stream.unprocessed.Store(true)
1375			streamsToClose = append(streamsToClose, stream)
1376		}
1377	}
1378	t.mu.Unlock()
1379	// Called outside t.mu because closeStream can take controlBuf's mu, which
1380	// could induce deadlock and is not allowed.
1381	for _, stream := range streamsToClose {
1382		t.closeStream(stream, errStreamDrain, false, http2.ErrCodeNo, statusGoAway, nil, false)
1383	}
1384	return nil
1385}
1386
1387// setGoAwayReason sets the value of t.goAwayReason based
1388// on the GoAway frame received.
1389// It expects a lock on transport's mutex to be held by
1390// the caller.
1391func (t *http2Client) setGoAwayReason(f *http2.GoAwayFrame) {
1392	t.goAwayReason = GoAwayNoReason
1393	switch f.ErrCode {
1394	case http2.ErrCodeEnhanceYourCalm:
1395		if string(f.DebugData()) == "too_many_pings" {
1396			t.goAwayReason = GoAwayTooManyPings
1397		}
1398	}
1399	if len(f.DebugData()) == 0 {
1400		t.goAwayDebugMessage = fmt.Sprintf("code: %s", f.ErrCode)
1401	} else {
1402		t.goAwayDebugMessage = fmt.Sprintf("code: %s, debug data: %q", f.ErrCode, string(f.DebugData()))
1403	}
1404}
1405
1406func (t *http2Client) GetGoAwayReason() (GoAwayReason, string) {
1407	t.mu.Lock()
1408	defer t.mu.Unlock()
1409	return t.goAwayReason, t.goAwayDebugMessage
1410}
1411
1412func (t *http2Client) handleWindowUpdate(f *http2.WindowUpdateFrame) {
1413	t.controlBuf.put(&incomingWindowUpdate{
1414		streamID:  f.Header().StreamID,
1415		increment: f.Increment,
1416	})
1417}
1418
1419// operateHeaders takes action on the decoded headers.
1420func (t *http2Client) operateHeaders(frame *http2.MetaHeadersFrame) {
1421	s := t.getStream(frame)
1422	if s == nil {
1423		return
1424	}
1425	endStream := frame.StreamEnded()
1426	s.bytesReceived.Store(true)
1427	initialHeader := atomic.LoadUint32(&s.headerChanClosed) == 0
1428
1429	if !initialHeader && !endStream {
1430		// As specified by gRPC over HTTP2, a HEADERS frame (and associated CONTINUATION frames) can only appear at the start or end of a stream. Therefore, second HEADERS frame must have EOS bit set.
1431		st := status.New(codes.Internal, "a HEADERS frame cannot appear in the middle of a stream")
1432		t.closeStream(s, st.Err(), true, http2.ErrCodeProtocol, st, nil, false)
1433		return
1434	}
1435
1436	// frame.Truncated is set to true when framer detects that the current header
1437	// list size hits MaxHeaderListSize limit.
1438	if frame.Truncated {
1439		se := status.New(codes.Internal, "peer header list size exceeded limit")
1440		t.closeStream(s, se.Err(), true, http2.ErrCodeFrameSize, se, nil, endStream)
1441		return
1442	}
1443
1444	var (
1445		// If a gRPC Response-Headers has already been received, then it means
1446		// that the peer is speaking gRPC and we are in gRPC mode.
1447		isGRPC         = !initialHeader
1448		mdata          = make(map[string][]string)
1449		contentTypeErr = "malformed header: missing HTTP content-type"
1450		grpcMessage    string
1451		recvCompress   string
1452		httpStatusCode *int
1453		httpStatusErr  string
1454		rawStatusCode  = codes.Unknown
1455		// headerError is set if an error is encountered while parsing the headers
1456		headerError string
1457	)
1458
1459	if initialHeader {
1460		httpStatusErr = "malformed header: missing HTTP status"
1461	}
1462
1463	for _, hf := range frame.Fields {
1464		switch hf.Name {
1465		case "content-type":
1466			if _, validContentType := grpcutil.ContentSubtype(hf.Value); !validContentType {
1467				contentTypeErr = fmt.Sprintf("transport: received unexpected content-type %q", hf.Value)
1468				break
1469			}
1470			contentTypeErr = ""
1471			mdata[hf.Name] = append(mdata[hf.Name], hf.Value)
1472			isGRPC = true
1473		case "grpc-encoding":
1474			recvCompress = hf.Value
1475		case "grpc-status":
1476			code, err := strconv.ParseInt(hf.Value, 10, 32)
1477			if err != nil {
1478				se := status.New(codes.Internal, fmt.Sprintf("transport: malformed grpc-status: %v", err))
1479				t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
1480				return
1481			}
1482			rawStatusCode = codes.Code(uint32(code))
1483		case "grpc-message":
1484			grpcMessage = decodeGrpcMessage(hf.Value)
1485		case ":status":
1486			if hf.Value == "200" {
1487				httpStatusErr = ""
1488				statusCode := 200
1489				httpStatusCode = &statusCode
1490				break
1491			}
1492
1493			c, err := strconv.ParseInt(hf.Value, 10, 32)
1494			if err != nil {
1495				se := status.New(codes.Internal, fmt.Sprintf("transport: malformed http-status: %v", err))
1496				t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
1497				return
1498			}
1499			statusCode := int(c)
1500			httpStatusCode = &statusCode
1501
1502			httpStatusErr = fmt.Sprintf(
1503				"unexpected HTTP status code received from server: %d (%s)",
1504				statusCode,
1505				http.StatusText(statusCode),
1506			)
1507		default:
1508			if isReservedHeader(hf.Name) && !isWhitelistedHeader(hf.Name) {
1509				break
1510			}
1511			v, err := decodeMetadataHeader(hf.Name, hf.Value)
1512			if err != nil {
1513				headerError = fmt.Sprintf("transport: malformed %s: %v", hf.Name, err)
1514				logger.Warningf("Failed to decode metadata header (%q, %q): %v", hf.Name, hf.Value, err)
1515				break
1516			}
1517			mdata[hf.Name] = append(mdata[hf.Name], v)
1518		}
1519	}
1520
1521	if !isGRPC || httpStatusErr != "" {
1522		var code = codes.Internal // when header does not include HTTP status, return INTERNAL
1523
1524		if httpStatusCode != nil {
1525			var ok bool
1526			code, ok = HTTPStatusConvTab[*httpStatusCode]
1527			if !ok {
1528				code = codes.Unknown
1529			}
1530		}
1531		var errs []string
1532		if httpStatusErr != "" {
1533			errs = append(errs, httpStatusErr)
1534		}
1535		if contentTypeErr != "" {
1536			errs = append(errs, contentTypeErr)
1537		}
1538		// Verify the HTTP response is a 200.
1539		se := status.New(code, strings.Join(errs, "; "))
1540		t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
1541		return
1542	}
1543
1544	if headerError != "" {
1545		se := status.New(codes.Internal, headerError)
1546		t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
1547		return
1548	}
1549
1550	// For headers, set them in s.header and close headerChan.  For trailers or
1551	// trailers-only, closeStream will set the trailers and close headerChan as
1552	// needed.
1553	if !endStream {
1554		// If headerChan hasn't been closed yet (expected, given we checked it
1555		// above, but something else could have potentially closed the whole
1556		// stream).
1557		if atomic.CompareAndSwapUint32(&s.headerChanClosed, 0, 1) {
1558			s.headerValid = true
1559			// These values can be set without any synchronization because
1560			// stream goroutine will read it only after seeing a closed
1561			// headerChan which we'll close after setting this.
1562			s.recvCompress = recvCompress
1563			if len(mdata) > 0 {
1564				s.header = mdata
1565			}
1566			close(s.headerChan)
1567		}
1568	}
1569
1570	for _, sh := range t.statsHandlers {
1571		if !endStream {
1572			inHeader := &stats.InHeader{
1573				Client:      true,
1574				WireLength:  int(frame.Header().Length),
1575				Header:      metadata.MD(mdata).Copy(),
1576				Compression: s.recvCompress,
1577			}
1578			sh.HandleRPC(s.ctx, inHeader)
1579		} else {
1580			inTrailer := &stats.InTrailer{
1581				Client:     true,
1582				WireLength: int(frame.Header().Length),
1583				Trailer:    metadata.MD(mdata).Copy(),
1584			}
1585			sh.HandleRPC(s.ctx, inTrailer)
1586		}
1587	}
1588
1589	if !endStream {
1590		return
1591	}
1592
1593	status := istatus.NewWithProto(rawStatusCode, grpcMessage, mdata[grpcStatusDetailsBinHeader])
1594
1595	// If client received END_STREAM from server while stream was still active,
1596	// send RST_STREAM.
1597	rstStream := s.getState() == streamActive
1598	t.closeStream(s, io.EOF, rstStream, http2.ErrCodeNo, status, mdata, true)
1599}
1600
1601// readServerPreface reads and handles the initial settings frame from the
1602// server.
1603func (t *http2Client) readServerPreface() error {
1604	frame, err := t.framer.fr.ReadFrame()
1605	if err != nil {
1606		return connectionErrorf(true, err, "error reading server preface: %v", err)
1607	}
1608	sf, ok := frame.(*http2.SettingsFrame)
1609	if !ok {
1610		return connectionErrorf(true, nil, "initial http2 frame from server is not a settings frame: %T", frame)
1611	}
1612	t.handleSettings(sf, true)
1613	return nil
1614}
1615
1616// reader verifies the server preface and reads all subsequent data from
1617// network connection.  If the server preface is not read successfully, an
1618// error is pushed to errCh; otherwise errCh is closed with no error.
1619func (t *http2Client) reader(errCh chan<- error) {
1620	var errClose error
1621	defer func() {
1622		close(t.readerDone)
1623		if errClose != nil {
1624			t.Close(errClose)
1625		}
1626	}()
1627
1628	if err := t.readServerPreface(); err != nil {
1629		errCh <- err
1630		return
1631	}
1632	close(errCh)
1633	if t.keepaliveEnabled {
1634		atomic.StoreInt64(&t.lastRead, time.Now().UnixNano())
1635	}
1636
1637	// loop to keep reading incoming messages on this transport.
1638	for {
1639		t.controlBuf.throttle()
1640		frame, err := t.framer.fr.ReadFrame()
1641		if t.keepaliveEnabled {
1642			atomic.StoreInt64(&t.lastRead, time.Now().UnixNano())
1643		}
1644		if err != nil {
1645			// Abort an active stream if the http2.Framer returns a
1646			// http2.StreamError. This can happen only if the server's response
1647			// is malformed http2.
1648			if se, ok := err.(http2.StreamError); ok {
1649				t.mu.Lock()
1650				s := t.activeStreams[se.StreamID]
1651				t.mu.Unlock()
1652				if s != nil {
1653					// use error detail to provide better err message
1654					code := http2ErrConvTab[se.Code]
1655					errorDetail := t.framer.fr.ErrorDetail()
1656					var msg string
1657					if errorDetail != nil {
1658						msg = errorDetail.Error()
1659					} else {
1660						msg = "received invalid frame"
1661					}
1662					t.closeStream(s, status.Error(code, msg), true, http2.ErrCodeProtocol, status.New(code, msg), nil, false)
1663				}
1664				continue
1665			}
1666			// Transport error.
1667			errClose = connectionErrorf(true, err, "error reading from server: %v", err)
1668			return
1669		}
1670		switch frame := frame.(type) {
1671		case *http2.MetaHeadersFrame:
1672			t.operateHeaders(frame)
1673		case *http2.DataFrame:
1674			t.handleData(frame)
1675		case *http2.RSTStreamFrame:
1676			t.handleRSTStream(frame)
1677		case *http2.SettingsFrame:
1678			t.handleSettings(frame, false)
1679		case *http2.PingFrame:
1680			t.handlePing(frame)
1681		case *http2.GoAwayFrame:
1682			errClose = t.handleGoAway(frame)
1683		case *http2.WindowUpdateFrame:
1684			t.handleWindowUpdate(frame)
1685		default:
1686			if logger.V(logLevel) {
1687				logger.Errorf("transport: http2Client.reader got unhandled frame type %v.", frame)
1688			}
1689		}
1690	}
1691}
1692
1693// keepalive running in a separate goroutine makes sure the connection is alive by sending pings.
1694func (t *http2Client) keepalive() {
1695	var err error
1696	defer func() {
1697		close(t.keepaliveDone)
1698		if err != nil {
1699			t.Close(err)
1700		}
1701	}()
1702	p := &ping{data: [8]byte{}}
1703	// True iff a ping has been sent, and no data has been received since then.
1704	outstandingPing := false
1705	// Amount of time remaining before which we should receive an ACK for the
1706	// last sent ping.
1707	timeoutLeft := time.Duration(0)
1708	// Records the last value of t.lastRead before we go block on the timer.
1709	// This is required to check for read activity since then.
1710	prevNano := time.Now().UnixNano()
1711	timer := time.NewTimer(t.kp.Time)
1712	for {
1713		select {
1714		case <-timer.C:
1715			lastRead := atomic.LoadInt64(&t.lastRead)
1716			if lastRead > prevNano {
1717				// There has been read activity since the last time we were here.
1718				outstandingPing = false
1719				// Next timer should fire at kp.Time seconds from lastRead time.
1720				timer.Reset(time.Duration(lastRead) + t.kp.Time - time.Duration(time.Now().UnixNano()))
1721				prevNano = lastRead
1722				continue
1723			}
1724			if outstandingPing && timeoutLeft <= 0 {
1725				err = connectionErrorf(true, nil, "keepalive ping failed to receive ACK within timeout")
1726				return
1727			}
1728			t.mu.Lock()
1729			if t.state == closing {
1730				// If the transport is closing, we should exit from the
1731				// keepalive goroutine here. If not, we could have a race
1732				// between the call to Signal() from Close() and the call to
1733				// Wait() here, whereby the keepalive goroutine ends up
1734				// blocking on the condition variable which will never be
1735				// signalled again.
1736				t.mu.Unlock()
1737				return
1738			}
1739			if len(t.activeStreams) < 1 && !t.kp.PermitWithoutStream {
1740				// If a ping was sent out previously (because there were active
1741				// streams at that point) which wasn't acked and its timeout
1742				// hadn't fired, but we got here and are about to go dormant,
1743				// we should make sure that we unconditionally send a ping once
1744				// we awaken.
1745				outstandingPing = false
1746				t.kpDormant = true
1747				t.kpDormancyCond.Wait()
1748			}
1749			t.kpDormant = false
1750			t.mu.Unlock()
1751
1752			// We get here either because we were dormant and a new stream was
1753			// created which unblocked the Wait() call, or because the
1754			// keepalive timer expired. In both cases, we need to send a ping.
1755			if !outstandingPing {
1756				if channelz.IsOn() {
1757					t.channelz.SocketMetrics.KeepAlivesSent.Add(1)
1758				}
1759				t.controlBuf.put(p)
1760				timeoutLeft = t.kp.Timeout
1761				outstandingPing = true
1762			}
1763			// The amount of time to sleep here is the minimum of kp.Time and
1764			// timeoutLeft. This will ensure that we wait only for kp.Time
1765			// before sending out the next ping (for cases where the ping is
1766			// acked).
1767			sleepDuration := min(t.kp.Time, timeoutLeft)
1768			timeoutLeft -= sleepDuration
1769			timer.Reset(sleepDuration)
1770		case <-t.ctx.Done():
1771			if !timer.Stop() {
1772				<-timer.C
1773			}
1774			return
1775		}
1776	}
1777}
1778
1779func (t *http2Client) Error() <-chan struct{} {
1780	return t.ctx.Done()
1781}
1782
1783func (t *http2Client) GoAway() <-chan struct{} {
1784	return t.goAway
1785}
1786
1787func (t *http2Client) socketMetrics() *channelz.EphemeralSocketMetrics {
1788	return &channelz.EphemeralSocketMetrics{
1789		LocalFlowControlWindow:  int64(t.fc.getSize()),
1790		RemoteFlowControlWindow: t.getOutFlowWindow(),
1791	}
1792}
1793
1794func (t *http2Client) RemoteAddr() net.Addr { return t.remoteAddr }
1795
1796func (t *http2Client) incrMsgSent() {
1797	if channelz.IsOn() {
1798		t.channelz.SocketMetrics.MessagesSent.Add(1)
1799		t.channelz.SocketMetrics.LastMessageSentTimestamp.Store(time.Now().UnixNano())
1800	}
1801}
1802
1803func (t *http2Client) incrMsgRecv() {
1804	if channelz.IsOn() {
1805		t.channelz.SocketMetrics.MessagesReceived.Add(1)
1806		t.channelz.SocketMetrics.LastMessageReceivedTimestamp.Store(time.Now().UnixNano())
1807	}
1808}
1809
1810func (t *http2Client) getOutFlowWindow() int64 {
1811	resp := make(chan uint32, 1)
1812	timer := time.NewTimer(time.Second)
1813	defer timer.Stop()
1814	t.controlBuf.put(&outFlowControlSizeRequest{resp})
1815	select {
1816	case sz := <-resp:
1817		return int64(sz)
1818	case <-t.ctxDone:
1819		return -1
1820	case <-timer.C:
1821		return -2
1822	}
1823}
1824
1825func (t *http2Client) stateForTesting() transportState {
1826	t.mu.Lock()
1827	defer t.mu.Unlock()
1828	return t.state
1829}