reg icon indicating copy to clipboard operation
reg copied to clipboard

TokenTransport may cause amount of ESTABLISHED connections

Open mysugar opened this issue 7 years ago • 2 comments

The unused resp.Body should be Close() and read , or http.Client will make many new connections.

here the code

I think it should like

func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	resp, err := t.Transport.RoundTrip(req)
	if err != nil {
		return resp, err
	}

	authService, err := isTokenDemand(resp)
	if err != nil {
		defer resp.Body.Close()
		io.Copy(ioutil.Discard,resp.Body)
		return nil, err
	}

	if authService == nil {
		return resp, nil
	}

	defer resp.Body.Close()
	io.Copy(ioutil.Discard,resp.Body)
	return t.authAndRetry(authService, req)
}

mysugar avatar Sep 30 '18 03:09 mysugar

want to make a PR, thanks!

jessfraz avatar Nov 14 '18 14:11 jessfraz

I’m glad to,will make some days later.

mysugar avatar Nov 15 '18 17:11 mysugar