Created
May 27, 2022 09:24
-
-
Save vsel/57e7a70036dee634fe6cf3d3c62eef80 to your computer and use it in GitHub Desktop.
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
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