Functions for accessing "clean" Go http.Client values
The Go standard library contains a default
http.DefaultClient. It is a common idiom in Go code to start with
http.DefaultClient and tweak it as necessary, and in fact, this is
encouraged; from the
http package documentation:
The Client's Transport typically has internal state (cached TCP connections), so Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.
Unfortunately, this is a shared value, and it is not uncommon for libraries to assume that they are free to modify it at will. With enough dependencies, it can be very easy to encounter strange problems and race conditions due to manipulation of this shared value across libraries and goroutines (clients are safe for concurrent use, but writing values to the client struct itself is not protected).
Making things worse is the fact that a bare
http.Client will use a default
http.DefaultTransport, which is another global value
that behaves the same way. So it is not simply enough to replace
This repository provides some simple functions to get a "clean"
-- one that uses the same default values as the Go standard library, but
returns a client that does not share any state with other clients.
Package cleanhttp offers convenience utilities for acquiring "clean" http.Transport and http.Client structs.
Values set on http.DefaultClient and http.DefaultTransport affect all callers. This can have detrimental effects, esepcially in TLS contexts, where client or root certificates set to talk to multiple endpoints can end up displacing each other, leading to hard-to-debug issues. This package provides non-shared http.Client and http.Transport structs to ensure that the configuration will not be overwritten by other parts of the application or dependencies.
The DefaultClient and DefaultTransport functions disable idle connections and keepalives. Without ensuring that idle connections are closed before garbage collection, short-term clients/transports can leak file descriptors, eventually leading to "too many open files" errors. If you will be connecting to the same hosts repeatedly from the same client, you can use DefaultPooledClient to receive a client that has connection pooling semantics similar to http.DefaultClient.
func DefaultClient ¶
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.
func DefaultPooledClient ¶
DefaultPooledClient returns a new http.Client with similar default values to http.Client, but with a shared Transport. Do not use this function 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 DefaultPooledTransport ¶
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 DefaultTransport ¶
DefaultTransport returns a new http.Transport with similar default values to http.DefaultTransport, but with idle connections and keepalives disabled.