*: drain http.Response.Body before closing

This commit is contained in:
Gyu-Ho Lee
2016-03-30 04:19:58 -07:00
parent 2deed74494
commit a42d1dc1fe
4 changed files with 21 additions and 8 deletions

View File

@@ -7,7 +7,11 @@
// Package httputil provides HTTP utility functions.
package httputil
import "net/http"
import (
"io"
"io/ioutil"
"net/http"
)
func RequestCanceler(rt http.RoundTripper, req *http.Request) func() {
ch := make(chan struct{})
@@ -17,3 +21,11 @@ func RequestCanceler(rt http.RoundTripper, req *http.Request) func() {
close(ch)
}
}
// GracefulClose drains http.Response.Body until it hits EOF
// and closes it. This prevents TCP/TLS connections from closing,
// therefore available for reuse.
func GracefulClose(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}