Documentation ¶
Index ¶
- func DoFunc[T any](ctx context.Context, r *Retrier, callback func(*Retrier) (T, error)) (T, error)
- func DoFunc2[T1, T2 any](ctx context.Context, r *Retrier, callback func(*Retrier) (T1, T2, error)) (T1, T2, error)
- func DoFunc3[T1, T2, T3 any](ctx context.Context, r *Retrier, callback func(*Retrier) (T1, T2, T3, error)) (T1, T2, T3, error)
- func TryForever() retrierOpt
- func WithJitter() retrierOpt
- func WithMaxAttempts(maxAttempts int) retrierOpt
- func WithRand(rand *rand.Rand) retrierOpt
- func WithSleepFunc(f func(time.Duration)) retrierOpt
- func WithStrategy(strategy Strategy, strategyType string) retrierOpt
- type Retrier
- func (r *Retrier) AttemptCount() int
- func (r *Retrier) Break()
- func (r *Retrier) Do(callback func(*Retrier) error) error
- func (r *Retrier) DoWithContext(ctx context.Context, callback func(*Retrier) error) error
- func (r *Retrier) Jitter() time.Duration
- func (r *Retrier) MarkAttempt()
- func (r *Retrier) NextInterval() time.Duration
- func (r *Retrier) SetNextInterval(d time.Duration)
- func (r *Retrier) ShouldGiveUp() bool
- func (r *Retrier) String() string
- type Strategy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoFunc ¶ added in v1.2.0
DoFunc is a helper for retrying callback functions that return a value or an error. It returns the last value returned by a call to callback, and reports an error if none of the calls succeeded. (Note this is not a method of Retrier, since methods can't be generic.)
func DoFunc2 ¶ added in v1.2.0
func DoFunc2[T1, T2 any](ctx context.Context, r *Retrier, callback func(*Retrier) (T1, T2, error)) (T1, T2, error)
DoFunc2 is a helper for retrying callback functions that return two value or an error. It returns the last values returned by a call to callback, and reports an error if none of the calls succeeded. (Note this is not a method of Retrier, since methods can't be generic.)
func DoFunc3 ¶ added in v1.2.0
func DoFunc3[T1, T2, T3 any](ctx context.Context, r *Retrier, callback func(*Retrier) (T1, T2, T3, error)) (T1, T2, T3, error)
DoFunc3 is a helper for retrying callback functions that return 3 values or an error. It returns the last values returned by a call to callback, and reports an error if none of the calls succeeded. (Note this is not a method of Retrier, since methods can't be generic.)
func TryForever ¶
func TryForever() retrierOpt
TryForever causes the retrier to to never give up retrying, until either the operation succeeds, or the operation calls retrier.Break()
func WithJitter ¶
func WithJitter() retrierOpt
WithJitter enables jitter on the retrier, which will cause all of the retries to wait a random amount of time < 1 second The idea here is to avoid thundering herds - retries that are in parallel will happen at slightly different times when jitter is enabled, whereas if jitter is disabled, all the retries might happen at the same time, causing further load on the system that we're tryung to do something with
func WithMaxAttempts ¶
func WithMaxAttempts(maxAttempts int) retrierOpt
WithMaxAttempts sets the maximum number of retries that a retrier will attempt
func WithSleepFunc ¶
WithSleepFunc sets the function that the retrier uses to sleep between successive attempts Only really useful for testing
func WithStrategy ¶
WithStrategy sets the retry strategy that the retrier will use to determine how long to wait between retries
Types ¶
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
func NewRetrier ¶
func NewRetrier(opts ...retrierOpt) *Retrier
NewRetrier creates a new instance of the Retrier struct. Pass in retrierOpt functions to customise the behaviour of the retrier
func (*Retrier) AttemptCount ¶
func (*Retrier) Break ¶
func (r *Retrier) Break()
Break causes the Retrier to stop retrying after it completes the next retry cycle
func (*Retrier) Do ¶
Do is the core loop of a Retrier. It defines the operation that the Retrier will attempt to perform, retrying it if necessary Calling retrier.Do(someFunc) will cause the Retrier to attempt to call the function, and if it returns an error, retry it using the settings provided to it.
func (*Retrier) DoWithContext ¶ added in v1.0.2
DoWithContext is a context-aware variant of Do.
func (*Retrier) Jitter ¶
Jitter returns a duration in the interval (0, 1] s if jitter is enabled, or 0 s if it's not
func (*Retrier) MarkAttempt ¶
func (r *Retrier) MarkAttempt()
MarkAttempt increments the attempt count for the retrier. This affects ShouldGiveUp, and also affects the retry interval for Exponential retry strategy
func (*Retrier) NextInterval ¶
NextInterval returns the next interval that the retrier will use. Behind the scenes, it calls the function generated by either retrier's strategy
func (*Retrier) SetNextInterval ¶ added in v1.1.0
SetNextInterval overrides the strategy for the interval before the next try
func (*Retrier) ShouldGiveUp ¶
ShouldGiveUp returns whether the retrier should stop trying do do the thing it's been asked to do It returns true if the retry count is greater than r.maxAttempts, or if r.Break() has been called It returns false if the retrier is supposed to try forever
type Strategy ¶
func Constant ¶
Constant returns a strategy that always returns the same value, the interval passed in as an arg to the function Semantically, when this is used with a roko.Retrier, it means that the retrier will always wait the given duration before retrying
func Exponential ¶
Exponential returns a strategy that increases expontially based on the number of attempts the retrier has made It uses the calculation: adjustment + (base ** attempts) + jitter
func ExponentialSubsecond ¶ added in v1.1.0
The 16 exponent-divisor is arbitrarily chosen to scale curves nicely for a reasonable range of initial delays and number of attempts (e.g. 1 second, 10 attempts).
Examples of a small, medium and large initial time.Second growing over 10 attempts:
100ms → 133ms → 177ms → 237ms → 316ms → 421ms → 562ms → 749ms → 1000ms 1.0s → 1.5s → 2.4s → 3.7s → 5.6s → 8.7s → 13.3s → 20.6s → 31.6s 5s → 9s → 14s → 25s → 42s → 72s → 120s → 208s → 354s