Skip to content

Commit

Permalink
More improvements in HTTP client
Browse files Browse the repository at this point in the history
* Use `http.DefaultTransport`
* Set `MaxIdleConnsPerHost` equal to the `MaxIdleConns`
* Drain and close response body in defer function
  • Loading branch information
pondzix committed Jan 8, 2025
1 parent ed7a139 commit a38b012
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 a38b012

Please sign in to comment.