Skip to content

Commit

Permalink
More improvements in HTTP client
Browse files Browse the repository at this point in the history
Continuation of 065a10a:

* Use `http.DefaultTransport`

It's because transport provided by `net/http` has some sensible defaults. Before that change we would use "empty" `http.Transport` with "empty" defaults.

* Set `MaxIdleConnsPerHost` equal to the `MaxIdleConns`

In Snowbridge we basically target single host, so it makes sens to set `MaxIdleConnsPerHost` == `MaxIdleConns`.
`MaxIdleConns` provided by the `http.DefaultTransport` is 100. Before that change `MaxIdleConnsPerHost` was 2  (`http.DefaultMaxIdleConnsPerHost`).

* Drain and close response body in defer function
  • Loading branch information
pondzix committed Jan 13, 2025
1 parent ed7a139 commit 78250dd
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/target/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ func newHTTPTarget(
if err1 != nil {
return nil, err1
}
transport := &http.Transport{}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.MaxIdleConnsPerHost = transport.MaxIdleConns

tlsConfig, err2 := common.CreateTLSConfiguration(certFile, keyFile, caFile, skipVerifyTLS)
if err2 != nil {
Expand Down Expand Up @@ -398,12 +399,12 @@ func (ht *HTTPTarget) Write(messages []*models.Message) (*models.TargetWriteResu
continue
}

// Ensure response body is always closed
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// Drain and close the body for successful responses
_, _ = io.Copy(io.Discard, resp.Body)
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
for _, msg := range goodMsgs {
if msg.AckFunc != nil { // Ack successful messages
msg.AckFunc()
Expand All @@ -415,8 +416,6 @@ func (ht *HTTPTarget) Write(messages []*models.Message) (*models.TargetWriteResu

// Process non-2xx responses
responseBody, err := io.ReadAll(resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close() // Explicitly close the body after reading

if err != nil {
failed = append(failed, goodMsgs...)
Expand Down

0 comments on commit 78250dd

Please sign in to comment.