Skip to content

proposal: net/http: Add NewDefaultTransport function #39299

@rittneje

Description

@rittneje

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions