Skip to content

Instantly share code, notes, and snippets.

@reetasingh
Last active January 5, 2023 22:04
Show Gist options
  • Save reetasingh/5e29082a834491c638373a0209eb9ba3 to your computer and use it in GitHub Desktop.
Save reetasingh/5e29082a834491c638373a0209eb9ba3 to your computer and use it in GitHub Desktop.
func Retry(ctx context.Context, f RetryFunc, retryAttempts int) error {
var err error
for i := 0; i < retryAttempts; i++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := f()
if err == nil {
return nil
}
}
return err
}
func main() {
reader := Reader{}
Retry(context.Background(), reader.Read, 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment