Go 1.27: How Background resp.Body Draining Speeds Up HTTP/1 FivefoldArticle · digest for October 21
Network trace and Go 1.27 buffer showing background draining of an HTTP response tail back into the idle pool
Article3 sections≈ 5 min

Go 1.27: How Background resp.Body Draining Speeds Up HTTP/1 Fivefold

You have probably written this familiar pattern in your Go services countless times: call http.Get, check the error, and immediately slap on defer resp.Body.Close(). It looks idiomatic, linters love it, and the tests are green. Yet if your client talks to a downstream service over HTTP/1.1 under the hood, that innocent-looking pattern has been quietly wrecking your network performance for years.

I used to run into this exact headache myself, wondering why a production service was choking on hundreds of repeated TLS handshakes even though the connection pool was tuned with plenty of headroom. In Go 1.27, Go core team engineers Nicholas Husin and Damien Neil finally shipped an elegant fix for a problem that had haunted the community for over a decade.

The One-Byte Trap: Why Sockets Got Marked Dirty

To see what the fuss is about, let's peek at how HTTP/1 works on the wire. Unlike HTTP/2 or HTTP/3, where streams are multiplexed inside a single session using frame IDs, HTTP/1 communicates strictly sequentially as one continuous stream of bytes. The client sends a request; the server sends a response. If the client wants to reuse that same TCP connection for the next request (keep-alive), the transport has to be 100% certain that the current response was read down to the very last byte.

Up through Go 1.26, net/http.Transport played it ultra-safe. If an application called resp.Body.Close() before reading the body all the way to io.EOF, the socket was flagged as "dirty." The transport immediately tore down the TCP connection and evicted the socket from the pool. The very next request then had to pay the full penalty: one Round-Trip Time (RTT) for the TCP handshake, followed by another one or two RTTs to establish a fresh TLS tunnel.

The sneakiest part is how easily you could trigger this drop by accident. A textbook example is streaming JSON decoding with the standard json.NewDecoder:

func fetchUserProfile(resp *http.Response) (*UserProfile, error) {
    defer resp.Body.Close()

    var profile UserProfile
    // NewDecoder stops right at the closing curly brace "}"
    if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
        return nil, err
    }
    // If the server sent a trailing newline "\n", the socket was torn down!
    return &profile, nil
}

json.NewDecoder dutifully decodes the object and stops at the final } character. Meanwhile, servers almost always append a trailing newline \n. That leaves a single unread byte sitting in the socket buffer. For the Go runtime, that one byte was enough: the socket was declared tainted, the session was destroyed, and throughput tanked.

Asynchronous Draining: How the Go 1.27 Fix Works

Previously, developers had to jump through hoops—manually draining leftovers with io.Copy(io.Discard, resp.Body) or rigging up custom wrappers with timers and goroutines so they wouldn't hang on an unexpectedly huge response.

In Go 1.27, net/http takes that job off your hands. Now, when you call resp.Body.Close(), the transport automatically spawns a background goroutine that neatly drains the "tail" of the response behind the scenes:

// In Go 1.27, the standard call is now safe for the connection pool
func checkHealth(client *http.Client, url string) (bool, error) {
    resp, err := client.Get(url)
    if err != nil {
        return false, err
    }
    // The transport drains the remainder in the background and returns the socket to the idle pool
    defer resp.Body.Close()

    return resp.StatusCode == http.StatusOK, nil
}

Background draining comes with two strict circuit breakers:

  • Volume limit: at most 256 KiB of data. If a server tries to feed the client megabytes of unread body, the transport won't blow up memory and will cleanly close the connection.
  • Time limit: at most 50 milliseconds. If the connection is lagging and packets are barely trickling in, the socket is closed rather than tying up resources.
  • Respecting the server: if the server explicitly sends a Connection: close header, background draining doesn't even start.

Benchmark Numbers and Takeaways

The authors' benchmark results speak for themselves. In a synthetic test of 100 sequential requests to the index.golang.org checksum service over HTTP/1, total execution time dropped from 710 milliseconds on Go 1.26 to 150 milliseconds on Go 1.27—nearly a fivefold speedup!

We already dug into this network quirk when looking at an earlier deep dive into clean Go sockets. Now that Go 1.27 is officially out, you no longer need io.Discard workarounds: upgrade your runtime, keep your standard defer resp.Body.Close(), and enjoy a free network-stack boost out of the box.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 2