Skip to content

Commit ee11e85

Browse files
author
Chao Xu
committed
address Michael's comments
1 parent 231a5fe commit ee11e85

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

http2/transport.go

+10-17
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type Transport struct {
110110

111111
// PingTimeout is the timeout after which the connection will be closed
112112
// if a response to Ping is not received.
113-
// Defaults to 1s.
113+
// Defaults to 15s.
114114
PingTimeout time.Duration
115115

116116
// ReadIdleTimeout is the timeout after which a health check using ping
@@ -144,17 +144,9 @@ func (t *Transport) disableCompression() bool {
144144
return t.DisableCompression || (t.t1 != nil && t.t1.DisableCompression)
145145
}
146146

147-
func (t *Transport) readIdleTimeout() time.Duration {
148-
to := t.ReadIdleTimeout
149-
if to == 0 {
150-
to = 60 * time.Second
151-
}
152-
return to
153-
}
154-
155147
func (t *Transport) pingTimeout() time.Duration {
156148
if t.PingTimeout == 0 {
157-
return 1 * time.Second
149+
return 15 * time.Second
158150
}
159151
return t.PingTimeout
160152

@@ -708,8 +700,8 @@ func (cc *ClientConn) healthCheck() {
708700
// We don't need to periodically ping in the health check, because the readLoop of ClientConn will
709701
// trigger the healthCheck again if there is no frame received.
710702
ctx, cancel := context.WithTimeout(context.Background(), pingTimeout)
703+
defer cancel()
711704
err := cc.Ping(ctx)
712-
cancel()
713705
if err != nil {
714706
cc.closeForLostPing()
715707
cc.t.connPool().MarkDead(cc)
@@ -905,7 +897,7 @@ func (cc *ClientConn) Close() error {
905897

906898
// closes the client connection immediately. In-flight requests are interrupted.
907899
func (cc *ClientConn) closeForLostPing() error {
908-
err := errors.New("http2: client connection force closed because ping frame was not answered")
900+
err := errors.New("http2: client connection force closed because ping frame was not acknowledged")
909901
return cc.closeForError(err)
910902
}
911903

@@ -1761,15 +1753,16 @@ func (rl *clientConnReadLoop) run() error {
17611753
rl.closeWhenIdle = cc.t.disableKeepAlives() || cc.singleUse
17621754
gotReply := false // ever saw a HEADERS reply
17631755
gotSettings := false
1764-
to := cc.t.readIdleTimeout()
1756+
readIdleTimeout := cc.t.ReadIdleTimeout
17651757
var t *time.Timer
1766-
if to != 0 {
1767-
t = time.AfterFunc(to, cc.healthCheck)
1758+
if readIdleTimeout != 0 {
1759+
t = time.AfterFunc(readIdleTimeout, cc.healthCheck)
1760+
defer t.Stop()
17681761
}
17691762
for {
17701763
f, err := cc.fr.ReadFrame()
1771-
if to != 0 {
1772-
t.Reset(to)
1764+
if readIdleTimeout != 0 {
1765+
t.Reset(readIdleTimeout)
17731766
}
17741767
if err != nil {
17751768
cc.vlogf("http2: Transport readFrame error on conn %p: (%T) %v", cc, err, err)

http2/transport_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3254,8 +3254,8 @@ func TestTransportCloseAfterLostPing(t *testing.T) {
32543254
defer close(clientDone)
32553255
req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
32563256
_, err := ct.tr.RoundTrip(req)
3257-
if err == nil || !strings.Contains(err.Error(), "ping frame was not answered") {
3258-
return fmt.Errorf("expected to get error about \"ping frame was not answered\", got %v", err)
3257+
if err == nil || !strings.Contains(err.Error(), "ping frame was not acknowledged") {
3258+
return fmt.Errorf("expected to get error about \"ping frame was not acknowledged\", got %v", err)
32593259
}
32603260
return nil
32613261
}
@@ -3270,6 +3270,8 @@ func TestTransportCloseAfterLostPing(t *testing.T) {
32703270
func TestTransportPingWhenReading(t *testing.T) {
32713271
testTransportPingWhenReading(t, 50*time.Millisecond, 110*time.Millisecond, 20)
32723272
testTransportPingWhenReading(t, 100*time.Millisecond, 50*time.Millisecond, 0)
3273+
// 0 means no ping/pong health check.
3274+
testTransportPingWhenReading(t, 0, 50*time.Millisecond, 0)
32733275
}
32743276

32753277
func testTransportPingWhenReading(t *testing.T, readIdleTimeout, serverResponseInterval time.Duration, expectedPingCount int) {

0 commit comments

Comments
 (0)