Created
June 1, 2017 18:17
-
-
Save wolfgangmeyers/172b86bbf4737a9cb8b39618ff83cdc3 to your computer and use it in GitHub Desktop.
Golang WithRetries wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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