Draining http response bodies

877 views
Skip to first unread message

Amnon

unread,
Oct 7, 2021, 3:36:25 AM10/7/21
to golang-nuts
Is it necessary for a http client to fully read the http response body?

Opinion on the Go forums seems divided https://forum.golangbridge.org/t/do-i-need-to-read-the-body-before-close-it/5594

But a simple benchmark https://play.golang.org/p/5JDWYbRe0lD
suggests that leaving unread data in the response body will prevent 
the connection being reused, and much slower performance.

The documentation https://pkg.go.dev/net/http states:

"The client must close the response body when finished with it:"

The following example does include a call to io.ReadAll(resp.Body)
but it does not spell out whether there would be a performance penalty
for failing to read the entire body.

It would be good if the standard library documentation was a bit more explicit here.


Amnon

unread,
Oct 8, 2021, 1:58:18 AM10/8/21
to golang-nuts

David Riley

unread,
Oct 8, 2021, 8:42:36 AM10/8/21
to Amnon, golang-nuts
I suspect a lot of this actually comes down to operating system level handling of TCP sockets, which is something that it would be hard for the docs to address other than maybe offering a warning that your OS *might* not release the port for reuse if the body isn't fully drained.

I could be wrong, though, there's half a chance it's really a GC thing instead. I'd have to look at the source to be sure.


- Dave

Amnon

unread,
Oct 8, 2021, 11:31:55 AM10/8/21
to golang-nuts
Whatever the OS, the crucial question is whether the TCP connections get reused, or whether a new TCP (and potentially TLS) connection has to be negotiated, involving multiple round trips to the server.

Someone has kindly pointed out that the docs do spell out the behaviour - I just missed the comment:

// It is the caller's responsibility to
// close Body. The default HTTP client's Transport may not
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is
// not read to completion and closed.
Reply all
Reply to author
Forward
0 new messages