From 51d9895612841f84504d5a3fab833f88f5bf3c3c Mon Sep 17 00:00:00 2001 From: Pierce Lopez Date: Mon, 25 Sep 2017 00:12:08 -0400 Subject: [PATCH] internal http api: update NewDeadlineTransport() with new go http features More timeouts and limits were added to http.Transport in go-1.7. Some that were enabled in http.DefaultTransport ended up disabled in nsq's with-custom-timeouts http.Transport. IdleConnTimeout in particular is helpful. --- internal/http_api/api_request.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/internal/http_api/api_request.go b/internal/http_api/api_request.go index 635fb3a51..20fad6562 100644 --- a/internal/http_api/api_request.go +++ b/internal/http_api/api_request.go @@ -13,30 +13,19 @@ import ( "time" ) -type deadlinedConn struct { - Timeout time.Duration - net.Conn -} - -func (c *deadlinedConn) Read(b []byte) (n int, err error) { - return c.Conn.Read(b) -} - -func (c *deadlinedConn) Write(b []byte) (n int, err error) { - return c.Conn.Write(b) -} - // A custom http.Transport with support for deadline timeouts func NewDeadlineTransport(connectTimeout time.Duration, requestTimeout time.Duration) *http.Transport { + // arbitrary values copied from http.DefaultTransport transport := &http.Transport{ - Dial: func(netw, addr string) (net.Conn, error) { - c, err := net.DialTimeout(netw, addr, connectTimeout) - if err != nil { - return nil, err - } - return &deadlinedConn{connectTimeout, c}, nil - }, + DialContext: (&net.Dialer{ + Timeout: connectTimeout, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, ResponseHeaderTimeout: requestTimeout, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, } return transport }