liveBook · Manning

Http.Transport

As explained in Go by Example

http.Transport is a Go HTTP transport implementation that performs request transmission and maintains a pool of reusable network connections.

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

How Go by Example covers it

How deep it goes
Explains Client delegation, RoundTripper, TCP pooling, and MaxIdleConnsPerHost tuning, but defers Transport internals.
Examples
Uses a concurrent HIT client configured with MaxIdleConnsPerHost, redirect prevention, and a thirty-second timeout.
What it assumes
Assumes familiarity with HTTP requests and responses while introducing Client delegation through the RoundTripper interface.
What you can do after
Operate/configure an HTTP client with reusable connections, controlled redirects, and bounded request duration.
How it connects
Connects HTTP clients with RoundTripper, HTTP requests, HTTP responses, TCP connections, redirects, and timeouts.

Also explained in

Compare how 2 books explain http.Transport →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.