Skip to content

Commit

Permalink
x-pack/filebeat/input/httpjson: Close connections properly
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Jun 4, 2024
1 parent df064b8 commit 7f3bf5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions x-pack/filebeat/input/httpjson/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (p *Policy) CustomRetryPolicy(ctx context.Context, resp *http.Response, err
// errors and may relate to outages on the server side. This will catch
// invalid response codes as well, like 0 and 999.
if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) {
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
return true, nil
}

Expand Down
15 changes: 7 additions & 8 deletions x-pack/filebeat/input/httpjson/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,6 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra
err error
urlCopy url.URL
urlString string
httpResp *http.Response
intermediateResps []*http.Response
finalResps []*http.Response
)
Expand Down Expand Up @@ -672,10 +671,16 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra
}

// collect data from new urls
httpResp, err = rf.collectResponse(ctx, chainTrCtx, r)
httpResp, err := rf.collectResponse(ctx, chainTrCtx, r)
if err != nil {
return -1, fmt.Errorf("failed to collect response: %w", err)
}
defer func() {
if httpResp != nil && httpResp.Body != nil {
httpResp.Body.Close()
}
}()

// store data according to response type
if i == len(r.requestFactories)-1 && len(ids) != 0 {
finalResps = append(finalResps, httpResp)
Expand All @@ -702,12 +707,6 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra
n += p.eventCount()
}

defer func() {
if httpResp != nil && httpResp.Body != nil {
httpResp.Body.Close()
}
}()

return n, nil
}

Expand Down

0 comments on commit 7f3bf5c

Please sign in to comment.