open-nomad/vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go

57 lines
1.8 KiB
Go
Raw Normal View History

2016-02-12 18:02:16 +00:00
package cleanhttp
import (
"net"
"net/http"
2017-02-11 00:41:28 +00:00
"runtime"
2016-02-12 18:02:16 +00:00
"time"
)
// DefaultTransport returns a new http.Transport with the same default values
2016-02-17 21:59:20 +00:00
// as http.DefaultTransport, but with idle connections and keepalives disabled.
2016-02-12 18:02:16 +00:00
func DefaultTransport() *http.Transport {
2016-02-17 21:59:20 +00:00
transport := DefaultPooledTransport()
transport.DisableKeepAlives = true
transport.MaxIdleConnsPerHost = -1
return transport
}
// DefaultPooledTransport returns a new http.Transport with similar default
// values to http.DefaultTransport. Do not use this for transient transports as
// it can leak file descriptors over time. Only use this for transports that
// will be re-used for the same host(s).
func DefaultPooledTransport() *http.Transport {
2016-02-12 18:02:16 +00:00
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
2017-02-11 00:41:28 +00:00
DialContext: (&net.Dialer{
2016-02-12 18:02:16 +00:00
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
2017-02-11 00:41:28 +00:00
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
2016-02-12 18:02:16 +00:00
}
return transport
}
2016-02-17 21:59:20 +00:00
// DefaultClient returns a new http.Client with similar default values to
// http.Client, but with a non-shared Transport, idle connections disabled, and
// keepalives disabled.
2016-02-12 18:02:16 +00:00
func DefaultClient() *http.Client {
return &http.Client{
Transport: DefaultTransport(),
}
}
2016-02-17 21:59:20 +00:00
// DefaultPooledClient returns a new http.Client with the same default values
2017-01-31 19:42:18 +00:00
// as http.Client, but with a shared Transport. Do not use this function
2016-02-17 21:59:20 +00:00
// for transient clients as it can leak file descriptors over time. Only use
// this for clients that will be re-used for the same host(s).
func DefaultPooledClient() *http.Client {
return &http.Client{
Transport: DefaultPooledTransport(),
}
2016-02-12 18:02:16 +00:00
}