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.
34 "github.com/golang/protobuf/proto"
35 "golang.org/x/net/context"
36 "golang.org/x/net/http2"
37 "golang.org/x/net/http2/hpack"
38 "google.golang.org/grpc/codes"
39 "google.golang.org/grpc/credentials"
40 "google.golang.org/grpc/keepalive"
41 "google.golang.org/grpc/metadata"
42 "google.golang.org/grpc/peer"
43 "google.golang.org/grpc/stats"
44 "google.golang.org/grpc/status"
45 "google.golang.org/grpc/tap"
48 // ErrIllegalHeaderWrite indicates that setting header is illegal because of
49 // the stream's state.
50 var ErrIllegalHeaderWrite = errors.New("transport: the stream is done or WriteHeader was already called")
52 // http2Server implements the ServerTransport interface with HTTP2.
53 type http2Server struct {
55 cancel context.CancelFunc
59 maxStreamID uint32 // max stream ID ever seen
60 authInfo credentials.AuthInfo // auth info about the connection
61 inTapHandle tap.ServerInHandle
63 hBuf *bytes.Buffer // the buffer for HPACK encoding
64 hEnc *hpack.Encoder // HPACK encoder
65 // The max number of concurrent streams.
67 // controlBuf delivers all the control related tasks (e.g., window
68 // updates, reset streams, and various settings) to the controller.
69 controlBuf *controlBuffer
71 // sendQuotaPool provides flow control to outbound message.
72 sendQuotaPool *quotaPool
74 // Flag to keep track of reading activity on transport.
75 // 1 is true and 0 is false.
76 activity uint32 // Accessed atomically.
77 // Keepalive and max-age parameters for the server.
78 kp keepalive.ServerParameters
80 // Keepalive enforcement policy.
81 kep keepalive.EnforcementPolicy
82 // The time instance last ping was received.
84 // Number of times the client has violated keepalive ping policy so far.
86 // Flag to signify that number of ping strikes should be reset to 0.
87 // This is set whenever data or header frames are sent.
89 resetPingStrikes uint32 // Accessed atomically.
90 initialWindowSize int32
93 mu sync.Mutex // guard the following
95 // drainChan is initialized when drain(...) is called the first time.
96 // After which the server writes out the first GoAway(with ID 2^31-1) frame.
97 // Then an independent goroutine will be launched to later send the second GoAway.
98 // During this time we don't want to write another first GoAway(with ID 2^31 -1) frame.
99 // Thus call to drain(...) will be a no-op if drainChan is already initialized since draining is
101 drainChan chan struct{}
103 activeStreams map[uint32]*Stream
104 // the per-stream outbound flow control window size set by the peer.
105 streamSendQuota uint32
106 // idle is the time instant when the connection went idle.
107 // This is either the beginning of the connection or when the number of
108 // RPCs go down to 0.
109 // When the connection is busy, this value is set to 0.
113 // newHTTP2Server constructs a ServerTransport based on HTTP2. ConnectionError is
114 // returned if something goes wrong.
115 func newHTTP2Server(conn net.Conn, config *ServerConfig) (_ ServerTransport, err error) {
116 writeBufSize := defaultWriteBufSize
117 if config.WriteBufferSize > 0 {
118 writeBufSize = config.WriteBufferSize
120 readBufSize := defaultReadBufSize
121 if config.ReadBufferSize > 0 {
122 readBufSize = config.ReadBufferSize
124 framer := newFramer(conn, writeBufSize, readBufSize)
125 // Send initial settings as connection preface to client.
126 var isettings []http2.Setting
127 // TODO(zhaoq): Have a better way to signal "no limit" because 0 is
128 // permitted in the HTTP2 spec.
129 maxStreams := config.MaxStreams
131 maxStreams = math.MaxUint32
133 isettings = append(isettings, http2.Setting{
134 ID: http2.SettingMaxConcurrentStreams,
138 dynamicWindow := true
139 iwz := int32(initialWindowSize)
140 if config.InitialWindowSize >= defaultWindowSize {
141 iwz = config.InitialWindowSize
142 dynamicWindow = false
144 icwz := int32(initialWindowSize)
145 if config.InitialConnWindowSize >= defaultWindowSize {
146 icwz = config.InitialConnWindowSize
147 dynamicWindow = false
149 if iwz != defaultWindowSize {
150 isettings = append(isettings, http2.Setting{
151 ID: http2.SettingInitialWindowSize,
154 if err := framer.fr.WriteSettings(isettings...); err != nil {
155 return nil, connectionErrorf(true, err, "transport: %v", err)
157 // Adjust the connection flow control window if needed.
158 if delta := uint32(icwz - defaultWindowSize); delta > 0 {
159 if err := framer.fr.WriteWindowUpdate(0, delta); err != nil {
160 return nil, connectionErrorf(true, err, "transport: %v", err)
163 kp := config.KeepaliveParams
164 if kp.MaxConnectionIdle == 0 {
165 kp.MaxConnectionIdle = defaultMaxConnectionIdle
167 if kp.MaxConnectionAge == 0 {
168 kp.MaxConnectionAge = defaultMaxConnectionAge
170 // Add a jitter to MaxConnectionAge.
171 kp.MaxConnectionAge += getJitter(kp.MaxConnectionAge)
172 if kp.MaxConnectionAgeGrace == 0 {
173 kp.MaxConnectionAgeGrace = defaultMaxConnectionAgeGrace
176 kp.Time = defaultServerKeepaliveTime
179 kp.Timeout = defaultServerKeepaliveTimeout
181 kep := config.KeepalivePolicy
182 if kep.MinTime == 0 {
183 kep.MinTime = defaultKeepalivePolicyMinTime
186 ctx, cancel := context.WithCancel(context.Background())
191 remoteAddr: conn.RemoteAddr(),
192 localAddr: conn.LocalAddr(),
193 authInfo: config.AuthInfo,
196 hEnc: hpack.NewEncoder(&buf),
197 maxStreams: maxStreams,
198 inTapHandle: config.InTapHandle,
199 controlBuf: newControlBuffer(),
200 fc: &inFlow{limit: uint32(icwz)},
201 sendQuotaPool: newQuotaPool(defaultWindowSize),
203 activeStreams: make(map[uint32]*Stream),
204 streamSendQuota: defaultWindowSize,
205 stats: config.StatsHandler,
209 initialWindowSize: iwz,
212 t.bdpEst = &bdpEstimator{
213 bdp: initialWindowSize,
214 updateFlowControl: t.updateFlowControl,
218 t.ctx = t.stats.TagConn(t.ctx, &stats.ConnTagInfo{
219 RemoteAddr: t.remoteAddr,
220 LocalAddr: t.localAddr,
222 connBegin := &stats.ConnBegin{}
223 t.stats.HandleConn(t.ctx, connBegin)
225 t.framer.writer.Flush()
227 loopyWriter(t.ctx, t.controlBuf, t.itemHandler)
234 // operateHeader takes action on the decoded headers.
235 func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(*Stream), traceCtx func(context.Context, string) context.Context) (close bool) {
236 streamID := frame.Header().StreamID
238 var state decodeState
239 for _, hf := range frame.Fields {
240 if err := state.processHeaderField(hf); err != nil {
241 if se, ok := err.(StreamError); ok {
242 t.controlBuf.put(&resetStream{streamID, statusCodeConvTab[se.Code]})
248 buf := newRecvBuffer()
253 fc: &inFlow{limit: uint32(t.initialWindowSize)},
254 recvCompress: state.encoding,
255 method: state.method,
258 if frame.StreamEnded() {
259 // s is just created by the caller. No lock needed.
260 s.state = streamReadDone
262 if state.timeoutSet {
263 s.ctx, s.cancel = context.WithTimeout(t.ctx, state.timeout)
265 s.ctx, s.cancel = context.WithCancel(t.ctx)
270 // Attach Auth info if there is any.
271 if t.authInfo != nil {
272 pr.AuthInfo = t.authInfo
274 s.ctx = peer.NewContext(s.ctx, pr)
275 // Cache the current stream to the context so that the server application
276 // can find out. Required when the server wants to send some metadata
277 // back to the client (unary call only).
278 s.ctx = newContextWithStream(s.ctx, s)
279 // Attach the received metadata to the context.
280 if len(state.mdata) > 0 {
281 s.ctx = metadata.NewIncomingContext(s.ctx, state.mdata)
283 if state.statsTags != nil {
284 s.ctx = stats.SetIncomingTags(s.ctx, state.statsTags)
286 if state.statsTrace != nil {
287 s.ctx = stats.SetIncomingTrace(s.ctx, state.statsTrace)
289 if t.inTapHandle != nil {
292 FullMethodName: state.method,
294 s.ctx, err = t.inTapHandle(s.ctx, info)
296 warningf("transport: http2Server.operateHeaders got an error from InTapHandle: %v", err)
297 t.controlBuf.put(&resetStream{s.id, http2.ErrCodeRefusedStream})
302 if t.state != reachable {
306 if uint32(len(t.activeStreams)) >= t.maxStreams {
308 t.controlBuf.put(&resetStream{streamID, http2.ErrCodeRefusedStream})
311 if streamID%2 != 1 || streamID <= t.maxStreamID {
313 // illegal gRPC stream id.
314 errorf("transport: http2Server.HandleStreams received an illegal stream id: %v", streamID)
317 t.maxStreamID = streamID
318 s.sendQuotaPool = newQuotaPool(int(t.streamSendQuota))
319 s.localSendQuota = newQuotaPool(defaultLocalSendQuota)
320 t.activeStreams[streamID] = s
321 if len(t.activeStreams) == 1 {
325 s.requestRead = func(n int) {
326 t.adjustWindow(s, uint32(n))
328 s.ctx = traceCtx(s.ctx, s.method)
330 s.ctx = t.stats.TagRPC(s.ctx, &stats.RPCTagInfo{FullMethodName: s.method})
331 inHeader := &stats.InHeader{
332 FullMethod: s.method,
333 RemoteAddr: t.remoteAddr,
334 LocalAddr: t.localAddr,
335 Compression: s.recvCompress,
336 WireLength: int(frame.Header().Length),
338 t.stats.HandleRPC(s.ctx, inHeader)
340 s.trReader = &transportReader{
341 reader: &recvBufferReader{
345 windowHandler: func(n int) {
346 t.updateWindow(s, uint32(n))
353 // HandleStreams receives incoming streams using the given handler. This is
354 // typically run in a separate goroutine.
355 // traceCtx attaches trace to ctx and returns the new context.
356 func (t *http2Server) HandleStreams(handle func(*Stream), traceCtx func(context.Context, string) context.Context) {
357 // Check the validity of client preface.
358 preface := make([]byte, len(clientPreface))
359 if _, err := io.ReadFull(t.conn, preface); err != nil {
360 // Only log if it isn't a simple tcp accept check (ie: tcp balancer doing open/close socket)
362 errorf("transport: http2Server.HandleStreams failed to receive the preface from client: %v", err)
367 if !bytes.Equal(preface, clientPreface) {
368 errorf("transport: http2Server.HandleStreams received bogus greeting from client: %q", preface)
373 frame, err := t.framer.fr.ReadFrame()
374 if err == io.EOF || err == io.ErrUnexpectedEOF {
379 errorf("transport: http2Server.HandleStreams failed to read initial settings frame: %v", err)
383 atomic.StoreUint32(&t.activity, 1)
384 sf, ok := frame.(*http2.SettingsFrame)
386 errorf("transport: http2Server.HandleStreams saw invalid preface type %T from client", frame)
393 frame, err := t.framer.fr.ReadFrame()
394 atomic.StoreUint32(&t.activity, 1)
396 if se, ok := err.(http2.StreamError); ok {
398 s := t.activeStreams[se.StreamID]
403 t.controlBuf.put(&resetStream{se.StreamID, se.Code})
406 if err == io.EOF || err == io.ErrUnexpectedEOF {
410 warningf("transport: http2Server.HandleStreams failed to read frame: %v", err)
414 switch frame := frame.(type) {
415 case *http2.MetaHeadersFrame:
416 if t.operateHeaders(frame, handle, traceCtx) {
420 case *http2.DataFrame:
422 case *http2.RSTStreamFrame:
423 t.handleRSTStream(frame)
424 case *http2.SettingsFrame:
425 t.handleSettings(frame)
426 case *http2.PingFrame:
428 case *http2.WindowUpdateFrame:
429 t.handleWindowUpdate(frame)
430 case *http2.GoAwayFrame:
431 // TODO: Handle GoAway from the client appropriately.
433 errorf("transport: http2Server.HandleStreams found unhandled frame type %v.", frame)
438 func (t *http2Server) getStream(f http2.Frame) (*Stream, bool) {
441 if t.activeStreams == nil {
442 // The transport is closing.
445 s, ok := t.activeStreams[f.Header().StreamID]
447 // The stream is already done.
453 // adjustWindow sends out extra window update over the initial window size
454 // of stream if the application is requesting data larger in size than
456 func (t *http2Server) adjustWindow(s *Stream, n uint32) {
459 if s.state == streamDone {
462 if w := s.fc.maybeAdjust(n); w > 0 {
463 if cw := t.fc.resetPendingUpdate(); cw > 0 {
464 t.controlBuf.put(&windowUpdate{0, cw})
466 t.controlBuf.put(&windowUpdate{s.id, w})
470 // updateWindow adjusts the inbound quota for the stream and the transport.
471 // Window updates will deliver to the controller for sending when
472 // the cumulative quota exceeds the corresponding threshold.
473 func (t *http2Server) updateWindow(s *Stream, n uint32) {
476 if s.state == streamDone {
479 if w := s.fc.onRead(n); w > 0 {
480 if cw := t.fc.resetPendingUpdate(); cw > 0 {
481 t.controlBuf.put(&windowUpdate{0, cw})
483 t.controlBuf.put(&windowUpdate{s.id, w})
487 // updateFlowControl updates the incoming flow control windows
488 // for the transport and the stream based on the current bdp
490 func (t *http2Server) updateFlowControl(n uint32) {
492 for _, s := range t.activeStreams {
495 t.initialWindowSize = int32(n)
497 t.controlBuf.put(&windowUpdate{0, t.fc.newLimit(n)})
498 t.controlBuf.put(&settings{
502 ID: http2.SettingInitialWindowSize,
510 func (t *http2Server) handleData(f *http2.DataFrame) {
511 size := f.Header().Length
514 sendBDPPing = t.bdpEst.add(uint32(size))
516 // Decouple connection's flow control from application's read.
517 // An update on connection's flow control should not depend on
518 // whether user application has read the data or not. Such a
519 // restriction is already imposed on the stream's flow control,
520 // and therefore the sender will be blocked anyways.
521 // Decoupling the connection flow control will prevent other
522 // active(fast) streams from starving in presence of slow or
525 // Furthermore, if a bdpPing is being sent out we can piggyback
526 // connection's window update for the bytes we just received.
528 if size != 0 { // Could be an empty frame.
529 t.controlBuf.put(&windowUpdate{0, uint32(size)})
531 t.controlBuf.put(bdpPing)
533 if err := t.fc.onData(uint32(size)); err != nil {
534 errorf("transport: http2Server %v", err)
538 if w := t.fc.onRead(uint32(size)); w > 0 {
539 t.controlBuf.put(&windowUpdate{0, w})
542 // Select the right stream to dispatch.
543 s, ok := t.getStream(f)
549 if s.state == streamDone {
553 if err := s.fc.onData(uint32(size)); err != nil {
556 t.controlBuf.put(&resetStream{s.id, http2.ErrCodeFlowControl})
559 if f.Header().Flags.Has(http2.FlagDataPadded) {
560 if w := s.fc.onRead(uint32(size) - uint32(len(f.Data()))); w > 0 {
561 t.controlBuf.put(&windowUpdate{s.id, w})
565 // TODO(bradfitz, zhaoq): A copy is required here because there is no
566 // guarantee f.Data() is consumed before the arrival of next frame.
567 // Can this copy be eliminated?
568 if len(f.Data()) > 0 {
569 data := make([]byte, len(f.Data()))
571 s.write(recvMsg{data: data})
574 if f.Header().Flags.Has(http2.FlagDataEndStream) {
575 // Received the end of stream from the client.
577 if s.state != streamDone {
578 s.state = streamReadDone
581 s.write(recvMsg{err: io.EOF})
585 func (t *http2Server) handleRSTStream(f *http2.RSTStreamFrame) {
586 s, ok := t.getStream(f)
593 func (t *http2Server) handleSettings(f *http2.SettingsFrame) {
597 var ss []http2.Setting
598 f.ForeachSetting(func(s http2.Setting) error {
602 t.controlBuf.put(&settings{ack: true, ss: ss})
605 func (t *http2Server) applySettings(ss []http2.Setting) {
606 for _, s := range ss {
607 if s.ID == http2.SettingInitialWindowSize {
609 for _, stream := range t.activeStreams {
610 stream.sendQuotaPool.addAndUpdate(int(s.Val) - int(t.streamSendQuota))
612 t.streamSendQuota = s.Val
621 defaultPingTimeout = 2 * time.Hour
624 func (t *http2Server) handlePing(f *http2.PingFrame) {
626 if f.Data == goAwayPing.data && t.drainChan != nil {
630 // Maybe it's a BDP ping.
632 t.bdpEst.calculate(f.Data)
636 pingAck := &ping{ack: true}
637 copy(pingAck.data[:], f.Data[:])
638 t.controlBuf.put(pingAck)
644 // A reset ping strikes means that we don't need to check for policy
645 // violation for this ping and the pingStrikes counter should be set
647 if atomic.CompareAndSwapUint32(&t.resetPingStrikes, 1, 0) {
652 ns := len(t.activeStreams)
654 if ns < 1 && !t.kep.PermitWithoutStream {
655 // Keepalive shouldn't be active thus, this new ping should
656 // have come after at least defaultPingTimeout.
657 if t.lastPingAt.Add(defaultPingTimeout).After(now) {
661 // Check if keepalive policy is respected.
662 if t.lastPingAt.Add(t.kep.MinTime).After(now) {
667 if t.pingStrikes > maxPingStrikes {
668 // Send goaway and close the connection.
669 errorf("transport: Got to too many pings from the client, closing the connection.")
670 t.controlBuf.put(&goAway{code: http2.ErrCodeEnhanceYourCalm, debugData: []byte("too_many_pings"), closeConn: true})
674 func (t *http2Server) handleWindowUpdate(f *http2.WindowUpdateFrame) {
675 id := f.Header().StreamID
678 t.sendQuotaPool.add(int(incr))
681 if s, ok := t.getStream(f); ok {
682 s.sendQuotaPool.add(int(incr))
686 // WriteHeader sends the header metedata md back to the client.
687 func (t *http2Server) WriteHeader(s *Stream, md metadata.MD) error {
690 return ContextErr(s.ctx.Err())
692 return ErrConnClosing
697 if s.headerOk || s.state == streamDone {
699 return ErrIllegalHeaderWrite
703 if s.header.Len() > 0 {
704 s.header = metadata.Join(s.header, md)
711 // TODO(mmukhi): Benchmark if the performance gets better if count the metadata and other header fields
712 // first and create a slice of that exact size.
713 headerFields := make([]hpack.HeaderField, 0, 2) // at least :status, content-type will be there if none else.
714 headerFields = append(headerFields, hpack.HeaderField{Name: ":status", Value: "200"})
715 headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: "application/grpc"})
716 if s.sendCompress != "" {
717 headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-encoding", Value: s.sendCompress})
719 for k, vv := range md {
720 if isReservedHeader(k) {
721 // Clients don't tolerate reading restricted headers after some non restricted ones were sent.
724 for _, v := range vv {
725 headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
728 t.controlBuf.put(&headerFrame{
734 outHeader := &stats.OutHeader{
735 //WireLength: // TODO(mmukhi): Revisit this later, if needed.
737 t.stats.HandleRPC(s.Context(), outHeader)
742 // WriteStatus sends stream status to the client and terminates the stream.
743 // There is no further I/O operations being able to perform on this stream.
744 // TODO(zhaoq): Now it indicates the end of entire stream. Revisit if early
746 func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error {
749 return ErrConnClosing
753 var headersSent, hasHeader bool
755 if s.state == streamDone {
762 if s.header.Len() > 0 {
767 if !headersSent && hasHeader {
768 t.WriteHeader(s, nil)
772 // TODO(mmukhi): Benchmark if the performance gets better if count the metadata and other header fields
773 // first and create a slice of that exact size.
774 headerFields := make([]hpack.HeaderField, 0, 2) // grpc-status and grpc-message will be there if none else.
776 headerFields = append(headerFields, hpack.HeaderField{Name: ":status", Value: "200"})
777 headerFields = append(headerFields, hpack.HeaderField{Name: "content-type", Value: "application/grpc"})
779 headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status", Value: strconv.Itoa(int(st.Code()))})
780 headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-message", Value: encodeGrpcMessage(st.Message())})
782 if p := st.Proto(); p != nil && len(p.Details) > 0 {
783 stBytes, err := proto.Marshal(p)
785 // TODO: return error instead, when callers are able to handle it.
789 headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status-details-bin", Value: encodeBinHeader(stBytes)})
792 // Attach the trailer metadata.
793 for k, vv := range s.trailer {
794 // Clients don't tolerate reading restricted headers after some non restricted ones were sent.
795 if isReservedHeader(k) {
798 for _, v := range vv {
799 headerFields = append(headerFields, hpack.HeaderField{Name: k, Value: encodeMetadataHeader(k, v)})
802 t.controlBuf.put(&headerFrame{
808 t.stats.HandleRPC(s.Context(), &stats.OutTrailer{})
814 // Write converts the data into HTTP2 data frame and sends it out. Non-nil error
815 // is returns if it fails (e.g., framing error, transport error).
816 func (t *http2Server) Write(s *Stream, hdr []byte, data []byte, opts *Options) error {
819 return ContextErr(s.ctx.Err())
821 return ErrConnClosing
825 var writeHeaderFrame bool
827 if s.state == streamDone {
829 return streamErrorf(codes.Unknown, "the stream has been done")
832 writeHeaderFrame = true
835 if writeHeaderFrame {
836 t.WriteHeader(s, nil)
838 // Add data to header frame so that we can equally distribute data across frames.
839 emptyLen := http2MaxFrameLen - len(hdr)
840 if emptyLen > len(data) {
843 hdr = append(hdr, data[:emptyLen]...)
844 data = data[emptyLen:]
847 streamQuotaVer uint32
852 for _, r := range [][]byte{hdr, data} {
854 size := http2MaxFrameLen
858 if streamQuota == 0 { // Used up all the locally cached stream quota.
859 sqChan, streamQuotaVer = s.sendQuotaPool.acquireWithVersion()
860 // Wait until the stream has some quota to send the data.
861 streamQuota, err = wait(s.ctx, t.ctx, nil, nil, sqChan)
866 if localSendQuota <= 0 {
867 localSendQuota, err = wait(s.ctx, t.ctx, nil, nil, s.localSendQuota.acquire())
872 if size > streamQuota {
874 } // No need to do that for localSendQuota since that's only a soft limit.
875 // Wait until the transport has some quota to send the data.
876 tq, err := wait(s.ctx, t.ctx, nil, nil, t.sendQuotaPool.acquire())
884 t.sendQuotaPool.add(tq - size)
887 localSendQuota -= size
889 // Reset ping strikes when sending data since this might cause
890 // the peer to send ping.
891 atomic.StoreUint32(&t.resetPingStrikes, 1)
894 t.controlBuf.put(&dataFrame{streamID: s.id, endStream: false, d: p, f: func() {
895 s.localSendQuota.add(sz)
899 failure := func() { // The stream quota version must have changed.
900 // Our streamQuota cache is invalidated now, so give it back.
901 s.sendQuotaPool.lockedAdd(streamQuota + size)
903 if !s.sendQuotaPool.compareAndExecute(streamQuotaVer, success, failure) {
904 // Couldn't send this chunk out.
905 t.sendQuotaPool.add(size)
906 localSendQuota += size
912 // ADd the left over quota back to stream.
913 s.sendQuotaPool.add(streamQuota)
915 if localSendQuota > 0 {
916 s.localSendQuota.add(localSendQuota)
921 // keepalive running in a separate goroutine does the following:
922 // 1. Gracefully closes an idle connection after a duration of keepalive.MaxConnectionIdle.
923 // 2. Gracefully closes any connection after a duration of keepalive.MaxConnectionAge.
924 // 3. Forcibly closes a connection after an additive period of keepalive.MaxConnectionAgeGrace over keepalive.MaxConnectionAge.
925 // 4. Makes sure a connection is alive by sending pings with a frequency of keepalive.Time and closes a non-responsive connection
926 // after an additional duration of keepalive.Timeout.
927 func (t *http2Server) keepalive() {
930 maxIdle := time.NewTimer(t.kp.MaxConnectionIdle)
931 maxAge := time.NewTimer(t.kp.MaxConnectionAge)
932 keepalive := time.NewTimer(t.kp.Time)
933 // NOTE: All exit paths of this function should reset their
934 // respective timers. A failure to do so will cause the
935 // following clean-up to deadlock and eventually leak.
943 if !keepalive.Stop() {
952 if idle.IsZero() { // The connection is non-idle.
954 maxIdle.Reset(t.kp.MaxConnectionIdle)
957 val := t.kp.MaxConnectionIdle - time.Since(idle)
960 // The connection has been idle for a duration of keepalive.MaxConnectionIdle or more.
961 // Gracefully close the connection.
962 t.drain(http2.ErrCodeNo, []byte{})
963 // Reseting the timer so that the clean-up doesn't deadlock.
964 maxIdle.Reset(infinity)
969 t.drain(http2.ErrCodeNo, []byte{})
970 maxAge.Reset(t.kp.MaxConnectionAgeGrace)
973 // Close the connection after grace period.
975 // Reseting the timer so that the clean-up doesn't deadlock.
976 maxAge.Reset(infinity)
981 if atomic.CompareAndSwapUint32(&t.activity, 1, 0) {
983 keepalive.Reset(t.kp.Time)
988 // Reseting the timer so that the clean-up doesn't deadlock.
989 keepalive.Reset(infinity)
994 keepalive.Reset(t.kp.Timeout)
1001 var goAwayPing = &ping{data: [8]byte{1, 6, 1, 8, 0, 3, 3, 9}}
1003 // TODO(mmukhi): A lot of this code(and code in other places in the tranpsort layer)
1004 // is duplicated between the client and the server.
1005 // The transport layer needs to be refactored to take care of this.
1006 func (t *http2Server) itemHandler(i item) error {
1007 switch i := i.(type) {
1009 if err := t.framer.fr.WriteData(i.streamID, i.endStream, i.d); err != nil {
1016 for _, f := range i.hf {
1017 t.hEnc.WriteField(f)
1022 size := t.hBuf.Len()
1023 if size > http2MaxFrameLen {
1024 size = http2MaxFrameLen
1031 err = t.framer.fr.WriteHeaders(http2.HeadersFrameParam{
1032 StreamID: i.streamID,
1033 BlockFragment: t.hBuf.Next(size),
1034 EndStream: i.endStream,
1035 EndHeaders: endHeaders,
1038 err = t.framer.fr.WriteContinuation(
1048 atomic.StoreUint32(&t.resetPingStrikes, 1)
1051 return t.framer.fr.WriteWindowUpdate(i.streamID, i.increment)
1054 t.applySettings(i.ss)
1055 return t.framer.fr.WriteSettingsAck()
1057 return t.framer.fr.WriteSettings(i.ss...)
1059 return t.framer.fr.WriteRSTStream(i.streamID, i.code)
1062 if t.state == closing {
1064 // The transport is closing.
1065 return fmt.Errorf("transport: Connection closing")
1067 sid := t.maxStreamID
1069 // Stop accepting more streams now.
1072 if err := t.framer.fr.WriteGoAway(sid, i.code, i.debugData); err != nil {
1076 // Abruptly close the connection following the GoAway (via
1077 // loopywriter). But flush out what's inside the buffer first.
1078 t.framer.writer.Flush()
1079 return fmt.Errorf("transport: Connection closing")
1084 // For a graceful close, send out a GoAway with stream ID of MaxUInt32,
1085 // Follow that with a ping and wait for the ack to come back or a timer
1086 // to expire. During this time accept new streams since they might have
1087 // originated before the GoAway reaches the client.
1088 // After getting the ack or timer expiration send out another GoAway this
1089 // time with an ID of the max stream server intends to process.
1090 if err := t.framer.fr.WriteGoAway(math.MaxUint32, http2.ErrCodeNo, []byte{}); err != nil {
1093 if err := t.framer.fr.WritePing(false, goAwayPing.data); err != nil {
1097 timer := time.NewTimer(time.Minute)
1102 case <-t.ctx.Done():
1105 t.controlBuf.put(&goAway{code: i.code, debugData: i.debugData})
1109 return t.framer.writer.Flush()
1112 t.bdpEst.timesnap(i.data)
1114 return t.framer.fr.WritePing(i.ack, i.data)
1116 err := status.Errorf(codes.Internal, "transport: http2Server.controller got unexpected item type %t", i)
1122 // Close starts shutting down the http2Server transport.
1123 // TODO(zhaoq): Now the destruction is not blocked on any pending streams. This
1124 // could cause some resource issue. Revisit this later.
1125 func (t *http2Server) Close() error {
1127 if t.state == closing {
1129 return errors.New("transport: Close() was already called")
1132 streams := t.activeStreams
1133 t.activeStreams = nil
1136 err := t.conn.Close()
1137 // Cancel all active streams.
1138 for _, s := range streams {
1142 connEnd := &stats.ConnEnd{}
1143 t.stats.HandleConn(t.ctx, connEnd)
1148 // closeStream clears the footprint of a stream when the stream is not needed
1150 func (t *http2Server) closeStream(s *Stream) {
1152 delete(t.activeStreams, s.id)
1153 if len(t.activeStreams) == 0 {
1156 if t.state == draining && len(t.activeStreams) == 0 {
1160 // In case stream sending and receiving are invoked in separate
1161 // goroutines (e.g., bi-directional streaming), cancel needs to be
1162 // called to interrupt the potential blocking on other goroutines.
1165 if s.state == streamDone {
1169 s.state = streamDone
1173 func (t *http2Server) RemoteAddr() net.Addr {
1177 func (t *http2Server) Drain() {
1178 t.drain(http2.ErrCodeNo, []byte{})
1181 func (t *http2Server) drain(code http2.ErrCode, debugData []byte) {
1184 if t.drainChan != nil {
1187 t.drainChan = make(chan struct{})
1188 t.controlBuf.put(&goAway{code: code, debugData: debugData, headsUp: true})
1191 var rgen = rand.New(rand.NewSource(time.Now().UnixNano()))
1193 func getJitter(v time.Duration) time.Duration {
1197 // Generate a jitter between +/- 10% of the value.
1199 j := rgen.Int63n(2*r) - r
1200 return time.Duration(j)