Skip to content

Commit

Permalink
Equip default transport with reduced connect timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Mar 15, 2022
1 parent 3537f2d commit 6972bc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion util/request/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/transport"
)

// Timeout is the default request timeout used by the Helper
Expand All @@ -21,7 +22,7 @@ func NewHelper(log *util.Logger) *Helper {
r := &Helper{
Client: &http.Client{
Timeout: Timeout,
Transport: NewTripper(log, http.DefaultTransport),
Transport: NewTripper(log, transport.Default()),
},
}

Expand Down
19 changes: 14 additions & 5 deletions util/transport/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ package transport

import (
"crypto/tls"
"net"
"net/http"
"time"
)

// Default returns http.DefaultTransport as http.Transport instead of http.RoundTripper
// Default returns an http.DefaultTransport as http.Transport with reduced dial timeout
func Default() *http.Transport {
t, ok := http.DefaultTransport.(*http.Transport)
if !ok {
panic("http.DefaultTransport is not an http.Transport")
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second, // reduced from 30s
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
return t
}

// InsecureTransport is an http.Transport with TLSClientConfig.InsecureSkipVerify enabled
Expand Down

0 comments on commit 6972bc6

Please sign in to comment.