- 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
Last active
January 13, 2023 15:44
-
-
Save arschles/b6e216fe99dd2d137e93986e86430678 to your computer and use it in GitHub Desktop.
Tuning an HTTP Client for Great Good!
- 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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