From d797a34ca926292f34ae0c776831e6638fad2008 Mon Sep 17 00:00:00 2001 From: Hoonmin Kim Date: Fri, 6 Mar 2015 00:36:28 +0900 Subject: [PATCH] Added a read timeout for http.Client. Signed-off-by: Hoonmin Kim --- utils.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/utils.go b/utils.go index 806f1b3..74a6c5c 100644 --- a/utils.go +++ b/utils.go @@ -12,22 +12,18 @@ func newHTTPClient(u *url.URL, tlsConfig *tls.Config, timeout time.Duration) *ht httpTransport := &http.Transport{ TLSClientConfig: tlsConfig, } - - switch u.Scheme { - default: - httpTransport.Dial = func(proto, addr string) (net.Conn, error) { - return net.DialTimeout(proto, addr, timeout) - } - case "unix": + if u.Scheme == "unix" { socketPath := u.Path - unixDial := func(proto, addr string) (net.Conn, error) { - return net.DialTimeout("unix", socketPath, timeout) + unixDial := func(proto string, addr string) (net.Conn, error) { + return net.Dial("unix", socketPath) } httpTransport.Dial = unixDial // Override the main URL object so the HTTP lib won't complain u.Scheme = "http" u.Host = "unix.sock" - u.Path = "" } - return &http.Client{Transport: httpTransport} + u.Path = "" + // The timeout includes connection time, reading the response body. + // A Timeout of zero means no timeout. + return &http.Client{Transport: httpTransport, Timeout: timeout} }