Created
November 22, 2024 15:29
-
-
Save yinkakun/ef59d0f405c436d280d75bf8137a5874 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(maxRetry int, interval time.Duration, f func() error) error { | |
var error error | |
for attempt := 0; attempt < maxRetry; attempt++ { | |
if err := f(); err != nil { | |
error = err | |
if attempt < maxRetry-1 { | |
time.Sleep(interval) | |
} | |
continue | |
} | |
return nil | |
} | |
return fmt.Errorf("operation failed after %d attempts: %w", maxRetry, error) | |
} |
Author
yinkakun
commented
Nov 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment