From 514f93dd1dbb31e835fe240b82497e3edd0f59c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Wed, 18 Aug 2021 15:29:58 +0300 Subject: [PATCH] querier: exhaust response body before closing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exhaust the whole body of the response before closing to facilitate re-use of keep-alive connections as per the documentation here: https://pkg.go.dev/net/http#Client.Get ``` Caller should close resp.Body when done reading from it. ``` Also some discussion here: https://groups.google.com/g/golang-nuts/c/148riho42sU https://github.com/cortexproject/cortex/issues/4428 Signed-off-by: Giedrius Statkevičius --- pkg/querier/queryrange/roundtrip.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index d3c11d72cd..48f9d43efc 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -18,6 +18,8 @@ package queryrange import ( "context" "flag" + "io" + "io/ioutil" "net/http" "strings" "time" @@ -289,7 +291,10 @@ func (q roundTripper) Do(ctx context.Context, r Request) (Response, error) { if err != nil { return nil, err } - defer func() { _ = response.Body.Close() }() + defer func() { + io.Copy(ioutil.Discard, io.LimitReader(response.Body, 1024)) + _ = response.Body.Close() + }() return q.codec.DecodeResponse(ctx, response, r) }