Retry operations with constant, delays and exponential backoff strategies.
go get github.com/gotidy/retry
Required at least 1.18 version of Go compiler.
delays := retry.Delays[1*time.Second, 2*time.Second, 4*time.Second]
result, err := retry.DoR(ctx, delays, func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
err := retry.Do(ctx, retry.Exponential(time.Second, 1.5, 0.5), func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
result, err := retry.DoR(ctx, retry.TruncatedExponential(time.Second, 1.5, 0, 10*time.Second), func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
There are also other strategies such as Constant, Zero.
If need to prevent retrying wrap error with `Permanent``.
result, err := retry.DoR(ctx, retry.Constant(time.Second), func(ctx context.Context) (int, error) {
result, err := DoSomething()
switch {
case errors.Is(err, ErrInvalidArguments)
return retry.Permanent(err) // Prevent retrying.
default:
return err
}
return result, nil
})