Skip to content

Instantly share code, notes, and snippets.

@vsel
Created May 27, 2022 09:24
Show Gist options
  • Save vsel/57e7a70036dee634fe6cf3d3c62eef80 to your computer and use it in GitHub Desktop.
Save vsel/57e7a70036dee634fe6cf3d3c62eef80 to your computer and use it in GitHub Desktop.
func retry(
f func() error,
maxRetries int,
backoffTime time.Duration,
factor float64,
) error {
retryAttempt := 1
for {
err := f()
if err != nil {
if retryAttempt >= maxRetries {
if err != nil {
return fmt.Errorf("%w; Max Retries Exceeded", err)
}
}
time.Sleep(time.Duration(math.Pow(factor, float64(retryAttempt-1))) * backoffTime)
retryAttempt++
continue
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment