Skip to content

Instantly share code, notes, and snippets.

@arschles
Last active January 13, 2023 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arschles/b6e216fe99dd2d137e93986e86430678 to your computer and use it in GitHub Desktop.
Save arschles/b6e216fe99dd2d137e93986e86430678 to your computer and use it in GitHub Desktop.
Tuning an HTTP Client for Great Good!

Other Go Libraries to Help With Doing HTTP Clients

  • ctxhttp - use HTTP clients but do requests timeouts with a context
    • httptrace - a good library to pair with ctxhttp; you can use this to trace requests up to the server. this is pretty advanced usage, so I recommend you go here once you have basic performance issues sorted out
  • fasthttp - slightly easier-to-use HTTP client. you still have to deal with connection pool details though. still useful if you like the API better
  • go-cleanhttp - battle-tested HTTP client. useful to compare against the above code
  • gorequest - nicer API, not as geared toward load testing. recommend against this for load tests, but for business logic it's good

Tips for Load Testing in Kubernetes

  • use a dedicated client just for load testing, so it has its own conn pool
  • 3rd party libraries can make it hard or impossible to do that, so the standard library is more reliable when you load test, even though the API is harder to deal with
  • if making requests through a kube service, evict connections out of the pool faster (IdleConnTimeout), so you start talking to the new pods faster
// this is how to create a scalable HTTP client using the standard library
transport := &http.Transport{
MaxIdleConns: 10, // global number of idle conns
MaxIdleConnsPerHost: 5, // subset of MaxIdleConns, per-host
// declare a conn idle after 10 seconds. too low and conns are recycled too much, too high and conns aren't recycled enough
IdleConnTimeout: 10 * time.Second,
// DisableKeepAlives: true, // this means create a new connection per request. not recommended
}
cl := &http.Client{
Transport: transport,
Timeout: 2 * time.Second,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment