# Do i need to read the body before close it?

**URL:** <https://forum.golangbridge.org/t/do-i-need-to-read-the-body-before-close-it/5594>\
**Category:** Getting Help\
**Created:** [June 4, 2017, 9:37pm UTC](https://forum.golangbridge.org/t/do-i-need-to-read-the-body-before-close-it/5594 "2017-06-04T21:37:28Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![diego.bernardes](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/d/a88e4f/32.png) [@diego.bernardes](https://forum.golangbridge.org/u/diego.bernardes)\
**Post date:** [June 5, 2017, 1:08pm UTC](https://forum.golangbridge.org/t/do-i-need-to-read-the-body-before-close-it/5594/14 "2017-06-05T13:08:34Z")

</div>

Found some issues on github about this problem, this on is the most similar:

> <https://github.com/google/go-github/pull/317>
>
> On successful queries (on errors ReadAll runs in CheckResponse), the
> Response.Bo…dy is not read all the way to the EOF as json.Decoder is used,
> which stops at the end of the object, which is probably followed by a \\n.
> 
> Then, Response.Body.Close() is called. When that happens with a non-drained
> Body, the TCP/TLS connection is closed (which makes sense, in case there was
> still a lot to read from the network), stopping the Transport from reusing
> it. Also, a library user can't drain the Body themselves because Close() is
> called before passing the Response to the user.
> 
> Since we know GitHub API responses only carry a single JSON object, draining
> before closing is free, and saving all those handshakes causes huge savings
> in latency, bandwidth and CPU for both the client and the poor GitHub
> servers.
> 
> Here's the result of running the benchmark below on a ADSL connection:
> 
> \`\`\`
> before: 2m3.001599093s
> after: 33.753543928s
> 
> func main() {
> ts := oauth2.StaticTokenSource(
> &oauth2.Token{AccessToken: os.Getenv("GITHUB\_TOKEN")},
> )
> tc := oauth2.NewClient(oauth2.NoContext, ts)
> gh := github.NewClient(tc)
> start := time.Now()
> for i := 0; i \< 100; i++ {
> gh.Repositories.Get("FiloSottile", "gvt")
> fmt.Print(".")
> }
> fmt.Printf("\\n%s\\n", time.Now().Sub(start))
> }
> \`\`\`

Seems the problem is related to readers not doing the read with (n, io.EOF).

> **bradfitz:** As background, Go used to auto-drain (around Go 1.0 and Go 1.1 era) but it was problematic for a number of reasons and we changed the behavior to be more predictable but also pushed io.EOFs earlier for many readers and made the final Read-with-EOF on the connection also auto-recycle the TCP connection for people who forget to Close. It seems there’s a Reader not doing early (n, io.EOF) instead of (n, nil)+(0, io.EOF) somewhere.

This [CL](https://go-review.googlesource.com/c/21290) has a nice information about the readers in the std lib.

> net/http and other things work better when io.Reader implementations  
> return (n, io.EOF) at the end, instead of (n, nil) followed by (0,  
> io.EOF). **Both are legal, but the standard library has been moving**  
> **towards n+io.EOF.**

In the snippet, this behaviour is not present, i think, but in my app, they may be, need to investigate more.

Thanks for your help guys.

---

_[View the full topic](https://forum.golangbridge.org/t/do-i-need-to-read-the-body-before-close-it/5594)._
