Better HTTP/1 connection reuse in Go 1.27
Recorded: Oct. 14, 2026, 6 p.m.
| Original | Summarized |
Better HTTP/1 connection reuse - The Go Programming Language Skip to Main Content Why Go arrow_drop_down Press Enter to activate/deactivate dropdown Case Studies Common problems companies solve with Go Use Cases Stories about how and why companies use Go Security How Go can help keep you secure by default Learn Press Enter to activate/deactivate dropdown Docs arrow_drop_down Press Enter to activate/deactivate dropdown Go Spec The official Go language specification Go User Manual A complete introduction to building software with Go Standard library Reference documentation for Go's standard library Release Notes Learn what's new in each Go release Effective Go Tips for writing clear, performant, and idiomatic Go code Packages Press Enter to activate/deactivate dropdown Community arrow_drop_down Press Enter to activate/deactivate dropdown Recorded Talks Videos from prior events Meetups Meet other local Go developers Conferences Learn and network with Go developers from around the world Go blog The Go project's official blog. Go project Get help and stay informed from Go Get connected Why Go navigate_next navigate_beforeWhy Go Case Studies Use Cases Security Learn Docs navigate_next navigate_beforeDocs Go Spec Go User Manual Standard library Release Notes Effective Go Packages Community navigate_next navigate_beforeCommunity Recorded Talks Meetups Conferences Go blog Go project Get connected The Go Blog Nicholas Husin and Damien Neil Take a look at the following function: It sends a GET request to url and returns the HTTP status code. TCP handshake (1 RTT): the client sends a SYN packet, the server These round trips (plus some CPU time spent on TLS handshakes) add up quickly. HTTP/1.0: while not part of the official specification, clients and When connection reuse fails As shown in the diagram, net/http.Get reads and parses Through Go 1.26, when resp.Body.Close is called, net/http func drainAndCloseBody(body io.ReadCloser) { While this works for small responses, if the unread body is large, establishing However, a server might send a response body that trickles in slowly, so we also Of course, we also do not want to block for up to 50 milliseconds when closing a As we can see, draining response bodies safely is surprisingly tricky and Go is all about simplicity. Let your code stay clean and simple, and let Previous article: Arch-specific SIMD in Go Why Go Use Cases Case Studies Get Started Playground Tour Stack Overflow Help Packages Standard Library About Go Packages pkg.go.dev API About Download Blog Issue Tracker Release Notes Brand Guidelines Code of Conduct Connect Bluesky Mastodon Twitter GitHub Slack r/golang Meetup Golang Weekly Opens in new window. Copyright Terms of Service Privacy Policy Report an Issue go.dev uses cookies from Google to deliver and enhance the quality of its services and to |
The efficiency of HTTP communication, particularly concerning connection reuse, is critical for performance, as the initial cost of establishing new connections involves significant network latency and computational overhead, encompassing TCP handshakes and TLS handshakes, which can consume multiple round-trip times. When making multiple requests, avoiding the repeated expenditure of this setup cost through connection reuse is essential. The mechanism for connection reuse varies across HTTP versions; HTTP/1.0 relies on clients and servers using the Connection: keep-alive header, while HTTP/1.1 establishes persistent connections by default, and HTTP/2 allows for the multiplexing of multiple requests and responses over a single TCP connection. Connection reuse in HTTP/1 and HTTP/1.1 can fail if the response body is not fully read before the connection is closed or reused. In HTTP/1, multiple requests are streamed over the same TCP connection. If a response body remains unread, the HTTP client cannot reliably determine where the next response begins in the byte stream, blocking reuse. This issue particularly affects scenarios where the client only needs the status code and ignores the body content, as the mechanism may fail if a newline character remains unconsumed. This failure results in the connection being abandoned, forcing subsequent requests to incur the full cost of establishing a new connection, which leads to silent performance degradation despite seemingly simple operations. Manually managing the draining of response bodies represents a complex workaround. While draining the body before closing it seems straightforward, it is problematic if the body is large, as discarding the remaining data might be slower than establishing a fresh TCP connection. Furthermore, asynchronous draining introduces risks, such as potential goroutine leaks, and requires careful consideration of timing constraints to ensure efficient connection pooling. The Go runtime has addressed this complexity by incorporating intelligent connection management. Starting with Go 1.27, the net/http.Transport automatically handles the draining of unread HTTP/1 response bodies asynchronously, limiting the operation to up to 50 milliseconds or 256 kilobytes. This automatic behavior is enhanced with logic that recognizes connection headers, such as Connection: close, to skip draining if it is determined to be unnecessary. This implementation ensures that the connection pool maximizes reuse by safely managing the state of the underlying TCP connection. Consequently, the general best practice is to rely on the simplicity of closing the response body when no longer needed, allowing the Go standard library to manage the intricacies of safe connection reuse and resource management automatically. |