Skip to content

Commit 51d9895

Browse files
committed
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.
1 parent f680458 commit 51d9895

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

internal/http_api/api_request.go

+9-20
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,19 @@ import (
1313
"time"
1414
)
1515

16-
type deadlinedConn struct {
17-
Timeout time.Duration
18-
net.Conn
19-
}
20-
21-
func (c *deadlinedConn) Read(b []byte) (n int, err error) {
22-
return c.Conn.Read(b)
23-
}
24-
25-
func (c *deadlinedConn) Write(b []byte) (n int, err error) {
26-
return c.Conn.Write(b)
27-
}
28-
2916
// A custom http.Transport with support for deadline timeouts
3017
func NewDeadlineTransport(connectTimeout time.Duration, requestTimeout time.Duration) *http.Transport {
18+
// arbitrary values copied from http.DefaultTransport
3119
transport := &http.Transport{
32-
Dial: func(netw, addr string) (net.Conn, error) {
33-
c, err := net.DialTimeout(netw, addr, connectTimeout)
34-
if err != nil {
35-
return nil, err
36-
}
37-
return &deadlinedConn{connectTimeout, c}, nil
38-
},
20+
DialContext: (&net.Dialer{
21+
Timeout: connectTimeout,
22+
KeepAlive: 30 * time.Second,
23+
DualStack: true,
24+
}).DialContext,
3925
ResponseHeaderTimeout: requestTimeout,
26+
MaxIdleConns: 100,
27+
IdleConnTimeout: 90 * time.Second,
28+
TLSHandshakeTimeout: 10 * time.Second,
4029
}
4130
return transport
4231
}

0 commit comments

Comments
 (0)