Skip to content

Commit

Permalink
refs #33: allow settings of idle connection timeout on http transport;
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanfinn committed Apr 21, 2023
1 parent 406c2cd commit 2aa5f55
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
26 changes: 24 additions & 2 deletions roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"strings"
"sync"
"time"

http "github.com/bogdanfinn/fhttp"
"github.com/bogdanfinn/fhttp/http2"
Expand All @@ -15,6 +16,8 @@ import (
utls "github.com/bogdanfinn/utls"
)

const defaultIdleConnectionTimeout = 90 * time.Second

var errProtocolNegotiated = errors.New("protocol negotiated")

type roundTripper struct {
Expand Down Expand Up @@ -141,7 +144,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 := defaultIdleConnectionTimeout

if rt.transportOptions != nil && rt.transportOptions.IdleConnTimeout != 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()
Expand All @@ -154,6 +169,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 = idleConnectionTimeout
}
}

Expand Down Expand Up @@ -210,7 +226,13 @@ func (rt *roundTripper) buildHttp1Transport() *http.Transport {
utlsConfig.ServerName = rt.serverNameOverwrite
}

t := &http.Transport{DialTLSContext: rt.dialTLS, TLSClientConfig: utlsConfig, ConnectionFlow: rt.connectionFlow}
idleConnectionTimeout := defaultIdleConnectionTimeout

if rt.transportOptions != nil && rt.transportOptions.IdleConnTimeout != nil {
idleConnectionTimeout = *rt.transportOptions.IdleConnTimeout
}

t := &http.Transport{DialTLSContext: rt.dialTLS, TLSClientConfig: utlsConfig, ConnectionFlow: rt.connectionFlow, IdleConnTimeout: idleConnectionTimeout}

if rt.transportOptions != nil {
t.DisableKeepAlives = rt.transportOptions.DisableKeepAlives
Expand Down

0 comments on commit 2aa5f55

Please sign in to comment.