Skip to content

Instantly share code, notes, and snippets.

@wolfgangmeyers
Created June 1, 2017 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfgangmeyers/172b86bbf4737a9cb8b39618ff83cdc3 to your computer and use it in GitHub Desktop.
Save wolfgangmeyers/172b86bbf4737a9cb8b39618ff83cdc3 to your computer and use it in GitHub Desktop.
Golang WithRetries wrapper
package retries
import "time"
// WithRetries will continue to attempt an operation
// up to the specified number of retries with an exponential
// backoff. If the operation is still unsuccessful
// after all retries are exhausted (operation returns an error),
// that error is returned.
func WithRetries(retries int, operation func() error) error {
err := operation()
backoff := time.Second * 2
for err != nil && retries > 0 {
time.Sleep(backoff)
retries--
backoff *= 2
err = operation()
}
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment