diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index 995b52511b8..29c9ce99f49 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -100,6 +100,7 @@ The list below covers the major changes between 7.0.0-rc2 and main only. - Skip flakey metrics test on windows in filebeat httpjson input. {issue}39676[39676] {pull}39678[39678] - Fix flakey test on Windows 2022 in packetbeat/route. {issue}39698[39698] {pull}39822[39822] - Fix bug in minimum length for request trace logging. {pull}39834[39834] +- Close connections properly in Filbeat's HTTPJSON input. {pull}39790[39790] ==== Added diff --git a/x-pack/filebeat/input/httpjson/policy.go b/x-pack/filebeat/input/httpjson/policy.go index 0c671cb85bb..43360c1ed0f 100644 --- a/x-pack/filebeat/input/httpjson/policy.go +++ b/x-pack/filebeat/input/httpjson/policy.go @@ -91,6 +91,12 @@ 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 { + _, _ = io.Copy(io.Discard, resp.Body) + resp.Body.Close() + } + }() return true, nil } diff --git a/x-pack/filebeat/input/httpjson/request.go b/x-pack/filebeat/input/httpjson/request.go index 3e63f026716..b15f3db51b1 100644 --- a/x-pack/filebeat/input/httpjson/request.go +++ b/x-pack/filebeat/input/httpjson/request.go @@ -676,6 +676,7 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra if err != nil { return -1, fmt.Errorf("failed to collect response: %w", err) } + // store data according to response type if i == len(r.requestFactories)-1 && len(ids) != 0 { finalResps = append(finalResps, httpResp) @@ -702,12 +703,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 }