File size: 993 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package retry
import (
"context"
"time"
)
// Wrap will wrap a single-argument function (excl. context) with retry logic.
func Wrap[T, A any](fn func(context.Context, A) (T, error), attempts int, backoff time.Duration) func(context.Context, A) (T, error) {
return func(ctx context.Context, a A) (T, error) {
var res T
var err error
err = DoTemporaryError(func(int) error {
res, err = fn(ctx, a)
return err
},
Log(ctx),
Limit(attempts),
FibBackoff(backoff),
)
return res, err
}
}
// Wrap2 will wrap a two-argument function (excl. context) with retry logic.
func Wrap2[T, A, B any](fn func(context.Context, A, B) (T, error), attempts int, backoff time.Duration) func(context.Context, A, B) (T, error) {
return func(ctx context.Context, a A, b B) (T, error) {
var res T
var err error
err = DoTemporaryError(func(int) error {
res, err = fn(ctx, a, b)
return err
},
Log(ctx),
Limit(attempts),
FibBackoff(backoff),
)
return res, err
}
}
|