Skip to content

Commit

Permalink
internal http api: update NewDeadlineTransport() with new go http fea…
Browse files Browse the repository at this point in the history
…tures

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.
  • Loading branch information
ploxiln committed Sep 25, 2017
1 parent b417fcf commit 6def508
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions internal/http_api/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6def508

Please sign in to comment.