Arash Taher

Closing Responses in Go lang is not enough

I just published my side project in Go as open source: Pensive, a full-text searchable bookmarking service.

It lets you save articles and search inside their content, not just titles.

Repo: https://github.com/arashThr/pensive


Did you know closing the Response body is not enough to reuse the connection and you also need to read the body?

We had some issue regarding our HTTP requests, and after some investigations I found out about this in Response docs:

If the Body is not both read to EOF and closed, the Client’s underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent “keep-alive” request.

Meaning you’ll have a bunch of sockets in the TIME_WAIT state. Subsequently, in HTTP/2 this can case cause connection issues due to flow control.

Also in Go code you can see that reading the body is required even if you close the body:

// The http Client and Transport guarantee that Body is always
// non-nil, even on responses without a body or responses with
// a zero-length body. 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.

Here’s a nice blog post about this issue with a good example to reproduce the problem.

Tagged: #Golang #Http #Programming #Tips

Subscribe to Newsletter