From 8d97dcfa25df8cf30332079191ead6864a16dd4a Mon Sep 17 00:00:00 2001 From: Dillon Lees Date: Thu, 5 Oct 2023 10:58:23 -0700 Subject: [PATCH 1/2] fix: ensure http client reuses connections --- client/rest/http.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/client/rest/http.go b/client/rest/http.go index a8c64f2..2ef5d3c 100644 --- a/client/rest/http.go +++ b/client/rest/http.go @@ -35,6 +35,9 @@ import ( func NewHTTPClient(proxyUrl string) (*http.Client, error) { transport := http.DefaultTransport.(*http.Transport).Clone() transport.MaxConnsPerHost = 200 + transport.MaxIdleConnsPerHost = 200 + transport.MaxIdleConns = 200 + transport.DisableKeepAlives = false // defaults to TLS 1.0 which is not favorable transport.TLSClientConfig = &tls.Config{ @@ -43,6 +46,7 @@ func NewHTTPClient(proxyUrl string) (*http.Client, error) { // increasing timeout because tls handshakes can take longer when doing a lot of concurrent calls transport.TLSHandshakeTimeout = 20 * time.Second + // increasing response header timeout to accout for WAF throttling rules transport.ResponseHeaderTimeout = 5 * time.Minute @@ -72,7 +76,6 @@ func NewRequest( params map[string]string, headers map[string]string, ) (*http.Request, error) { - // set query params if params != nil { q := endpoint.Query() @@ -83,22 +86,26 @@ func NewRequest( } // set body - var buf io.Reader + var ( + reader io.Reader + buffer = &bytes.Buffer{} + ) if body != nil { switch body := body.(type) { case url.Values: - buf = strings.NewReader(body.Encode()) + reader = strings.NewReader(body.Encode()) default: data := new(bytes.Buffer) if err := json.NewEncoder(data).Encode(body); err != nil { return nil, err } else { - buf = data + reader = data } } + buffer.ReadFrom(reader) } - if req, err := http.NewRequestWithContext(ctx, verb, endpoint.String(), buf); err != nil { + if req, err := http.NewRequestWithContext(ctx, verb, endpoint.String(), buffer); err != nil { return nil, err } else { // set headers From 7a5c30cb8e9423856cc52b1cab74b728d34ad4b3 Mon Sep 17 00:00:00 2001 From: Dillon Lees Date: Thu, 5 Oct 2023 14:00:18 -0700 Subject: [PATCH 2/2] fix: remove overall connection limit --- client/rest/http.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/rest/http.go b/client/rest/http.go index 2ef5d3c..5fa256a 100644 --- a/client/rest/http.go +++ b/client/rest/http.go @@ -36,7 +36,6 @@ func NewHTTPClient(proxyUrl string) (*http.Client, error) { transport := http.DefaultTransport.(*http.Transport).Clone() transport.MaxConnsPerHost = 200 transport.MaxIdleConnsPerHost = 200 - transport.MaxIdleConns = 200 transport.DisableKeepAlives = false // defaults to TLS 1.0 which is not favorable