Documentation ¶
Index ¶
- func Cause(err error) error
- func Done(e error) error
- func ExponentialBackoffAlgorithm(initialDelay time.Duration, maxDelay time.Duration, multiplier float64, ...) func() time.Duration
- func FixedBackoffAlgorithm(delay time.Duration) func() time.Duration
- func FullJitterBackoffAlgorithm(baseDelay time.Duration, maxDelay time.Duration) func() time.Duration
- func HintStop(e error) error
- func HintTemporary(e error) error
- func IsStop(e error) bool
- func IsTemporary(e error) bool
- func Nope(e error) error
- func Once(ops ...Operation) error
- func Repeat(ops ...Operation) error
- func SetContext(ctx context.Context) func(*DelayOptions)
- func SetContextHintStop() func(*DelayOptions)
- func SetErrorsTimeout(t time.Duration) func(*DelayOptions)
- type DelayOptions
- type ExponentialBackoffBuilder
- func (s *ExponentialBackoffBuilder) Set() func(*DelayOptions)
- func (s *ExponentialBackoffBuilder) WithInitialDelay(d time.Duration) *ExponentialBackoffBuilder
- func (s *ExponentialBackoffBuilder) WithJitter(j float64) *ExponentialBackoffBuilder
- func (s *ExponentialBackoffBuilder) WithMaxDelay(d time.Duration) *ExponentialBackoffBuilder
- func (s *ExponentialBackoffBuilder) WithMultiplier(m float64) *ExponentialBackoffBuilder
- type FixedBackoffBuilder
- type FullJitterBackoffBuilder
- type OpWrapper
- type Operation
- func Compose(ops ...Operation) Operation
- func Fn(op func() error) Operation
- func FnDone(op Operation) Operation
- func FnES(op func(error)) Operation
- func FnHintStop(op Operation) Operation
- func FnHintTemporary(op Operation) Operation
- func FnNope(op Operation) Operation
- func FnOnError(op Operation) Operation
- func FnOnSuccess(op Operation) Operation
- func FnOnlyOnce(op Operation) Operation
- func FnPanic(op Operation) Operation
- func FnRepeat(ops ...Operation) Operation
- func FnS(op func()) Operation
- func FnWithCounter(op func(int) error) Operation
- func FnWithErrorAndCounter(op func(error, int) error) Operation
- func Forward(op Operation) Operation
- func LimitMaxTries(max int) Operation
- func StopOnSuccess() Operation
- func WithDelay(options ...func(hb *DelayOptions)) Operation
- type Repeater
- type StopError
- type TemporaryError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cause ¶
Cause extracts the cause error from TemporaryError and StopError or return the passed one.
func ExponentialBackoffAlgorithm ¶
func ExponentialBackoffAlgorithm(initialDelay time.Duration, maxDelay time.Duration, multiplier float64, jitter float64) func() time.Duration
ExponentialBackoffAlgorithm implements classic caped exponential backoff.
Example (initialDelay=1, maxDelay=30, Multiplier=2, Jitter=0.5): Attempt Delay ------- -------------------------- 0 1 + random [-0.5...0.5] 1 2 + random [-1...1] 2 4 + random [-2...2] 3 8 + random [-4...4] 4 16 + random [-8...8] 5 32 + random [-16...16] 6 64 + random [-32...32] = 30
func FixedBackoffAlgorithm ¶
FixedBackoffAlgorithm implements backoff with a fixed delay.
func FullJitterBackoffAlgorithm ¶
func FullJitterBackoffAlgorithm(baseDelay time.Duration, maxDelay time.Duration) func() time.Duration
FullJitterBackoffAlgorithm implements caped exponential backoff with jitter. Algorithm is fast because it does not use floating point arithmetics.
Details: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
Example (BaseDelay=1, maxDelay=30): Call Delay ------- ---------------- 1 random [0...1] 2 random [0...2] 3 random [0...4] 4 random [0...8] 5 random [0...16] 6 random [0...30] 7 random [0...30]
func IsTemporary ¶ added in v1.4.1
IsTemporary checks if passed error is TemporaryError.
func Once ¶ added in v1.4.1
Once composes the operations and executes the result once.
It is guaranteed that the first op will be called at least once.
func Repeat ¶
Repeat repeat operations until one of them stops the repetition.
It is guaranteed that the first op will be called at least once.
func SetContext ¶
func SetContext(ctx context.Context) func(*DelayOptions)
SetContext allows to set a context instead of default one.
func SetContextHintStop ¶ added in v1.4.1
func SetContextHintStop() func(*DelayOptions)
SetContextHintStop instructs to use HintStop(nil) instead of context error in case of context expiration.
func SetErrorsTimeout ¶
func SetErrorsTimeout(t time.Duration) func(*DelayOptions)
SetErrorsTimeout specifies the maximum timeout for repetition in case of error. This timeout is reset each time when the repetition operation is successfully completed.
Default value is maximum time.Duration value.
Types ¶
type DelayOptions ¶
type DelayOptions struct { ErrorsTimeout time.Duration Backoff func() time.Duration Context context.Context ContextHintStop bool }
DelayOptions holds parameters for a heartbeat process.
type ExponentialBackoffBuilder ¶
type ExponentialBackoffBuilder struct { // MaxDelay specifies maximum value of a delay calculated by the // algorithm. // // Default value is maximum time.Duration value. MaxDelay time.Duration // InitialDelay specifies an initial delay for the algorithm. // // Default value is equal to 1 second. InitialDelay time.Duration // Miltiplier specifies a multiplier for the last calculated // or specified delay. // // Default value is 2. Multiplier float64 // Jitter specifies randomization factor [0..1]. // // Default value is 0. Jitter float64 // contains filtered or unexported fields }
ExponentialBackoffBuilder is an option builder.
func ExponentialBackoff ¶
func ExponentialBackoff(initialDelay time.Duration) *ExponentialBackoffBuilder
ExponentialBackoff create a builder for Delay's option.
func (*ExponentialBackoffBuilder) Set ¶
func (s *ExponentialBackoffBuilder) Set() func(*DelayOptions)
Set creates a Delay' option.
func (*ExponentialBackoffBuilder) WithInitialDelay ¶
func (s *ExponentialBackoffBuilder) WithInitialDelay(d time.Duration) *ExponentialBackoffBuilder
WithInitialDelay allows to set InitialDelay.
InitialDelay specifies an initial delay for the algorithm.
func (*ExponentialBackoffBuilder) WithJitter ¶
func (s *ExponentialBackoffBuilder) WithJitter(j float64) *ExponentialBackoffBuilder
WithJitter allows to set Jitter.
Jitter specifies randomization factor [0..1].
Default value is 0.
func (*ExponentialBackoffBuilder) WithMaxDelay ¶
func (s *ExponentialBackoffBuilder) WithMaxDelay(d time.Duration) *ExponentialBackoffBuilder
WithMaxDelay allows to set MaxDelay.
MaxDelay specifies the maximum value of a delay calculated by the algorithm.
Default value is maximum time.Duration value.
func (*ExponentialBackoffBuilder) WithMultiplier ¶
func (s *ExponentialBackoffBuilder) WithMultiplier(m float64) *ExponentialBackoffBuilder
WithMultiplier allows to set Multiplier.
Miltiplier specifies a multiplier for the last calculated ¶
Default value is 2.
type FixedBackoffBuilder ¶
FixedBackoffBuilder is an option builder.
func FixedBackoff ¶
func FixedBackoff(delay time.Duration) *FixedBackoffBuilder
FixedBackoff create a builder for Delay's option.
func (*FixedBackoffBuilder) Set ¶
func (s *FixedBackoffBuilder) Set() func(*DelayOptions)
Set creates a Delay' option.
type FullJitterBackoffBuilder ¶
type FullJitterBackoffBuilder struct { // MaxDelay specifies maximum value of a delay calculated by the // algorithm. // // Default value is maximum time.Duration value. MaxDelay time.Duration // BaseDelay specifies base of an exponent. BaseDelay time.Duration }
FullJitterBackoffBuilder is an option builder.
func FullJitterBackoff ¶
func FullJitterBackoff(baseDelay time.Duration) *FullJitterBackoffBuilder
FullJitterBackoff create a builder for Delay's option.
func (*FullJitterBackoffBuilder) Set ¶
func (s *FullJitterBackoffBuilder) Set() func(*DelayOptions)
Set creates a Delay' option.
func (*FullJitterBackoffBuilder) WithBaseDelay ¶
func (s *FullJitterBackoffBuilder) WithBaseDelay(d time.Duration) *FullJitterBackoffBuilder
WithBaseDelay allows to set BaseDelay.
BaseDelay specifies base of an exponent.
func (*FullJitterBackoffBuilder) WithMaxDelay ¶
func (s *FullJitterBackoffBuilder) WithMaxDelay(d time.Duration) *FullJitterBackoffBuilder
WithMaxDelay allows to set MaxDelay.
MaxDelay specifies the maximum value of a delay calculated by the algorithm.
Default value is maximum time.Duration value.
type OpWrapper ¶ added in v1.4.1
OpWrapper is the type of function for repetition.
func WrStopOnContextError ¶ added in v1.4.1
WrStopOnContextError stops an operation in case of context error.
type Operation ¶
Operation is the type of function for repetition.
func FnHintStop ¶ added in v1.4.1
FnHintStop hints all operation errors as StopError.
func FnHintTemporary ¶ added in v1.4.1
FnHintTemporary hints all operation errors as temporary.
func FnOnSuccess ¶ added in v1.4.1
FnOnSuccess executes operation in case of error is nil.
func FnOnlyOnce ¶ added in v1.4.1
FnOnlyOnce executes op only once permanently.
func FnPanic ¶ added in v1.4.1
FnPanic panics if op returns any error other than nil, TemporaryError and StopError.
func FnS ¶ added in v1.4.1
func FnS(op func()) Operation
FnS wraps operation with no arguments and return value.
func FnWithCounter ¶
FnWithCounter wraps operation with counter only.
func FnWithErrorAndCounter ¶
FnWithErrorAndCounter wraps operation and adds call counter.
func LimitMaxTries ¶
LimitMaxTries returns true if attempt number is less then max.
func StopOnSuccess ¶
func StopOnSuccess() Operation
StopOnSuccess returns true in case of error is nil.
func WithDelay ¶
func WithDelay(options ...func(hb *DelayOptions)) Operation
WithDelay constructs HeartbeatPredicate.
type Repeater ¶ added in v1.4.1
type Repeater interface { Once(...Operation) error Repeat(...Operation) error Compose(...Operation) Operation FnRepeat(...Operation) Operation }
Repeater represents general package concept.
func Cpp ¶ added in v1.4.1
Cpp returns object that calls C (constructor) at first, then ops, then D (destructor). D will be called in any case if C returns nil.
Note! Cpp panics if D returns non nil error. Wrap it using Done if you log D's error or handle it somehow else.
func NewRepeater ¶ added in v1.4.1
func NewRepeater() Repeater
NewRepeater sets up everything to be able to repeat operations.
func NewRepeaterExt ¶ added in v1.5.0
NewRepeaterExt returns object that wraps all ops with with the given opw and wraps composed operation with the given copw.
func With ¶ added in v1.5.0
With returns object that calls C (constructor) at first, then ops, then D (destructor). D will be called in any case if C returns nil.
Note! D is able to hide original error an return nil or return error event if the original error is nil.
func WithContext ¶ added in v1.4.1
WithContext repeat operations until one of them stops the repetition or context will be canceled.
It is guaranteed that the first op will be called at least once.
type StopError ¶
type StopError struct {
Cause error
}
StopError allows to stop repetition process without specifying a separate error.
This error never returns to the caller as is, only wrapped error.
type TemporaryError ¶
type TemporaryError struct {
Cause error
}
TemporaryError allows not to stop repetitions process right now.
This error never returns to the caller as is, only wrapped error.
func (*TemporaryError) Error ¶
func (e *TemporaryError) Error() string