Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active June 30, 2022 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/c906c76a2af918e5fb8c575a0dd53ebf to your computer and use it in GitHub Desktop.
Save Integralist/c906c76a2af918e5fb8c575a0dd53ebf to your computer and use it in GitHub Desktop.
[Go retry] #go #golang #retry
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