From abb10e1c23c97f620446b473fa1f65673b7e9454 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Date: Thu, 2 May 2019 13:10:59 +0200 Subject: [PATCH] Properly close response body before retrying This avoids leaking the TCP connection until the 30 seconds timeout and makes it reuse it instead --- registry/tokentransport.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/registry/tokentransport.go b/registry/tokentransport.go index c34edaa4..5301fca2 100644 --- a/registry/tokentransport.go +++ b/registry/tokentransport.go @@ -3,6 +3,8 @@ package registry import ( "encoding/json" "fmt" + "io" + "io/ioutil" "net/http" "net/url" ) @@ -13,12 +15,18 @@ type TokenTransport struct { Password string } +func discardResponse(resp *http.Response) { + _, _ = io.Copy(ioutil.Discard, resp.Body) + resp.Body.Close() +} + func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { resp, err := t.Transport.RoundTrip(req) if err != nil { return resp, err } if authService := isTokenDemand(resp); authService != nil { + discardResponse(resp) resp, err = t.authAndRetry(authService, req) } return resp, err