Author Archives: Matthew Cottingham
Author Archives: Matthew Cottingham
It's well known that we're heavy users of the Go programming language at CloudFlare. Our work often involves delving into the standard library source code to understand internal code paths, error handling and performance characteristics.
Recently, I looked at how the standard library's built-in HTTP client handles connections to remote servers in order to provide minimal roundtrip latency.
CC By 2.0 Image by Dean Hochman
A common pattern that aims to avoid connection setup costs (such as the TCP handshake and TLS setup) and confer control over the number of concurrently established connections is to pool them. net/http
maintains a pool of connections to each remote host which supports Connection: keep-alive
. The default size of the pool is two idle connections per remote host.
More interestingly, when you make a request with net/http
, a race happens. Races in code are often an unwanted side effect, but in this case it's intentional. Two goroutines operate in parallel: one that tries to dial a connection to the remote host, and another which tries to retrieve an idle connection from the connection pool. The fastest goroutine wins.
To illustrate, let's look at the code executed when transport.RoundTrip(req)
is Continue reading