Skip to content

Commit 81a5b85

Browse files
committed
Fixed comments
1 parent 5bd1e15 commit 81a5b85

File tree

4 files changed

+41
-39
lines changed

4 files changed

+41
-39
lines changed

internal/transport/controlbuf.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ type incomingGoAway struct {
190190
func (*incomingGoAway) isTransportResponseFrame() bool { return false }
191191

192192
type goAway struct {
193-
code http2.ErrCode
194-
debugData []byte
195-
headsUp bool
196-
closeConnErr error // if set, loopyWriter will exit with this error
193+
code http2.ErrCode
194+
debugData []byte
195+
headsUp bool
196+
closeConn error // if set, loopyWriter will exit with this error
197197
}
198198

199199
func (*goAway) isTransportResponseFrame() bool { return false }
@@ -495,21 +495,22 @@ type loopyWriter struct {
495495
ssGoAwayHandler func(*goAway) (bool, error)
496496
}
497497

498-
func newLoopyWriter(s side, fr *framer, cbuf *controlBuffer, bdpEst *bdpEstimator, conn net.Conn, logger *grpclog.PrefixLogger) *loopyWriter {
498+
func newLoopyWriter(s side, fr *framer, cbuf *controlBuffer, bdpEst *bdpEstimator, conn net.Conn, logger *grpclog.PrefixLogger, goAwayHandler func(*goAway) (bool, error)) *loopyWriter {
499499
var buf bytes.Buffer
500500
l := &loopyWriter{
501-
side: s,
502-
cbuf: cbuf,
503-
sendQuota: defaultWindowSize,
504-
oiws: defaultWindowSize,
505-
estdStreams: make(map[uint32]*outStream),
506-
activeStreams: newOutStreamList(),
507-
framer: fr,
508-
hBuf: &buf,
509-
hEnc: hpack.NewEncoder(&buf),
510-
bdpEst: bdpEst,
511-
conn: conn,
512-
logger: logger,
501+
side: s,
502+
cbuf: cbuf,
503+
sendQuota: defaultWindowSize,
504+
oiws: defaultWindowSize,
505+
estdStreams: make(map[uint32]*outStream),
506+
activeStreams: newOutStreamList(),
507+
framer: fr,
508+
hBuf: &buf,
509+
hEnc: hpack.NewEncoder(&buf),
510+
bdpEst: bdpEst,
511+
conn: conn,
512+
logger: logger,
513+
ssGoAwayHandler: goAwayHandler,
513514
}
514515
return l
515516
}

internal/transport/http2_client.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
409409
go t.reader(readerErrCh)
410410
defer func() {
411411
if err != nil {
412+
// Close writerDone in case of error occurs
412413
close(t.writerDone)
413414
t.Close(err)
414415
}
@@ -456,12 +457,12 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
456457
if err := t.framer.writer.Flush(); err != nil {
457458
return nil, err
458459
}
460+
// Block until the server preface is received successfully or an error occurs.
459461
if err = <-readerErrCh; err != nil {
460462
return nil, err
461463
}
462464
go func() {
463-
t.loopy = newLoopyWriter(clientSide, t.framer, t.controlBuf, t.bdpEst, t.conn, t.logger)
464-
t.loopy.ssGoAwayHandler = t.outgoingGoAwayHandler
465+
t.loopy = newLoopyWriter(clientSide, t.framer, t.controlBuf, t.bdpEst, t.conn, t.logger, t.outgoingGoAwayHandler)
465466
if err := t.loopy.run(); !isIOError(err) {
466467
// Immediately close the connection, as the loopy writer returns
467468
// when there are no more active streams and we were draining (the
@@ -519,13 +520,13 @@ func (t *http2Client) getPeer() *peer.Peer {
519520
}
520521
}
521522

522-
// OutgoingGoAwayHandler writes GOAWAY to the connection. Always returns (false, err) as we want the GoAway
523+
// OutgoingGoAwayHandler writes a GOAWAY to the connection. Always returns (false, err) as we want the GoAway
523524
// to be the last frame loopy writes to the transport.
524525
func (t *http2Client) outgoingGoAwayHandler(g *goAway) (bool, error) {
525526
if err := t.framer.fr.WriteGoAway(math.MaxUint32, http2.ErrCodeNo, g.debugData); err != nil {
526527
return false, err
527528
}
528-
return false, g.closeConnErr
529+
return false, g.closeConn
529530
}
530531

531532
func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr) ([]hpack.HeaderField, error) {
@@ -1004,10 +1005,8 @@ func (t *http2Client) Close(err error) {
10041005
t.mu.Unlock()
10051006
// Per HTTP/2 spec, a GOAWAY frame must be sent before closing the
10061007
// connection. See https://httpwg.org/specs/rfc7540.html#GOAWAY.
1007-
t.controlBuf.put(&goAway{code: http2.ErrCodeNo, debugData: []byte(fmt.Sprintf("client shutdown with: %v", err)), closeConnErr: err})
1008-
if t.writerDone != nil {
1009-
<-t.writerDone
1010-
}
1008+
t.controlBuf.put(&goAway{code: http2.ErrCodeNo, debugData: []byte(fmt.Sprintf("client shutdown with: %v", err)), closeConn: err})
1009+
<-t.writerDone
10111010
t.cancel()
10121011
t.conn.Close()
10131012
channelz.RemoveEntry(t.channelz.ID)

internal/transport/http2_server.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
330330
t.handleSettings(sf)
331331

332332
go func() {
333-
t.loopy = newLoopyWriter(serverSide, t.framer, t.controlBuf, t.bdpEst, t.conn, t.logger)
334-
t.loopy.ssGoAwayHandler = t.outgoingGoAwayHandler
333+
t.loopy = newLoopyWriter(serverSide, t.framer, t.controlBuf, t.bdpEst, t.conn, t.logger, t.outgoingGoAwayHandler)
335334
err := t.loopy.run()
336335
close(t.loopyWriterDone)
337336
if !isIOError(err) {
@@ -673,9 +672,9 @@ func (t *http2Server) HandleStreams(ctx context.Context, handle func(*Stream)) {
673672
// Any error processing client headers, e.g. invalid stream ID,
674673
// is considered a protocol violation.
675674
t.controlBuf.put(&goAway{
676-
code: http2.ErrCodeProtocol,
677-
debugData: []byte(err.Error()),
678-
closeConnErr: err,
675+
code: http2.ErrCodeProtocol,
676+
debugData: []byte(err.Error()),
677+
closeConn: err,
679678
})
680679
continue
681680
}
@@ -920,7 +919,7 @@ func (t *http2Server) handlePing(f *http2.PingFrame) {
920919

921920
if t.pingStrikes > maxPingStrikes {
922921
// Send goaway and close the connection.
923-
t.controlBuf.put(&goAway{code: http2.ErrCodeEnhanceYourCalm, debugData: []byte("too_many_pings"), closeConnErr: errors.New("got too many pings from the client")})
922+
t.controlBuf.put(&goAway{code: http2.ErrCodeEnhanceYourCalm, debugData: []byte("too_many_pings"), closeConn: errors.New("got too many pings from the client")})
924923
}
925924
}
926925

@@ -1350,7 +1349,7 @@ func (t *http2Server) outgoingGoAwayHandler(g *goAway) (bool, error) {
13501349
// Stop accepting more streams now.
13511350
t.state = draining
13521351
sid := t.maxStreamID
1353-
retErr := g.closeConnErr
1352+
retErr := g.closeConn
13541353
if len(t.activeStreams) == 0 {
13551354
retErr = errors.New("second GOAWAY written and no active streams left to process")
13561355
}

internal/transport/transport_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,10 +2672,8 @@ func (s) TestClientSendsAGoAwayFrame(t *testing.T) {
26722672
defer lis.Close()
26732673
// greetDone is used to notify when server is done greeting the client.
26742674
greetDone := make(chan struct{})
2675-
// successCh verifies that GOAWAY is received at server side
2676-
successCh := make(chan struct{})
26772675
// errorCh verifies that desired GOAWAY not received by server
2678-
errorCh := make(chan struct{})
2676+
errorCh := make(chan error)
26792677
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
26802678
defer cancel()
26812679
// Launch the server.
@@ -2718,14 +2716,16 @@ func (s) TestClientSendsAGoAwayFrame(t *testing.T) {
27182716
goAwayFrame := fr
27192717
if goAwayFrame.ErrCode == http2.ErrCodeNo {
27202718
t.Logf("Received goAway frame from client")
2721-
close(successCh)
2719+
close(errorCh)
27222720
} else {
27232721
t.Logf("Received unexpected goAway frame from client")
2722+
errorCh <- errors.New("received unexpected goAway frame from client")
27242723
close(errorCh)
27252724
}
27262725
return
27272726
default:
27282727
t.Logf("The server received a frame other than GOAWAY")
2728+
errorCh <- errors.New("server received a frame other than GOAWAY")
27292729
close(errorCh)
27302730
return
27312731
}
@@ -2744,10 +2744,13 @@ func (s) TestClientSendsAGoAwayFrame(t *testing.T) {
27442744
ct.Close(errors.New("manually closed by client"))
27452745
t.Logf("Closed the client connection")
27462746
select {
2747-
case <-successCh:
2748-
case <-errorCh:
2749-
t.Errorf("Received an unexpected frame")
2747+
case err = <-errorCh:
27502748
case <-ctx.Done():
27512749
t.Errorf("Timed out")
27522750
}
2751+
if err != nil {
2752+
t.Errorf("Error receiving the GOAWAY frame: %v", err)
2753+
} else {
2754+
t.Logf("Received a GOAWAY frame from client")
2755+
}
27532756
}

0 commit comments

Comments
 (0)