closeResponse close non nil response with any response Body. convenient wrapper to drain any remaining data on response body. Subsequently this allows golang http RoundTripper to re-use the same connection for future requests.
(resp *http.Response)
| 133 | // Subsequently this allows golang http RoundTripper |
| 134 | // to re-use the same connection for future requests. |
| 135 | func closeResponse(resp *http.Response) { |
| 136 | // Callers should close resp.Body when done reading from it. |
| 137 | // If resp.Body is not closed, the Client's underlying RoundTripper |
| 138 | // (typically Transport) may not be able to re-use a persistent TCP |
| 139 | // connection to the server for a subsequent "keep-alive" request. |
| 140 | if resp != nil && resp.Body != nil { |
| 141 | // Drain any remaining Body and then close the connection. |
| 142 | // Without this closing connection would disallow re-using |
| 143 | // the same connection for future uses. |
| 144 | // - http://stackoverflow.com/a/17961593/4465767 |
| 145 | io.Copy(io.Discard, resp.Body) |
| 146 | resp.Body.Close() |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | var ( |
| 151 | // Hex encoded string of nil sha256sum bytes. |
no test coverage detected