GO language

Go 1.27 Makes HTTP/1 Connection Reuse Automatic, Cutting Request Times

Starting with Go 1.27, the net/http package drains unread HTTP/1 response bodies on its own, so connections can be reused without manual workarounds. A test by the Go team shows a nearly fivefold speedup.

The Go Blog describes a long-standing pitfall in HTTP/1 clients. If a program closes a response body without reading it fully, net/http through Go 1.26 abandons the connection and opens a new one for the next request. Each new connection costs several network round trips for the TCP and TLS handshakes.

The cause is that HTTP/1 sends responses back-to-back on one stream. The client can only find the start of the next response after the current body has been read to the end. Even code that does read the body can be affected: a JSON decoder may stop before a trailing newline, leaving the connection a single byte short of being reusable. HTTP/2 does not have this problem because it multiplexes streams.

Developers have long worked around this by draining the body before closing it, often with limits on size and time to avoid wasted effort. Doing this safely requires fairly intricate code.

In Go 1.27, net/http.Transport handles it automatically. It drains an unread HTTP/1 body in the background for up to 50 ms or 256 KB. It also skips draining when the response carries a Connection: close header, since that work would be wasted. The blog notes this behavior may change in future releases.

In the author's benchmark, 100 requests to index.golang.org over HTTP/1 took about 710 ms on Go 1.26 and about 150 ms on Go 1.27. The recommendation is now simply to close response bodies when done.

Source: Hacker News (front page)


Posted

in

by

Tags: