Go 1.27 Makes HTTP/1 Connections Easier to Reuse
An HTTP client can be correct and still spend much of its time reconnecting. Consider a service that sends a request, checks the status code, and closes the response body. It has obeyed Go’s most visible rule for client responses: close the body. Yet before Go 1.27, that pattern could discard a perfectly healthy HTTP/1 connection on every call. The next call then had to open a new TCP connection and, for HTTPS, negotiate TLS again.
Go’s net/http package now makes a bounded attempt to finish reading an unwanted HTTP/1 response body in the background. If it reaches the end cheaply enough, the connection can return to the idle pool. This is a small API-facing change with a substantial effect on programs that only need response headers or read part of a body. Understanding the wire format explains both why the old behavior existed and why the new behavior has limits.
What reuse saves
Before an HTTPS request can travel on a new connection, the client and server establish TCP and negotiate TLS. Every exchange costs network round trips. The exact cost depends on the TLS version, distance, and connection state, but it can outweigh the time needed to serve a small response. A reused connection skips those setup exchanges for the next request.
The Go team’s example requests the status code from index.golang.org one hundred times with HTTP/2 disabled. On the authors’ machine, it took about 710 ms with Go 1.26 and 150 ms with Go 1.27. That is a demonstration of a particular endpoint, network, and sequential workload, not a promise that every Go client will become five times faster. The mechanism behind the gap is more general: whether a connection is ready for another response after the current one is closed.
HTTP/1.1 normally permits persistent connections. The same TCP stream can carry another request and response after the previous response is complete. HTTP/2 handles concurrency differently: it frames multiple streams on one connection, so an unread body on one stream does not create the same boundary problem for other streams. Go’s new drain behavior specifically targets HTTP/1 responses.
The response boundary is the key
An HTTP/1 response is a status line, headers, a blank line, and possibly a body. When http.Client.Do returns, it has read enough to provide the status and headers, but the body can still be arriving. To parse a later response on the same byte stream, the transport must know where the current body ends. The protocol supplies that boundary through rules such as Content-Length or chunked transfer coding. RFC 9112 explicitly requires a client to read the whole response body if it intends to reuse that persistent connection.
Suppose an endpoint replies with a status code and a short JSON explanation. If the caller only wants StatusCode, closing Body leaves the JSON unread. Through Go 1.26, net/http could not safely put that HTTP/1 connection back in the pool; a later response could otherwise be confused with bytes from the old one. It closed the connection instead. This was a conservative, correct choice, but a costly one when the unread tail was tiny.
This can also surprise code that does parse the body. A JSON decoder stops after a complete JSON value. If the server appends a newline after the closing brace, the decoder can leave that one byte unread. Before this change, even that byte could prevent reuse. Reading exactly one logical value is not always the same as reading the HTTP body to EOF.
Why simply draining everything is a poor fix
One longstanding workaround was to copy the rest of the body to io.Discard before closing it. That is reasonable for a known small response, but dangerous as a general library policy. A server might send a large download when the caller only wanted a status code. Downloading megabytes just to save a TCP handshake wastes bandwidth and can make the request slower. A slow or streaming response might never finish, tying up a goroutine or delaying the caller.
The useful policy needs three properties. It must limit bytes, limit elapsed time, and avoid making Body.Close wait for the drain. It must also give up when reuse is impossible, such as when the peer sends Connection: close. Application code can build this with goroutines, timers, interruption, and careful connection ownership, but it is easy to get wrong. Go moved that decision into net/http.Transport, where the transport already knows the protocol and manages the idle pool.
In Go 1.27, closing an unread HTTP/1 body triggers an asynchronous drain of up to 256 KiB or 50 ms, according to the Go team’s explanation. If the drain reaches the end in time, that connection can be reused. If it does not, the transport abandons it rather than spending unbounded resources. A subsequent request can proceed on another connection while the drain is running; the old connection may become available for a later request if it finishes promptly. The optimization is opportunistic, not a guarantee that the very next request will use the same socket.
The exact limits are an implementation policy described for Go 1.27, not a new requirement of HTTP. The release notes describe the feature more broadly as an automatic, conservative drain. Future Go versions may adjust the policy.
What application code should do
The basic pattern stays simple. Close every response body once it is no longer needed, including non-2xx responses. If the body matters, read and handle it normally. If only the status matters, there is no need to add a generic, unconditional io.Copy(io.Discard, resp.Body) solely to coax the standard transport into reusing an HTTP/1 connection on Go 1.27.
func statusCode(client *http.Client, url string) (int, error) {
resp, err := client.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
The defer is only reached after a successful Get; that matters because a failed request may have no usable response. A long-lived http.Client or Transport also matters: creating a fresh client or disabling keep-alives for every request prevents the pool from providing much value. The transport is safe to share among goroutines, and Go’s default client already shares one.
Do not assume this change makes closing a body optional. A body left open still consumes resources and may prevent the transport from recovering the connection. Nor does the drain turn a deliberately streaming or very large response into a cheap one: once its limits are reached, reuse is forgone. For a client that needs only response metadata from a large endpoint, changing the request or the server contract may be more effective than relying on drainage.
How to check whether it helps your service
First, identify which protocol your client actually negotiates. A client using HTTP/2 for its traffic will not see this particular HTTP/1 improvement. Then look at request patterns that close bodies early: status checks, redirects, partial parsers, and error paths are common candidates. Finally, measure connection creation and latency before and after a Go upgrade using representative traffic rather than repeating the Go team’s benchmark verbatim.
Go exposes connection events through net/http/httptrace. In a focused test, GotConnInfo.Reused can show whether a request received a previously used connection. Correlate that with body sizes, response times, and negotiated protocol. A low reuse rate may also come from server-side Connection: close, idle timeouts, many distinct hosts, or client configuration. The new drain only addresses one cause.
The Go 1.27 release notes mention a rare downside: a program that retains too many idle connections because of an unusual client or transport setup may now keep more of them alive. In such a case, inspect pool settings and client lifetime. DisableKeepAlives can turn reuse off, but it also gives up the setup savings; it is a diagnostic or workload-specific choice, not a default response to the upgrade.
The larger lesson is about ownership. The caller knows when it is finished with the body; the transport knows whether reading the remainder is worthwhile and whether a socket can be reused. Go 1.27 puts the bounded optimization at the layer with the necessary information. For application code, the useful rule is still the straightforward one: read what you need, then close the response body.
Sources: Go team’s explanation, Go 1.27 release notes, Go net/http documentation, HTTP/1.1 specification (RFC 9112), Go httptrace documentation.