http.Transport is a RoundTripper that sends HTTP requests, receives responses, establishes TCP connections, and maintains a pool of reusable connections. An http.Client delegates transmission to its Transport rather than performing network I/O itself. section 7.3.1 section 7.4.1
Configure Transport alongside Client when a request-heavy program needs predictable connection and timeout behavior:
- Reuse the same Transport for multiple requests so idle TCP connections can be reused instead of repeatedly reconnecting. section 7.4.1
- Set MaxIdleConnsPerHost to match the request concurrency when workers repeatedly contact the same host, reducing connection churn. section 7.4.2
- Pair the Transport with Client.CheckRedirect and Client.Timeout when redirects could distort measurements or slow responses could block workers. section 7.4.2
Tune a client for concurrent requests
client := &http.Client{ Transport: &http.Transport{ MaxIdleConnsPerHost: concurrency, }, CheckRedirect: func(_ *http.Request, _ []*http.Request) error { return http.ErrUseLastResponse }, Timeout: 30 * time.Second, } send := func(r *http.Request) Result { return Send(client, r) } section 7.4.2
Listing 7.5 Optimizing http.Client (hit/option.go)
package hit import ( . . . "time" ) func withDefaults(o Options) Options { . . . if o.Send == nil { client := &http.Client{ #1 Transport: &http.Transport{ #2 MaxIdleConnsPerHost: o.Concurrency, #3 }, CheckRedirect: func( _ *http.Request, _ []*http.Request, ) error { return http.ErrUseLastResponse #4 }, Timeout: 30 * time.Second, #5 } o.Send = func(r *http.Request) Result { return Send(client, r) #6 } } return o }
Inspect the complete client configuration that combines Transport pooling, redirect handling, a timeout, and request dispatch. section 7.4.2

