Merge pull request #381 from vieux/closeIdleConnections

closeIdleConnections to prevent leaks with https
This commit is contained in:
Andrea Luzzardi 2015-02-10 13:41:28 -08:00
commit 7b009d6d43
2 changed files with 15 additions and 0 deletions

View File

@ -180,6 +180,11 @@ func getContainerJSON(c *context, w http.ResponseWriter, r *http.Request) {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
// cleanup
defer resp.Body.Close()
defer closeIdleConnections(client)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)

View File

@ -50,6 +50,13 @@ func copyHeader(dst, src http.Header) {
}
}
// prevents leak with https
func closeIdleConnections(client *http.Client) {
if tr, ok := client.Transport.(*http.Transport); ok {
tr.CloseIdleConnections()
}
}
func proxyAsync(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request, callback func(*http.Response)) error {
// Use a new client for each request
client, scheme := newClientAndScheme(tlsConfig)
@ -72,7 +79,10 @@ func proxyAsync(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *ht
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(NewWriteFlusher(w), resp.Body)
// cleanup
resp.Body.Close()
closeIdleConnections(client)
return nil
}