diff --git a/client_options.go b/client_options.go index 7ee277d..9653d1b 100644 --- a/client_options.go +++ b/client_options.go @@ -18,6 +18,9 @@ type TransportOptions struct { MaxResponseHeaderBytes int64 // Zero means to use a default limit. WriteBufferSize int // If zero, a default (currently 4KB) is used. ReadBufferSize int // If zero, a default (currently 4KB) is used. + // IdleConnTimeout is the maximum amount of time an idle (keep-alive) + // connection will remain idle before closing itself. Zero means no limit. + IdleConnTimeout time.Duration } type BadPinHandlerFunc func(req *http.Request) diff --git a/go.mod b/go.mod index 95ebb63..8a4db1b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bogdanfinn/tls-client go 1.18 require ( - github.com/bogdanfinn/fhttp v0.5.20 + github.com/bogdanfinn/fhttp v0.5.21-public-test github.com/bogdanfinn/utls v1.5.16 github.com/google/uuid v1.3.0 github.com/stretchr/testify v1.8.0 diff --git a/go.sum b/go.sum index 5cdb0e3..1722417 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/bogdanfinn/fhttp v0.5.20 h1:joQrA0tVFBQNQ8BbKRmIM972GfawXA+qbCRQlX6dauQ= -github.com/bogdanfinn/fhttp v0.5.20/go.mod h1:brqi5woc5eSCVHdKYBV8aZLbO7HGqpwyDLeXW+fT18I= +github.com/bogdanfinn/fhttp v0.5.21-public-test h1:3/NtlIawWB+9zaekAF8ZVcLd9jFqiQi8g07ycUiKo0s= +github.com/bogdanfinn/fhttp v0.5.21-public-test/go.mod h1:brqi5woc5eSCVHdKYBV8aZLbO7HGqpwyDLeXW+fT18I= github.com/bogdanfinn/utls v1.5.16 h1:NhhWkegEcYETBMj9nvgO4lwvc6NcLH+znrXzO3gnw4M= github.com/bogdanfinn/utls v1.5.16/go.mod h1:mHeRCi69cUiEyVBkKONB1cAbLjRcZnlJbGzttmiuK4o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/roundtripper.go b/roundtripper.go index 3fd130d..cbc701f 100644 --- a/roundtripper.go +++ b/roundtripper.go @@ -7,6 +7,7 @@ import ( "net" "strings" "sync" + "time" http "github.com/bogdanfinn/fhttp" "github.com/bogdanfinn/fhttp/http2" @@ -141,7 +142,19 @@ func (rt *roundTripper) dialTLS(ctx context.Context, network, addr string) (net. utlsConfig.ServerName = rt.serverNameOverwrite } - t2 := http2.Transport{DialTLS: rt.dialTLSHTTP2, TLSClientConfig: utlsConfig, ConnectionFlow: rt.connectionFlow, HeaderPriority: rt.headerPriority} + idleConnectionTimeout := 90 * time.Second + + if rt.transportOptions != nil { + idleConnectionTimeout = rt.transportOptions.IdleConnTimeout + } + + t2 := http2.Transport{ + DialTLS: rt.dialTLSHTTP2, + TLSClientConfig: utlsConfig, + ConnectionFlow: rt.connectionFlow, + HeaderPriority: rt.headerPriority, + IdleConnTimeout: idleConnectionTimeout, + } if rt.transportOptions != nil { t1 := t2.GetT1() @@ -154,6 +167,7 @@ func (rt *roundTripper) dialTLS(ctx context.Context, network, addr string) (net. t1.MaxResponseHeaderBytes = rt.transportOptions.MaxResponseHeaderBytes t1.WriteBufferSize = rt.transportOptions.WriteBufferSize t1.ReadBufferSize = rt.transportOptions.ReadBufferSize + t1.IdleConnTimeout = rt.transportOptions.IdleConnTimeout } } @@ -221,6 +235,7 @@ func (rt *roundTripper) buildHttp1Transport() *http.Transport { t.MaxResponseHeaderBytes = rt.transportOptions.MaxResponseHeaderBytes t.WriteBufferSize = rt.transportOptions.WriteBufferSize t.ReadBufferSize = rt.transportOptions.ReadBufferSize + t.IdleConnTimeout = rt.transportOptions.IdleConnTimeout } return t