Retryable HTTP Client In Go
Simple HTTP client interface with automatic retries and constant backoff. Inspired by HashiCorp's go-retryablehttp library.
NewClient() creates and returns a retryable HTTP client instance with provided options.
c, err := retryablehttp.NewClient(
retryablehttp.WithHTTPClient(http.DefaultClient),
retryablehttp.WithMaxReqCount(3),
retryablehttp.WithBackoff(100*time.Millisecond),
retryablehttp.WithResHandler(func(res *http.Response) error {
if res == nil {
return ErrNilRes
}
statusCode := res.StatusCode
if statusCode < 200 || statusCode > 299 {
return ErrUnsuccessfulStatusCode
}
return nil
}),
)
WithHTTPClient option configures underlying http client.
WithMaxReqCount option configures maximum request count.
WithBackoff option configures backoff duration which represents sleeping intervals between requests.
WithResponseHandler option configures response handler which handles responses.
Client has Do(*http.Request) (*http.Response, error)
function which is identical to *http.Client
. This makes retryable http client broadly applicable with minimal effort.
res, err := c.Do(req)
Any contribution or feedback is welcome.