|
| 1 | +package connection |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/cenkalti/backoff/v4" |
| 7 | +) |
| 8 | + |
| 9 | +type PermanentError struct { |
| 10 | + Inner error |
| 11 | +} |
| 12 | + |
| 13 | +func (e PermanentError) Error() string { return e.Inner.Error() } |
| 14 | +func (e PermanentError) Unwrap() error { |
| 15 | + return e.Inner |
| 16 | +} |
| 17 | +func (e PermanentError) Is(err error) bool { |
| 18 | + _, ok := err.(PermanentError) |
| 19 | + return ok |
| 20 | +} |
| 21 | + |
| 22 | +// Same as Retry only that the functionToRetry can return a value upon correct execution |
| 23 | +func RetryWithData[T any](functionToRetry func() (*T, error), minDelay uint64, factor float64, maxTries uint64) (*T, error) { |
| 24 | + i := 0 |
| 25 | + f := func() (*T, error) { |
| 26 | + val, err := functionToRetry() |
| 27 | + i++ |
| 28 | + if perm, ok := err.(PermanentError); err != nil && ok { |
| 29 | + return nil, backoff.Permanent(perm.Inner) |
| 30 | + } |
| 31 | + return val, err |
| 32 | + } |
| 33 | + |
| 34 | + randomOption := backoff.WithRandomizationFactor(0) |
| 35 | + |
| 36 | + initialRetryOption := backoff.WithInitialInterval(time.Millisecond * time.Duration(minDelay)) |
| 37 | + multiplierOption := backoff.WithMultiplier(factor) |
| 38 | + expBackoff := backoff.NewExponentialBackOff(randomOption, multiplierOption, initialRetryOption) |
| 39 | + var maxRetriesBackoff backoff.BackOff |
| 40 | + |
| 41 | + if maxTries > 0 { |
| 42 | + maxRetriesBackoff = backoff.WithMaxRetries(expBackoff, maxTries) |
| 43 | + } else { |
| 44 | + maxRetriesBackoff = expBackoff |
| 45 | + } |
| 46 | + |
| 47 | + return backoff.RetryWithData(f, maxRetriesBackoff) |
| 48 | +} |
| 49 | + |
| 50 | +// Retries a given function in an exponential backoff manner. |
| 51 | +// It will retry calling the function while it returns an error, until the max retries. |
| 52 | +// If maxTries == 0 then the retry function will run indefinitely until success |
| 53 | +// from the configuration are reached, or until a `PermanentError` is returned. |
| 54 | +// The function to be retried should return `PermanentError` when the condition for stop retrying |
| 55 | +// is met. |
| 56 | +func Retry(functionToRetry func() error, minDelay uint64, factor float64, maxTries uint64) error { |
| 57 | + i := 0 |
| 58 | + f := func() error { |
| 59 | + err := functionToRetry() |
| 60 | + i++ |
| 61 | + if perm, ok := err.(PermanentError); err != nil && ok { |
| 62 | + return backoff.Permanent(perm.Inner) |
| 63 | + } |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + randomOption := backoff.WithRandomizationFactor(0) |
| 68 | + |
| 69 | + initialRetryOption := backoff.WithInitialInterval(time.Millisecond * time.Duration(minDelay)) |
| 70 | + multiplierOption := backoff.WithMultiplier(factor) |
| 71 | + expBackoff := backoff.NewExponentialBackOff(randomOption, multiplierOption, initialRetryOption) |
| 72 | + maxRetriesBackoff := backoff.WithMaxRetries(expBackoff, maxTries) |
| 73 | + |
| 74 | + return backoff.Retry(f, maxRetriesBackoff) |
| 75 | +} |
0 commit comments