Go (golang) http calls with retries and backoff
pester wraps Go's standard lib http client to provide several options to increase resiliency in your request. If you experience poor network conditions or requests could experience varied delays, you can now pester the endpoint for data.
Use pester where you would use the http client calls. By default, pester will use a concurrency of 1, and retry the endpoint 3 times with the DefaultBackoff strategy of waiting 1 second between retries.
/* swap in replacement, just switch
http.{Get|Post|PostForm|Head|Do} to
pester.{Get|Post|PostForm|Head|Do}
*/
resp, err := pester.Get("http://sethammons.com")Provide your own backoff strategy, or use one of the provided built in strategies:
DefaultBackoff: 1 secondLinearBackoff: n seconds where n is the retry numberLinearJitterBackoff: n seconds where n is the retry number, +/- 0-33%ExponentialBackoff: n seconds where n is 2^(retry number)ExponentialJitterBackoff: n seconds where n is 2^(retry number), +/- 0-33%client := pester.New()
client.Backoff = func(retry int) time.Duration {
// set up something dynamic or use a look up table
return time.Duration(retry) * time.Minute
}For a complete and working example, see the sample directory.
pester allows you to use a constructor to control:
…pester also allows you to control the resiliency and can optionally log the errors.
…You can run tests in the root directory with $ go test. There is a benchmark-like test available with $ cd benchmarks; go test.
You can see pester in action with $ cd sample; go run main.go.
For watching open file descriptors, you can run watch "lsof -i -P | grep main" if you started the app with go run main.go.
I did this for watching for FD leaks. My method was to alter sample/main.go to only run one case (pester.Get with set backoff stategy, concurrency and retries increased)
and adding a sleep after the result came back. This let me verify if FDs were getting left open when they should have closed. If you know a better way, let me know!
I was able to see that FDs are now closing when they should :)
Are we there yet? Are we there yet? Are we there yet? Are we there yet? ...
No open issues yet, or sync has not completed.