Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure http client reuses connections #51

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions client/rest/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
func NewHTTPClient(proxyUrl string) (*http.Client, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.MaxConnsPerHost = 200
transport.MaxIdleConnsPerHost = 200
transport.DisableKeepAlives = false

// defaults to TLS 1.0 which is not favorable
transport.TLSClientConfig = &tls.Config{
Expand All @@ -43,6 +45,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

Expand Down Expand Up @@ -72,7 +75,6 @@ func NewRequest(
params map[string]string,
headers map[string]string,
) (*http.Request, error) {

// set query params
if params != nil {
q := endpoint.Query()
Expand All @@ -83,22 +85,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
Expand Down
Loading