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

More improvements in HTTP client #388

Merged
merged 1 commit into from
Jan 15, 2025
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
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)
pondzix marked this conversation as resolved.
Show resolved Hide resolved
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
Loading