Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP: tune client timeouts used by all requests. #109

Merged
merged 1 commit into from
Dec 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions selvpcclient/selvpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,57 @@ const (
// DefaultUserAgent contains basic user agent that will be used in queries.
DefaultUserAgent = AppName + "/" + AppVersion

// defaultHTTPTimeout represents default timeout (in seconds) for HTTP
// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP
// requests.
defaultHTTPTimeout = 40
defaultHTTPTimeout = 60

// defaultDialTimeout represents default timeout (in seconds) for HTTP
// defaultDialTimeout represents the default timeout (in seconds) for HTTP
// connection establishments.
defaultDialTimeout = 5
defaultDialTimeout = 40

// defaultTLSHandshakeTimeout represents default timeout (in seconds) for
// TSL handshake timeout.
defaultTLSHandshakeTimeout = 5
// defaultKeepalive represents the default keep-alive period for an active
// network connection.
defaultKeepaliveTimeout = 40

// defaultMaxIdleConns represents the maximum number of idle (keep-alive)
// connections.
defaultMaxIdleConns = 100

// defaultIdleConnTimeout represents the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing itself.
defaultIdleConnTimeout = 100

// defaultTLSHandshakeTimeout represents the default timeout (in seconds)
// for TLS handshake.
defaultTLSHandshakeTimeout = 30

// defaultExpectContinueTimeout represents the default amount of time to
// wait for a server's first response headers.
defaultExpectContinueTimeout = 1
)

// NewHTTPClient returns a reference to an initialized HTTP client with
// configured timeouts.
// NewHTTPClient returns a reference to an initialized configured HTTP client.
func NewHTTPClient() *http.Client {
return &http.Client{
Timeout: time.Second * defaultHTTPTimeout,
Transport: newHTTPTransport(),
}
}

// newHTTPTransport returns a reference to an initialized HTTP transport with
// configured timeouts.
// newHTTPTransport returns a reference to an initialized configured HTTP
// transport.
func newHTTPTransport() *http.Transport {
return &http.Transport{
Dial: (&net.Dialer{
Timeout: time.Second * defaultDialTimeout,
}).Dial,
TLSHandshakeTimeout: time.Second * defaultTLSHandshakeTimeout,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultDialTimeout * time.Second,
KeepAlive: defaultKeepaliveTimeout * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: defaultMaxIdleConns,
IdleConnTimeout: defaultIdleConnTimeout * time.Second,
TLSHandshakeTimeout: defaultTLSHandshakeTimeout * time.Second,
ExpectContinueTimeout: defaultExpectContinueTimeout * time.Second,
}
}

Expand Down