Last active
June 30, 2022 11:06
-
-
Save Integralist/c906c76a2af918e5fb8c575a0dd53ebf to your computer and use it in GitHub Desktop.
[Go retry] #go #golang #retry
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 main | |
import ( | |
"context" | |
"fmt" | |
"time" | |
"github.com/sethvargo/go-retry" | |
) | |
func main() { | |
ctx := context.Background() | |
b := retry.NewConstant(1 * time.Second) | |
b = retry.WithMaxRetries(3, b) | |
count := 0 | |
if err := retry.Do(ctx, b, func(_ context.Context) error { | |
count++ | |
if count < 5 { | |
fmt.Println("error to retry") | |
return retry.RetryableError(fmt.Errorf("whoops")) | |
} | |
return nil | |
}); err != nil { | |
fmt.Println("error happened:", err) | |
return | |
} | |
fmt.Println("success") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment