Skip to content

Commit ecf9cbd

Browse files
authored
HTTP: tune client timeouts used by all requests. (#109)
Set higher default HTTP timeout, tune client's transport to use newer net.DialContext instead of deprecated net.Dial and fix inline comments.
1 parent 300c0ca commit ecf9cbd

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

selvpcclient/selvpc.go

+36-15
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,57 @@ const (
3030
// DefaultUserAgent contains basic user agent that will be used in queries.
3131
DefaultUserAgent = AppName + "/" + AppVersion
3232

33-
// defaultHTTPTimeout represents default timeout (in seconds) for HTTP
33+
// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP
3434
// requests.
35-
defaultHTTPTimeout = 40
35+
defaultHTTPTimeout = 60
3636

37-
// defaultDialTimeout represents default timeout (in seconds) for HTTP
37+
// defaultDialTimeout represents the default timeout (in seconds) for HTTP
3838
// connection establishments.
39-
defaultDialTimeout = 5
39+
defaultDialTimeout = 40
4040

41-
// defaultTLSHandshakeTimeout represents default timeout (in seconds) for
42-
// TSL handshake timeout.
43-
defaultTLSHandshakeTimeout = 5
41+
// defaultKeepalive represents the default keep-alive period for an active
42+
// network connection.
43+
defaultKeepaliveTimeout = 40
44+
45+
// defaultMaxIdleConns represents the maximum number of idle (keep-alive)
46+
// connections.
47+
defaultMaxIdleConns = 100
48+
49+
// defaultIdleConnTimeout represents the maximum amount of time an idle
50+
// (keep-alive) connection will remain idle before closing itself.
51+
defaultIdleConnTimeout = 100
52+
53+
// defaultTLSHandshakeTimeout represents the default timeout (in seconds)
54+
// for TLS handshake.
55+
defaultTLSHandshakeTimeout = 30
56+
57+
// defaultExpectContinueTimeout represents the default amount of time to
58+
// wait for a server's first response headers.
59+
defaultExpectContinueTimeout = 1
4460
)
4561

46-
// NewHTTPClient returns a reference to an initialized HTTP client with
47-
// configured timeouts.
62+
// NewHTTPClient returns a reference to an initialized configured HTTP client.
4863
func NewHTTPClient() *http.Client {
4964
return &http.Client{
5065
Timeout: time.Second * defaultHTTPTimeout,
5166
Transport: newHTTPTransport(),
5267
}
5368
}
5469

55-
// newHTTPTransport returns a reference to an initialized HTTP transport with
56-
// configured timeouts.
70+
// newHTTPTransport returns a reference to an initialized configured HTTP
71+
// transport.
5772
func newHTTPTransport() *http.Transport {
5873
return &http.Transport{
59-
Dial: (&net.Dialer{
60-
Timeout: time.Second * defaultDialTimeout,
61-
}).Dial,
62-
TLSHandshakeTimeout: time.Second * defaultTLSHandshakeTimeout,
74+
Proxy: http.ProxyFromEnvironment,
75+
DialContext: (&net.Dialer{
76+
Timeout: defaultDialTimeout * time.Second,
77+
KeepAlive: defaultKeepaliveTimeout * time.Second,
78+
DualStack: true,
79+
}).DialContext,
80+
MaxIdleConns: defaultMaxIdleConns,
81+
IdleConnTimeout: defaultIdleConnTimeout * time.Second,
82+
TLSHandshakeTimeout: defaultTLSHandshakeTimeout * time.Second,
83+
ExpectContinueTimeout: defaultExpectContinueTimeout * time.Second,
6384
}
6485
}
6586

0 commit comments

Comments
 (0)