Skip to content

Commit

Permalink
feat: add TCP and TLS timeouts to HTTP client settings
Browse files Browse the repository at this point in the history
- Add `TCPTimeout` and `TLSHandshakeTimeout` options to `HTTPClientSettings`
- Set default values for `TCPTimeout` and `TLSHandshakeTimeout` if not provided
- Update `newCustomDialer` and `newCustomTransport` to use `TCPTimeout` and `TLSHandshakeTimeout` respectively

Signed-off-by: Corentin Barreau <[email protected]>
  • Loading branch information
CorentinB committed Jun 16, 2023
1 parent 1196190 commit 9b88877
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"os"
"sync"
"time"

"github.com/paulbellamy/ratecounter"
)
Expand All @@ -24,6 +25,8 @@ type HTTPClientSettings struct {
FullOnDisk bool
MaxReadBeforeTruncate int
FollowRedirects bool
TCPTimeout time.Duration
TLSHandshakeTimeout time.Duration
}

type CustomHTTPClient struct {
Expand Down Expand Up @@ -123,13 +126,22 @@ func NewWARCWritingHTTPClient(HTTPClientSettings HTTPClientSettings) (httpClient
}
}

// Verify timeouts and set default values
if HTTPClientSettings.TCPTimeout == 0 {
HTTPClientSettings.TCPTimeout = 10 * time.Second
}

if HTTPClientSettings.TLSHandshakeTimeout == 0 {
HTTPClientSettings.TLSHandshakeTimeout = 10 * time.Second
}

// Configure custom dialer / transport
customDialer, err := newCustomDialer(httpClient, HTTPClientSettings.Proxy)
customDialer, err := newCustomDialer(httpClient, HTTPClientSettings.Proxy, HTTPClientSettings.TCPTimeout)
if err != nil {
return nil, err
}

customTransport, err := newCustomTransport(customDialer, HTTPClientSettings.DecompressBody)
customTransport, err := newCustomTransport(customDialer, HTTPClientSettings.DecompressBody, HTTPClientSettings.TLSHandshakeTimeout)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type customDialer struct {
client *CustomHTTPClient
}

func newCustomDialer(httpClient *CustomHTTPClient, proxyURL string) (d *customDialer, err error) {
func newCustomDialer(httpClient *CustomHTTPClient, proxyURL string, TCPTimeout time.Duration) (d *customDialer, err error) {
d = new(customDialer)

d.Timeout = 5 * time.Second
d.Timeout = TCPTimeout
d.client = httpClient

if proxyURL != "" {
Expand Down
4 changes: 2 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (t *customTransport) RoundTrip(req *http.Request) (resp *http.Response, err
return
}

func newCustomTransport(dialer *customDialer, decompressBody bool) (t *customTransport, err error) {
func newCustomTransport(dialer *customDialer, decompressBody bool, TLSHandshakeTimeout time.Duration) (t *customTransport, err error) {
t = new(customTransport)

t.t = http.Transport{
Expand All @@ -45,7 +45,7 @@ func newCustomTransport(dialer *customDialer, decompressBody bool) (t *customTra
// disable keep alive
MaxConnsPerHost: 0,
IdleConnTimeout: -1,
TLSHandshakeTimeout: 15 * time.Second,
TLSHandshakeTimeout: TLSHandshakeTimeout,
ExpectContinueTimeout: 1 * time.Second,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DisableCompression: true,
Expand Down

0 comments on commit 9b88877

Please sign in to comment.