Jorge Turbica / blog · Blog ← All articles ES · Leer en español
Languages

Go 1.27 retires the drain-resp.Body trick: from 710 to 150 ms over HTTP/1

Go 1.27 drains unread HTTP/1 bodies in the background and returns the connection to the pool. It matters for clients making many requests in a row.

Nearly five times faster: 100 back-to-back HTTP/1 requests drop from ~710 ms on Go 1.26 to ~150 ms on Go 1.27 in the Go team's benchmark. The reason is that Go 1.27 retires something that sat on best-practice lists for years: you no longer need to drain a response body by hand before closing it for the connection to be reused. They explain it on the Go blog on October 14, 2026.

What changes in net/http

In HTTP/1, responses arrive one after another on the same connection, so the client can only reuse it once the current body has been fully read. Up to Go 1.26, if code called resp.Body.Close() without reading everything, net/http dropped the connection and opened a new one, paying for TCP and TLS again: about 3 RTTs with TLS 1.2 (1 for TCP and 2 for TLS), per the article, though with TLS 1.3 the handshake can drop to 1 RTT. HTTP/2 doesn't have this problem because it multiplexes streams.

In Go 1.27, net/http.Transport asynchronously drains the unread HTTP/1 body for up to 50 ms or 256 KB, so it can return the connection to the pool. If the response carries Connection: close, it doesn't try, since that would be wasted work.

The benchmark makes 100 sequential requests forcing HTTP/1 with GODEBUG=http2client=0. According to the authors, Nicholas Husin and Damien Neil, it takes ~710 ms on Go 1.26 and ~150 ms on Go 1.27. It's a single-machine measurement against a single server with no variance: an order of magnitude, not a promise.

Who it affects

The clients that notice most are those speaking HTTP/1 with many requests in a row: calls to internal APIs behind a proxy that doesn't negotiate HTTP/2, probes, scrapers and clients that leave bodies unread on error paths or when doing HEAD and conditional GETs. In those cases every dropped connection means extra latency and more sockets in TIME_WAIT, and on the server side, more TLS handshakes.

It also cleans up code: helpers like io.Copy(io.Discard, resp.Body) with size caps and timeouts are no longer needed, and the article simply recommends defer resp.Body.Close(). A small case fixed along the way: a json.Decoder that left the trailing newline unread used to break reuse.

Two limits the post itself points out. The 256 KB cap exists because with large unread bodies it's cheaper to open a new connection than to drain, and the behavior "may evolve in future releases". And there's no documented GODEBUG to turn it off, so if you're worried about a side effect, your only option is to test it.

How to upgrade

To try it without touching your installation, use Go's version installer:

bash
go install golang.org/dl/go1.27@latest
go1.27 download
go1.27 test ./...

To upgrade a module, go get [email protected] bumps the go directive in go.mod, and with automatic toolchain switching Go downloads the version it needs. In containers, just switch the base image to golang:1.27.

Migration checklist

  1. Measure before upgrading: on a service making many HTTP/1 calls, record new connections per second and client p99 latency (httptrace or your proxy or load balancer metrics).
  2. Upgrade in a test environment and repeat the measurement with the same traffic.
  3. Find your "drain and close" helpers and remove them only if the numbers don't get worse.
  4. Check that every path, including error paths and redirects, closes the body.
  5. Watch CPU and memory: asynchronous draining moves work from the request path to the background.

Sources

Jorge Turbica

Software Engineer and SRE. I write about the real challenges I run into in production.

See portfolio ↗