Documentation ¶
Index ¶
Constants ¶
const MaxRetriesUnlimited int = -1
MaxRetriesUnlimited is the value of our max retries when we don't have a maximum.
Variables ¶
var ErrNoRetryFunc = errors.New("no retry function has been set")
Functions ¶
func Retry ¶
Retry is a function that takes a Retryable interface and a context. It runs Run(), performing whatever action the caller has defined. If Run() returns an error, Retry determines whether it should retry using ShouldRetry(), and if it should, sleeps the appropriate duration as specified by Interval() before retrying.
Types ¶
type Retrier ¶
type Retrier struct { // Maximum number of retries (optional) MaxRetries int // Only retry on these errors (optional) OnlyRetryError []error // A function that returns a time.Duration for the next interval, based on current attempt number (optional) RetryInterval RetryInterval // The operation to be run (required) Func func(context.Context) (any, error) // contains filtered or unexported fields }
Retrier is a struct that allows the caller to define their parameters for retrying an operation.
func NewRetrier ¶
func NewRetrier(opts ...RetrierOpt) (*Retrier, error)
NewRetrier takes one or more RetrierOpt functions, creates a new Retrier, sets the fields specified in the RetrierOpts, and returns a pointer to the new Retrier. If it determines that no Func has been specified (the operation to be run or retried), it returns an error.
func (*Retrier) Clone ¶
Clone returns a cloned copy of the retrier. If for some reason we add pointer fieds to this struct we'll have to handle those.
func (*Retrier) Interval ¶
Interval returns the RetryInterval function that has been set on the Retrier, passing it the current number of attempts so that the RetryInterval function can calculate the interval duration until the next retry.
func (*Retrier) Run ¶
Run is a function that runs the operation that has been set on the Retrier, passing along the context and increasing the attempt number with each try.
func (*Retrier) ShouldRetry ¶
ShouldRetry is a function that determines whether the operation should be retried. It checks whether the max number of retries has been reached. It also checks whether the caller has submitted any OnlyRetryErrors, in which case retries should only occur in the case of those errors.
type RetrierOpt ¶
RetrierOpt is a function that takes a pointer to a Retrier and returns that same pointer, allowing some actions to be performed upon the Retrier — for example, setting some of its fields.
func WithIntervalFunc ¶
func WithIntervalFunc(i RetryInterval) RetrierOpt
WithIntervalFunc allows the caller to define a function to calculate the interval between retries (IntervalExponential and IntervalFibonacci are available for this purpose). It is optional.
func WithMaxRetries ¶
func WithMaxRetries(retries int) RetrierOpt
WithMaxRetries allows the caller to define a max number of retries. It is optional.
func WithOnlyRetryErrors ¶
func WithOnlyRetryErrors(err ...error) RetrierOpt
WithOnlyRetryErrors allows the caller to define a list of errors upon which to retry. If one or more errors is provided, Retry() will only retry on those errors. It is optional.
func WithRetrierFunc ¶
func WithRetrierFunc(f func(context.Context) (any, error)) RetrierOpt
WithRetrierFunc allows the caller to define the operation to be run/retried. It is required to create a new Retrier.
type RetryInterval ¶
RetryInterval is a function that takes an integer that defines the attempt number. Given the number of attempts it will calculate and return the `time.Duration` that should be waited before attempting the next retry.
func IntervalDuration ¶
func IntervalDuration(dur time.Duration) RetryInterval
IntervalDuration returns a function that calculates a static interveral duration.
func IntervalExponential ¶
func IntervalExponential(base time.Duration) RetryInterval
IntervalExponential returns a function that calculates an interval duration for the next retry, based on the current number of attempts and the base number/unit. The intervals increase exponentially (^2) with each attempt.
func IntervalFibonacci ¶
func IntervalFibonacci(base time.Duration) RetryInterval
IntervalFibonacci returns a function that calculates an interval duration for the next retry, based on the current number of attempts and the base number/unit. The intervals increase on the basis of the Fibonacci sequence with each attempt.