go-retry
is a package that helps facilitate retry logic with jittered
exponential backoff. It provides a convenient interface for configuring various
parameters. See below for more information.
func makeNetworkCall(ctx context.Context) {
defaultBackoff := retry.DefaultBackoff()
// try at most 5 times
getErr := retry.Retry(ctx, defaultBackoff, 5, func(ctx context.Context) error {
response, err := http.Get("https://my.favorite.service")
if err != nil {
return err
}
// do something with response...
})
if getErr != nil {
// get failed, even after all the retries
}
}
Copyright Vimeo.