Documentation
¶
Index ¶
- Variables
- type Errors
- type Option
- type Registry
- func (r *Registry) Clean()
- func (r *Registry) GetTemporaryError(name string) (TemporaryError, bool)
- func (r *Registry) GetTemporaryErrors(names ...string) []TemporaryError
- func (r *Registry) ListTemporaryErrors() []TemporaryError
- func (r *Registry) LoadDefaults() *Registry
- func (r *Registry) RegisterTemporaryError(name string, fn func() TemporaryError)
- func (r *Registry) RegisterTemporaryErrors(temporaryErrors map[string]func() TemporaryError)
- func (r *Registry) UnRegisterTemporaryError(names ...string)
- func (r *Registry) UnRegisterTemporaryErrors(temporaryErrors map[string]func() TemporaryError)
- type Retrier
- type RetryableFunc
- type TemporaryError
- type TimerPool
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidRetrier is the error returned when the retrier is invalid. ErrInvalidRetrier = fmt.Errorf("invalid retrier") // ErrMaxRetriesReached is the error returned when the maximum number of retries is reached. ErrMaxRetriesReached = errors.New("maximum number of retries reached") // ErrTimeoutReached is the error returned when the timeout is reached. ErrTimeoutReached = fmt.Errorf("operation timeout reached") // ErrOperationStopped is the error returned when the retry is stopped. ErrOperationStopped = fmt.Errorf("operation stopped") // ErrNilRetryableFunc is the error returned when the retryable function is nil. ErrNilRetryableFunc = fmt.Errorf("failed to invoke the function. It appears to be is nil") )
Functions ¶
This section is empty.
Types ¶
type Errors ¶ added in v1.0.7
type Errors struct {
// Retries holds the trace of each attempt.
Retries map[int]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.
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 WithMaxRetries ¶ added in v1.0.5
WithMaxRetries returns an option that sets the maximum number of retries.
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 for temporary errors.
func (*Registry) GetTemporaryError ¶ added in v1.0.9
func (r *Registry) GetTemporaryError(name string) (TemporaryError, bool)
GetTemporaryError returns a temporary error by name.
func (*Registry) GetTemporaryErrors ¶ added in v1.0.9
func (r *Registry) GetTemporaryErrors(names ...string) []TemporaryError
GetTemporaryErrors returns a list of temporary errors filtered by name.
func (*Registry) ListTemporaryErrors ¶ added in v1.0.9
func (r *Registry) ListTemporaryErrors() []TemporaryError
ListTemporaryErrors returns a list of temporary errors.
func (*Registry) LoadDefaults ¶ added in v1.0.9
LoadDefaults loads the default temporary errors into the registry.
func (*Registry) RegisterTemporaryError ¶ added in v1.0.9
func (r *Registry) RegisterTemporaryError(name string, fn func() TemporaryError)
RegisterTemporaryError registers a temporary error.
func (*Registry) RegisterTemporaryErrors ¶ added in v1.0.9
func (r *Registry) RegisterTemporaryErrors(temporaryErrors map[string]func() TemporaryError)
RegisterTemporaryErrors registers multiple temporary errors.
func (*Registry) UnRegisterTemporaryError ¶ added in v1.0.9
UnRegisterTemporaryError unregisters a temporary error(s).
func (*Registry) UnRegisterTemporaryErrors ¶ added in v1.0.9
func (r *Registry) UnRegisterTemporaryErrors(temporaryErrors map[string]func() TemporaryError)
UnRegisterTemporaryErrors unregisters multiple temporary errors.
type Retrier ¶
type Retrier struct {
// MaxRetries is the maximum number of retries.
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
// 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
- 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 ...string) (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, the function retries the function until the maximum number of retries is reached.
- The context is used to cancel the retries, or set a deadline if the `retryableFunc` hangs.
func (*Retrier) IsTemporaryError ¶
IsTemporaryError checks if the error is in the list of temporary errors.
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`.
func (*Retrier) Validate ¶ added in v1.0.8
Validate validates the Retrier. This method will check if:
- `MaxRetries` is less than or equal to zero
- `Interval` is greater than or equal to `Timeout`
- The total time consumed by all retries (`Interval` multiplied by `MaxRetries`) should be less than `Timeout`.
type RetryableFunc ¶ added in v1.0.5
type RetryableFunc func() error
RetryableFunc signature of retryable function
type TemporaryError ¶ added in v1.0.5
type TemporaryError error
TemporaryError implements the error interface.
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.