Skip to content

Commit 5058c86

Browse files
author
Chao Xu
committed
minor fixes
1 parent 331a10c commit 5058c86

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

http2/transport.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ type Transport struct {
116116

117117
// PingTimeout is the timeout after which the connection will be closed
118118
// if a response to Ping is not received.
119-
// 0 means no periodic pings. Defaults to 0.
119+
// Defaults to PingPeriod/2
120120
PingTimeout time.Duration
121121

122122
// ReadIdleTimeout is the timeout after which the periodic ping for
123123
// connection health check will begin if no frame is received on the
124124
// connection.
125-
// The health check will stop once there is frame received on the
125+
// The health check will stop once there is a frame received on the
126126
// connection.
127127
// Defaults to 60s.
128128
ReadIdleTimeout time.Duration
@@ -158,6 +158,14 @@ func (t *Transport) readIdleTimeout() time.Duration {
158158
return to
159159
}
160160

161+
func (t *Transport) pingTimeout() time.Duration {
162+
if t.PingTimeout == 0 {
163+
return t.PingPeriod / 2
164+
}
165+
return t.PingTimeout
166+
167+
}
168+
161169
// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
162170
// It returns an error if t1 has already been HTTP/2-enabled.
163171
func ConfigureTransport(t1 *http.Transport) error {
@@ -267,7 +275,8 @@ type ClientConn struct {
267275
wmu sync.Mutex // held while writing; acquire AFTER mu if holding both
268276
werr error // first write error that has occurred
269277

270-
healthCheckStopCh chan struct{}
278+
hmu sync.Mutex // guard the healthCheckStopCh
279+
healthCheckStopCh chan struct{} // A close-only channel to stop the health check.
271280
}
272281

273282
// clientStream is the state for a single HTTP/2 stream. One of these
@@ -705,7 +714,7 @@ func (t *Transport) newClientConn(c net.Conn, singleUse bool) (*ClientConn, erro
705714

706715
func (cc *ClientConn) healthCheck(stop chan struct{}) {
707716
pingPeriod := cc.t.PingPeriod
708-
pingTimeout := cc.t.PingTimeout
717+
pingTimeout := cc.t.pingTimeout()
709718
if pingPeriod == 0 || pingTimeout == 0 {
710719
return
711720
}
@@ -729,6 +738,8 @@ func (cc *ClientConn) healthCheck(stop chan struct{}) {
729738
}
730739

731740
func (cc *ClientConn) startHealthCheck() {
741+
cc.hmu.Lock()
742+
defer cc.hmu.Unlock()
732743
if cc.healthCheckStopCh != nil {
733744
// a health check is already running
734745
return
@@ -738,6 +749,8 @@ func (cc *ClientConn) startHealthCheck() {
738749
}
739750

740751
func (cc *ClientConn) stopHealthCheck() {
752+
cc.hmu.Lock()
753+
defer cc.hmu.Unlock()
741754
if cc.healthCheckStopCh == nil {
742755
// no health check running
743756
return
@@ -934,7 +947,7 @@ func (cc *ClientConn) Close() error {
934947

935948
// closes the client connection immediately. In-flight requests are interrupted.
936949
func (cc *ClientConn) closeForLostPing() error {
937-
err := errors.New("http2: client connection force closed because ping frame is not responded")
950+
err := errors.New("http2: client connection force closed because ping frame was not answered")
938951
return cc.closeForError(err)
939952
}
940953

0 commit comments

Comments
 (0)