Documentation
¶
Index ¶
- func Cause(err 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 Repeat(ops ...Operation) error
- func SetContext(ctx context.Context) 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 Operation
- func Compose(ops ...Operation) Operation
- func ComposeSlice(ops []Operation) Operation
- func Fn(op func() error) Operation
- func FnWithCounter(op func(int) error) Operation
- func FnWithErrorAndCounter(op func(error, int) error) Operation
- func LimitMaxTries(max int) Operation
- func StopOnSuccess() Operation
- func WithDelay(options ...func(hb *DelayOptions)) Operation
- 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 Repeat ¶
Repeat repeat operation 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 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
}
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 Operation ¶
Operation is the type of function for repetition.
func ComposeSlice ¶
ComposeSlice binds all passed operations into a single one.
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 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
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
backoff
command
|
|
|
backoff_with_timeout
command
|
|
|
heartbeat
command
|
|
|
heartbeat_with_error_timeout
command
|