hashicorp / go-retryablehttp

Retryable HTTP client in Go
Mozilla Public License 2.0
1.99k stars 251 forks source link

How can I retry based on the error client.Do() throws ? #160

Open rew1nter opened 2 years ago

rew1nter commented 2 years ago

For example, I wanna catch TCP dial Temporary failure in resolution error which is thrown by client.Do() and then retry

SimonRichardson commented 2 years ago

You need to override the https://github.com/hashicorp/go-retryablehttp/blob/master/client.go#L343-L351= using https://github.com/hashicorp/go-retryablehttp/blob/master/client.go#L404=

client := retryablehttp.NewClient()
client.CheckRetry = (ctx context.Context, resp *http.Response, err error) (bool, error) {
    // do not retry on context.Canceled or context.DeadlineExceeded
    if ctx.Err() != nil {
        return false, ctx.Err()
    }

    // Do your retry logic here...

    return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}