24

I have a function that just makes a get request to check the status code. It does not read anything from the body. Should I still end the function with resp.Body.Close() ?

Callers should close resp.Body when done reading from it. If resp.Body is not 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.

6
  • From the doc : "The client must close the response body when finished with it:". Reading from it changes nothing to the problem. Sep 3, 2013 at 18:08
  • @dystroy I have updated the question with the godoc paragraph I am refering to Sep 3, 2013 at 18:10
  • Well. I don't see why you're asking then. And why would you want to avoid to close the body ? Sep 3, 2013 at 18:11
  • 2
    Obviously this can simply solved by adding .Close() no matter what. But I want more about the learn the internals. What .Close() does? and when is it necessary? Sep 3, 2013 at 18:12
  • 1
    You must call Close, it is always necessary.
    – Volker
    Sep 3, 2013 at 19:29

1 Answer 1

41

Yes. When you call http.Get, the function returns a response as soon as all the HTTP headers have been read. The body of the response has not been read yet. The Response.Body is a wrapper around the network connection to the server. When you read from it, it downloads the body of the response.

.Close() tells the system that you're done with the network connection. If you have not read the response body, the default http transport closes the connection. (The transport can only re-use the connection if the body has been read, because if it reused a connection with an unread body the next request made using that connection would receive the previous request's response!)

So reading the Body is often more efficient than simply Close()ing if you're making more than one request - especially with TLS connections which are relatively expensive to create.

If you don't need the body of the response, you should use Head instead of Get. Head doesn't require reading or closing the response body.

5
  • 1
    "So reading the Body is often more efficient than simply Close()ing if you're making more than one request", @andybalholm. Could you further elaborate on this? :)
    – Roy Lee
    Jan 21, 2016 at 8:41
  • Another enquiry :) I'd made a contrive example: play.golang.org/p/TlNSuGCfi3. *Notice it will fail in goplayground as http protocol is not supported, but when I run on my machine, each response maps correctly with its url, not as what you described above, that same connection would receive the previous requests response. Am I doing it correctly ?
    – Roy Lee
    Jan 21, 2016 at 8:53
  • 4
    @Roylee, this is about when all the requests are to the same server (e.g. https://golang.org and https://golang.org/doc/). If the response bodies aren't closed at all, each request will be sent on a separate TCP connection, and neither connection would ever be closed. If the bodies are closed without reading them, each request Dials and Closes its own TCP connection. But if you read and close the body, the second request will probably be sent on the same connection as the first, saving the time that would be used for the TCP and TLS handshakes on the second connection. Jan 21, 2016 at 15:54
  • 1
    @Roylee, the part about getting the previous request's response was talking about what would happen if the transport reused the underlying connection with an unread body, not what would happen if you never called Close. I hope my edit makes it clearer. Jan 21, 2016 at 16:01
  • Oh yes indeed! Thank you so much for your time to reply. It clears my doubts completely, I understand now! :)
    – Roy Lee
    Jan 22, 2016 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.