Documentation
¶
Index ¶
- Variables
- type Errors
- type Hooks
- type Option
- type Registry
- func (r *Registry) Clean()
- func (r *Registry) IsTemporaryError(err error, errs ...error) bool
- func (r *Registry) Len() int
- func (r *Registry) ListTemporaryErrors() []error
- func (r *Registry) LoadDefaults() *Registry
- func (r *Registry) RegisterTemporaryError(err error)
- func (r *Registry) RegisterTemporaryErrors(errs ...error)
- func (r *Registry) UnRegisterTemporaryError(err error)
- func (r *Registry) UnRegisterTemporaryErrors(errs ...error)
- type Retrier
- func (r *Retrier) Cancel()
- func (r *Retrier) Do(ctx context.Context, retryableFunc RetryableFunc, temporaryErrors ...error) (errs *Errors)
- func (r *Retrier) DoWithContext(ctx context.Context, retryableFunc RetryableFuncWithContext, ...) (errs *Errors)
- func (r *Retrier) PutErrors(errs *Errors)
- func (r *Retrier) SetRegistry(reg *Registry) error
- func (r *Retrier) Stop()
- func (r *Retrier) Validate() error
- type RetryableFunc
- type RetryableFuncWithContext
- type TimerPool
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidRetrier is the error returned when the retrier is invalid. ErrInvalidRetrier = ewrap.New("invalid retrier") // ErrMaxRetriesReached is the error returned when the maximum number of retries is reached. ErrMaxRetriesReached = ewrap.New("maximum number of retries reached") // ErrTimeoutReached is the error returned when the timeout is reached. ErrTimeoutReached = ewrap.New("operation timeout reached") // ErrOperationStopped is the error returned when the retry is stopped. ErrOperationStopped = ewrap.New("operation stopped") // ErrNilRetryableFunc is the error returned when the retryable function is nil. ErrNilRetryableFunc = ewrap.New("failed to invoke the function. It appears to be nil") )
var ErrOperationFailed = ewrap.New("failed")
ErrOperationFailed is the error returned when the operation fails.
Functions ¶
This section is empty.
Types ¶
type Errors ¶ added in v1.0.7
type Errors struct {
// Attempts holds the trace of each attempt in order.
Attempts []error
// Last holds the last error returned by the retry function.
Last error
}
Errors holds the error returned by the retry function along with the trace of each attempt.
func DoWithResult ¶ added in v1.1.2
func DoWithResult[T any](ctx context.Context, r *Retrier, fn func() (T, error), temporaryErrors ...error) (T, *Errors)
DoWithResult retries a function that returns a result and an error.
type Hooks ¶ added in v1.1.2
type Hooks struct {
// OnRetry is called before waiting for the next retry interval.
OnRetry func(attempt int, err error)
}
Hooks defines callback functions invoked by the retrier.
type Option ¶ added in v1.0.5
type Option func(*Retrier)
Option is a function type that can be used to configure the `Retrier` struct.
func WithBackoffFactor ¶ added in v1.0.9
WithBackoffFactor returns an option that sets the backoff factor.
func WithInterval ¶ added in v1.0.5
WithInterval returns an option that sets the interval.
func WithJitter ¶ added in v1.0.5
WithJitter returns an option that sets the jitter.
func WithLogger ¶ added in v1.1.2
WithLogger sets a slog logger.
func WithMaxRetries ¶ added in v1.0.5
WithMaxRetries returns an option that sets the maximum number of retries after the first attempt.
func WithTimeout ¶ added in v1.0.5
WithTimeout returns an option that sets the timeout.
type Registry ¶ added in v1.0.9
type Registry struct {
// contains filtered or unexported fields
}
Registry holds a set of temporary errors.
func (*Registry) Clean ¶ added in v1.0.9
func (r *Registry) Clean()
Clean removes all temporary errors from the registry.
func (*Registry) IsTemporaryError ¶ added in v1.1.0
IsTemporaryError reports whether err matches any of the temporary errors.
func (*Registry) ListTemporaryErrors ¶ added in v1.0.9
ListTemporaryErrors returns all temporary errors in the registry.
func (*Registry) LoadDefaults ¶ added in v1.0.9
LoadDefaults loads a set of default temporary errors.
func (*Registry) RegisterTemporaryError ¶ added in v1.0.9
RegisterTemporaryError registers a temporary error.
func (*Registry) RegisterTemporaryErrors ¶ added in v1.0.9
RegisterTemporaryErrors registers multiple temporary errors.
func (*Registry) UnRegisterTemporaryError ¶ added in v1.0.9
UnRegisterTemporaryError removes a temporary error.
func (*Registry) UnRegisterTemporaryErrors ¶ added in v1.0.9
UnRegisterTemporaryErrors removes multiple temporary errors.
type Retrier ¶
type Retrier struct {
// MaxRetries is the maximum number of retries after the first attempt.
MaxRetries int
// Jitter is the amount of jitter to apply to the retry interval.
Jitter time.Duration
// BackoffFactor is the factor to apply to the retry interval.
BackoffFactor float64
// Interval is the interval between retries.
Interval time.Duration
// Timeout is the timeout for the retry function.
Timeout time.Duration
// Registry is the registry for temporary errors.
Registry *Registry
// Logger used for logging attempts.
Logger *slog.Logger
// Hooks executed during retries.
Hooks Hooks
// contains filtered or unexported fields
}
Retrier is a type that retries a function until it returns a nil error or the maximum number of retries is reached.
func NewRetrier ¶
NewRetrier returns a new Retrier configured with the given options. If no options are provided, the default options are used. The default options are:
- MaxRetries: 5 (retries after the first attempt)
- Jitter: 1 * time.Second
- Interval: 500 * time.Millisecond
- Timeout: 20 * time.Second
func (*Retrier) Cancel ¶ added in v1.0.4
func (r *Retrier) Cancel()
Cancel cancels the retries notifying the `Do` function to return.
func (*Retrier) Do ¶ added in v1.0.7
func (r *Retrier) Do(ctx context.Context, retryableFunc RetryableFunc, temporaryErrors ...error) (errs *Errors)
Do retries a `retryableFunc` until it returns a nil error or the maximum number of retries is reached.
- If the maximum number of retries is reached, the function returns an `Errors` object.
- If the `retryableFunc` returns a nil error, the function assigns an `Errors.Last` before returning.
- If the `retryableFunc` returns a temporary error, the function retries the function.
- If the `retryableFunc` returns a non-temporary error, the function assigns the error to `Errors.Last` and returns.
- If the `temporaryErrors` list is empty and the registry has entries, only those errors are retried.
- If the `temporaryErrors` list is empty and the registry is empty, all errors are retried.
- The context is checked between attempts; long-running functions should handle cancellation themselves.
func (*Retrier) DoWithContext ¶ added in v1.2.0
func (r *Retrier) DoWithContext(ctx context.Context, retryableFunc RetryableFuncWithContext, temporaryErrors ...error) (errs *Errors)
DoWithContext retries a context-aware function until it succeeds, a terminal error is encountered, the provided context is canceled, the retrier timeout elapses, or the maximum number of retries is reached. It is the context-aware counterpart of Do.
The behavior of DoWithContext mirrors Do:
- The retryableFunc is invoked at most MaxRetries+1 times (the initial attempt plus up to MaxRetries retries) until it returns a nil error.
- Any error returned by retryableFunc that matches one of the temporaryErrors is treated as transient. That error is appended to errs.Attempts and the call is retried according to the configured backoff, jitter, and interval settings.
- Any error that does not match temporaryErrors is considered terminal. In that case, the retry loop stops immediately and errs.Last is set to that error without performing further retries.
- If the Retrier is configured with a registry, attempt and error information are recorded in the registry in the same way as for Do.
When the maximum number of retries (MaxRetries) is reached without a successful (nil) result, the last attempt error is wrapped with ErrMaxRetriesReached and appended to errs.Attempts. In this case, errs.Last contains the last error returned by retryableFunc.
Context and timeout handling:
- The provided ctx is observed on each attempt and during backoff delays. If ctx is canceled or its deadline is exceeded, DoWithContext stops retrying and returns immediately, with errs.Last set to the corresponding context error or any wrapped form produced by the internals of the retrier.
- In addition to ctx, the Retrier's Timeout field enforces an overall timeout for the entire operation. If this timeout elapses first, DoWithContext stops retrying and returns with errs.Last set to ErrTimeoutReached (wrapped with the attempt number).
Use DoWithContext when the operation being retried accepts a context and must support cancellation. Use Do for retrying functions that do not take a context.
func (*Retrier) PutErrors ¶ added in v1.1.2
PutErrors returns an Errors object to the pool after resetting it.
func (*Retrier) SetRegistry ¶
SetRegistry sets the registry for temporary errors. Use this function to set a custom registry if: - you want to add custom temporary errors. - you want to remove the default temporary errors. - you want to replace the default temporary errors with your own. - you have initialized the Retrier without using the constructor `NewRetrier`.
type RetryableFunc ¶ added in v1.0.5
type RetryableFunc func() error
RetryableFunc signature of retryable function.
type RetryableFuncWithContext ¶ added in v1.2.0
RetryableFuncWithContext is a retryable function that observes context cancellation.
type TimerPool ¶ added in v1.0.9
type TimerPool struct {
// contains filtered or unexported fields
}
TimerPool is a pool of timers.
func NewTimerPool ¶ added in v1.0.4
NewTimerPool creates a new timer pool.