Tags: #go #resilience
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")
}