parent
f6ff39ffb0
commit
dfffe5cd15
|
@ -157,7 +157,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/go-cleanhttp",
|
||||
"Rev": "5df5ddc69534f1a4697289f1dca2193fbb40213f"
|
||||
"Rev": "ce617e79981a8fff618bb643d155133a8f38db96"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/hashicorp/go-multierror",
|
||||
|
|
|
@ -3,13 +3,14 @@ package cleanhttp
|
|||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultTransport returns a new http.Transport with the same default values
|
||||
// as http.DefaultTransport
|
||||
func DefaultTransport() *http.Transport {
|
||||
return &http.Transport{
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
|
@ -17,6 +18,8 @@ func DefaultTransport() *http.Transport {
|
|||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
}
|
||||
SetTransportFinalizer(transport)
|
||||
return transport
|
||||
}
|
||||
|
||||
// DefaultClient returns a new http.Client with the same default values as
|
||||
|
@ -26,3 +29,12 @@ func DefaultClient() *http.Client {
|
|||
Transport: DefaultTransport(),
|
||||
}
|
||||
}
|
||||
|
||||
// SetTransportFinalizer sets a finalizer on the transport to ensure that
|
||||
// idle connections are closed prior to garbage collection; otherwise
|
||||
// these may leak
|
||||
func SetTransportFinalizer(transport *http.Transport) {
|
||||
runtime.SetFinalizer(&transport, func(t **http.Transport) {
|
||||
(*t).CloseIdleConnections()
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue