import "github.com/ic-it/retrygo"
- type ErrRecovered
- type Retry
- func New[T any](policy RetryPolicy, options ...RetryOption[T]) (Retry[T], error)
- func NewZero(policy RetryPolicy, options ...RetryOption[zero]) (Retry[zero], error)
- func (r Retry[T]) Do(ctx context.Context, f func(context.Context) (T, error)) (T, error)
- func (r Retry[T]) DoZero(ctx context.Context, f func(context.Context) error) error
- type RetryInfo
- type RetryOption
- type RetryPolicy
- func Combine(policies ...RetryPolicy) RetryPolicy
- func Constant(interval time.Duration) RetryPolicy
- func Exponential(interval time.Duration) RetryPolicy
- func Jitter(interval time.Duration) RetryPolicy
- func LimitCount(count int) RetryPolicy
- func LimitTime(limit time.Duration) RetryPolicy
- func Linear(interval time.Duration) RetryPolicy
ErrRecovered is returned when a panic is recovered.
type ErrRecovered struct{ V any }
func (e ErrRecovered) Error() string
Retry is the main type of this package.
type Retry[T any] struct {
// contains filtered or unexported fields
}
func New[T any](policy RetryPolicy, options ...RetryOption[T]) (Retry[T], error)
New creates a new Retry instance with the given RetryPolicy and RetryOptions.
func NewZero(policy RetryPolicy, options ...RetryOption[zero]) (Retry[zero], error)
NewZero creates a new Retry instance with no return value.
func (r Retry[T]) Do(ctx context.Context, f func(context.Context) (T, error)) (T, error)
Do calls the given function f until it returns nil error or the context is done.
func (r Retry[T]) DoZero(ctx context.Context, f func(context.Context) error) error
DoZero calls the given function f until it returns nil error or the context is done.
RetryInfo contains information about the retry
type RetryInfo struct {
Fails int // Fails is the number of retries
Err error // Err is the error returned by the function
Since time.Time // Since is the time when the retry started
}
type RetryOption[T any] func(*Retry[T])
type RetryOption[T any] interface {
// contains filtered or unexported methods
}
func WithRecovery[T any]() RetryOption[T]
WithRecovery enables the recovery mode.
RetryPolicy is a function that returns a retry strategy based on the RetryInfo
type RetryPolicy func(RetryInfo) (continueRetry bool, sleep time.Duration)
func Combine(policies ...RetryPolicy) RetryPolicy
Combine returns a RetryPolicy that combines multiple RetryPolicies. The function will return true if all the policies return true. (logical AND) If all the policies return true, the function will return the sum of the sleep durations.
Sleep formula: sleep1 + sleep2 + ...
func Constant(interval time.Duration) RetryPolicy
Constant returns a RetryPolicy that always returns the same interval between retries.
Sleep formula: interval
func Exponential(interval time.Duration) RetryPolicy
Exponential returns a RetryPolicy that increases the interval between retries exponentially.
Sleep formula: interval * 2^fails
func Jitter(interval time.Duration) RetryPolicy
Jitter returns a RetryPolicy that adds a random non-negative jitter to the interval between retries.
Sleep formula: interval + rand.Int63n(interval)
func LimitCount(count int) RetryPolicy
LimitCount returns a RetryPolicy that limits the number of retries.
Sleep formula: 0
func LimitTime(limit time.Duration) RetryPolicy
LimitTime returns a RetryPolicy that limits the total time spent on retries.
Sleep formula: 0
WARNING: Use context.WithTimeout instead of this function if you can!
func Linear(interval time.Duration) RetryPolicy
Linear returns a RetryPolicy that increases the interval between retries linearly.
Sleep formula: interval * fails
Generated by gomarkdoc