We would like to be able to use the default options for http.Transport without modifying the global, or worrying about whether anyone has modified the transport, or having to typecast it to call Clone. I propose adding a NewDefaultTransport() *Transport function to the http package. It should return a new Transport with the default options filled out. The global definition then becomes var DefaultTransport RoundTripper = NewDefaultTransport(). Note that the function returns *Transport not RoundTripper so we don't have to typecast it.
func NewDefaultTransport() *Transport {
return &Transport{
Proxy: ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
This was previously discussed in #26013, but in that case people wanted to pull in modifications to the global that had been made, so the Clone() method was introduced.
We would like to be able to use the default options for
http.Transportwithout modifying the global, or worrying about whether anyone has modified the transport, or having to typecast it to callClone. I propose adding aNewDefaultTransport() *Transportfunction to the http package. It should return a newTransportwith the default options filled out. The global definition then becomesvar DefaultTransport RoundTripper = NewDefaultTransport(). Note that the function returns*TransportnotRoundTripperso we don't have to typecast it.This was previously discussed in #26013, but in that case people wanted to pull in modifications to the global that had been made, so the
Clone()method was introduced.