]> Cypherpunks repositories - gostls13.git/commit
net/http: try to drain response body upon closing for better connection re-use
authorNicholas S. Husin <nsh@golang.org>
Thu, 22 Jan 2026 03:46:21 +0000 (22:46 -0500)
committerNicholas Husin <nsh@golang.org>
Mon, 2 Feb 2026 17:54:16 +0000 (09:54 -0800)
commit11d5284363ed88b8cc8ec6e68db80a16b2f9e708
treed5ecdce5042c715d8ae460f845844e06378a532b
parentd99be5c44449a3a40a62942272e99642962a37d9
net/http: try to drain response body upon closing for better connection re-use

Currently, we have a rather inconsistent behavior in terms of whether a
connection can be re-used or not when an HTTP body is not read to
completion:

- In HTTP/2, not reading bodies to completion is not an issue, since a
  new HTTP/2 stream can be created on the same TCP connection.
- In HTTP/1 server, we discard up to 256 KiB of unconsumed request body,
  to potentially allow re-use.
- In HTTP/1 client, we do not do anything, and fail to re-use a TCP
  connection if there are any unconsumed response body at all.

This has led to some confusion. For example, some users have mistakenly
discarded response body for HTTP/2 when doing so is not needed. Manually
discarding response body can also be disadvantageous if the body is
excessively large or is a never-ending stream.

To solve this issue, this CL makes it so that closing a response body
will cause any remaining content to be drained, up to a limit of 256 KiB
or 50 milliseconds, whichever one is reached first. This allows better
connection re-use for HTTP/1, and most users can now avoid having to
manually drain their response body.

For #77370

Change-Id: I71e1227fc9cf5f901362c8e234320817f6b0be24
Reviewed-on: https://go-review.googlesource.com/c/go/+/737720
Reviewed-by: Nicholas Husin <husin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
src/net/http/export_test.go
src/net/http/transport.go
src/net/http/transport_test.go