Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drain Response.Body to enable TCP/TLS connection reuse (4x speedup) #317

Merged
merged 1 commit into from
Mar 27, 2016
Merged

Drain Response.Body to enable TCP/TLS connection reuse (4x speedup) #317

merged 1 commit into from
Mar 27, 2016

Commits on Mar 27, 2016

  1. Drain Response.Body to enable TCP/TLS connection reuse (4x speedup)

    On successful queries (on errors ReadAll runs in CheckResponse), the
    Response.Body 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))
        }
    FiloSottile committed Mar 27, 2016
    Configuration menu
    Copy the full SHA
    e0ff711 View commit details
    Browse the repository at this point in the history