IT

Go 1.27 Speeds Up HTTP/1 Clients with Automatic Body Draining

Go 1.27 automatically drains unread HTTP/1 response bodies, so connections get reused and repeated requests run much faster.

15 October 2026  ·  4 min read  ·  Anand

Go 1.27 Speeds Up HTTP/1 Clients with Automatic Body Draining

Go 1.27 changes how the standard library HTTP client handles HTTP/1 responses whose body you never read. Before this release, an unread body made the client discard the connection and open a new one for the next request, paying for a fresh TCP and TLS handshake every time. The client now drains a small amount of the body for you in the background, so the connection goes back into the pool. In the Go team’s own test, a loop of status-only requests ran about five times faster, with no code changes at all. Services that call webhooks, health endpoints or chatty internal APIs over HTTP/1 stand to gain the most.

The hidden cost of an unread response body

On an HTTP/1 connection, responses arrive one after another. The client can only read the next response once the current body has been read to its end, as defined by Content-Length or the chunked transfer encoding. If your code closes a body it has not finished reading, the Transport cannot tell where the next response starts. It throws the connection away and opens a new one, which costs several network round trips.

This function, which only wants the status code, used to trigger exactly that:

func getStatus(url string) (int, error) {
    resp, err := http.Get(url)
    if err != nil {
        return 0, err
    }
    defer resp.Body.Close()
    return resp.StatusCode, nil
}

The Go team points to two common ways this happens in real code:

  • Ignored bodies: an API returns a JSON message such as a success note that the caller has no use for.
  • Partial reads: encoding/json.Decoder stops at the closing brace, leaving a trailing newline unread. One unread byte is enough to lose the connection.

HTTP/2 never had this problem, because it multiplexes many streams over one connection instead of sending responses strictly in order.

What Go 1.27 changes

In Go 1.27, net/http.Transport drains an unread HTTP/1 body for you, asynchronously, so closing the body does not block your code. To avoid wasting effort on large or slow responses, draining is capped at 50 milliseconds or 256 KB, whichever comes first. If the server has sent a Connection: close header, the Transport skips draining entirely, since the connection cannot be reused anyway. The Go team notes that these limits are an implementation detail and may change in future releases, so do not build logic that depends on the exact numbers.

How big is the difference?

The blog post benchmarks the status-only function above, calling it 100 times against index.golang.org with HTTP/2 disabled through GODEBUG=http2client=0:

Version Time for 100 requests
Go 1.26 about 710 ms
Go 1.27 about 150 ms

These are single-machine measurements, so your numbers will vary. The gain grows with handshake cost: servers far away, TLS-heavy traffic and upstreams with slow connection setup benefit most. It also means fewer sockets stuck in TIME_WAIT and less handshake load on the servers you call.

What to do in your code

On Go 1.27 and later

Keep doing the simple, correct thing: always close response bodies, usually with defer resp.Body.Close(). You no longer need hand-written drain helpers, and over time you can simplify them away. Closing the body is still essential, because an unclosed body leaks the connection no matter which Go version you use.

If you still support older Go versions

Libraries and services that must build on Go 1.26 or earlier should keep draining by hand, with limits on both bytes and time so a large or slow body cannot stall the caller. A minimal byte-limited version looks like this:

defer func() {
    io.CopyN(io.Discard, resp.Body, 256<<10)
    resp.Body.Close()
}()

If latency matters, move the drain into a goroutine so it stays off the request path, which is essentially what Go 1.27 now does for you.

Rolling out the new toolchain

The improvement lives in the standard library, so you get it when you build with the Go 1.27 toolchain. Not by changing code. A practical checklist:

  • Run go version locally and in CI to confirm which toolchain builds your binaries.
  • Update the golang base image tags in your Dockerfiles and rebuild.
  • Run your test suite, then deploy to one instance first.
  • Compare outbound connection counts before and after on the host, for example with the commands below.
ss -tan state established '( dport = :443 )' | wc -l
ss -tan state time-wait | wc -l

Fewer new connections and a smaller TIME_WAIT count to the same upstreams show that reuse is working.

How TechProvidence can help

TechProvidence supports development teams with build pipelines, container images and the Linux servers their Go services run on. We can update your toolchains and CI images, roll out rebuilt services safely, and measure connection behaviour and latency before and after the change. If you want help planning an upgrade, contact us.

Source: The Go Blog

Running into something similar?

We look after Linux servers, control panels and virtualization for businesses every day. If an update, vulnerability or outage here affects you, we can help.