Skip to content

Commit

Permalink
Add TCP idle timeout (sideshow#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
closer committed Feb 21, 2018
1 parent df275e5 commit 53cb7f6
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ var DefaultHost = HostDevelopment
var (
// TLSDialTimeout is the maximum amount of time a dial will wait for a connect
// to complete.
TLSDialTimeout = 20 * time.Second
TLSDialTimeoGut = 20 * time.Second
// HTTPClientTimeout specifies a time limit for requests made by the
// HTTPClient. The timeout includes connection time, any redirects,
// and reading the response body.
HTTPClientTimeout = 60 * time.Second
// TCPKeepAlive specifies the keep-alive period for an active network
// connection. If zero, keep-alives are not enabled.
TCPKeepAlive = 60 * time.Second
// TCPIdleTimeout specifies the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// If zero, are no limit.
TCPIdleTimeout = 0 * time.Second
)

// DialTLS is the default dial function for creating TLS connections for
Expand Down Expand Up @@ -79,10 +84,11 @@ func NewClient(certificate tls.Certificate) *Client {
if len(certificate.Certificate) > 0 {
tlsConfig.BuildNameToCertificate()
}
transport := &http2.Transport{
TLSClientConfig: tlsConfig,
DialTLS: DialTLS,
}
transport, _ := http2.ConfigureTransport(&http.Transport{
IdleConnTimeout: TCPIdleTimeout,
})
transport.TLSClientConfig = tlsConfig
transport.DialTLS = DialTLS
return &Client{
HTTPClient: &http.Client{
Transport: transport,
Expand All @@ -102,9 +108,10 @@ func NewClient(certificate tls.Certificate) *Client {
// notifications; don’t repeatedly open and close connections. APNs treats rapid
// connection and disconnection as a denial-of-service attack.
func NewTokenClient(token *token.Token) *Client {
transport := &http2.Transport{
DialTLS: DialTLS,
}
transport, _ := http2.ConfigureTransport(&http.Transport{
IdleConnTimeout: TCPIdleTimeout,
})
transport.DialTLS = DialTLS
return &Client{
Token: token,
HTTPClient: &http.Client{
Expand Down

0 comments on commit 53cb7f6

Please sign in to comment.