Documentation ¶
Overview ¶
Package retry offers feature to retry a function. It offers different backoff strategies.
Retry means when the first function execution fails, then it will do retry. It will try to retry until maximum allowed attempt. There will be a delay (backoff) from one attempt to another.
When a function return unretryable error, it will not try to the next attempt.
Index ¶
- Constants
- func Do(f func() error, options ...Option) error
- func DoWithContext(ctx context.Context, f func() error, options ...Option) error
- func Unretryable(err error) error
- type Backoff
- type BackoffCappedExponential
- type BackoffConstant
- type BackoffDecorrelated
- type BackoffEqualJitter
- type BackoffFullJitter
- type BackoffTruncatedExponential
- type ContextDoneError
- type Option
- type Rand
- type Randomizer
- type Retrier
- type UnretryableError
Constants ¶
const ( DefaultBackoffMaximumInterval = time.Millisecond * 1000 DefaultBackoffBaseInterval = time.Millisecond * 100 )
DefaultBackoff denotes default configuration value for Backoff
const (
DefaultRetryMaxAttempts uint = 10
)
DefaultRetry default configuration for retrier.
Variables ¶
This section is empty.
Functions ¶
func DoWithContext ¶
DoWithContext runs f with retry. While waiting for next attempt, it will also check context cancelation. This is handy if you do not want to initialize Retrier.
func Unretryable ¶
Unretryable wraps an error in UnretryableError struct When you do this inside your function, it will not retry into the next attempt.
Types ¶
type BackoffCappedExponential ¶
type BackoffCappedExponential struct {
// contains filtered or unexported fields
}
BackoffCappedExponential backoff strategy to multiplied backoff by a constant after each attempt, up to some maximum value.
func NewBackoffCappedExponential ¶
func NewBackoffCappedExponential(maxInterval, baseInterval time.Duration) BackoffCappedExponential
NewBackoffCappedExponential initialize BackoffCappedExponential.
type BackoffConstant ¶
type BackoffConstant struct {
// contains filtered or unexported fields
}
BackoffConstant backoff strategy to backoff by a constant number for each attempt.
func NewBackoffConstant ¶
func NewBackoffConstant(baseInterval time.Duration) BackoffConstant
NewBackoffConstant initialize ConstantBackoff.
type BackoffDecorrelated ¶
type BackoffDecorrelated struct {
// contains filtered or unexported fields
}
BackoffDecorrelated a timed backoff loops which is similar to "Full Jitter" with increment in the maximum jitter based on the last backoff value.
func NewBackoffDecorrelated ¶
func NewBackoffDecorrelated(maxInterval, baseInterval time.Duration, randomizer Randomizer) *BackoffDecorrelated
NewBackoffDecorrelated initialize BackoffDecorrelated.
type BackoffEqualJitter ¶
type BackoffEqualJitter struct {
// contains filtered or unexported fields
}
BackoffEqualJitter a timed backoff loops which keeps some of the backoff and jitter by a smaller amount.
func NewBackoffEqualJitter ¶
func NewBackoffEqualJitter(maxInterval, baseInterval time.Duration, randomizer Randomizer) BackoffEqualJitter
NewBackoffEqualJitter initialize BackoffEqualJitter.
type BackoffFullJitter ¶
type BackoffFullJitter struct {
// contains filtered or unexported fields
}
BackoffFullJitter backoff strategy to multiplied backoff by randomize delay up until current multiplied backoff.
func NewBackoffFullJitter ¶
func NewBackoffFullJitter(maxInterval, baseInterval time.Duration, randomizer Randomizer) BackoffFullJitter
NewBackoffFullJitter initialize BackoffFullJitter.
type BackoffTruncatedExponential ¶
type BackoffTruncatedExponential struct {
// contains filtered or unexported fields
}
BackoffTruncatedExponential backoff strategy to multiplied backoff by periodically increasing delays with additional jitters.
func NewBackoffTruncatedExponential ¶
func NewBackoffTruncatedExponential(maxInterval, baseInterval time.Duration, randomizer Randomizer) BackoffTruncatedExponential
NewBackoffTruncatedExponential initialize BackoffTruncatedExponential.
type ContextDoneError ¶
type ContextDoneError struct {
// contains filtered or unexported fields
}
ContextDoneError denotes an error that triggered when context is done.
func (*ContextDoneError) Unwrap ¶
func (u *ContextDoneError) Unwrap() error
type Option ¶
type Option func(*Retrier)
Option denotes optional configuration.
func WithBackoff ¶
WithBackoff configures backoff strategy.
func WithMaxRetryAttempts ¶
WithMaxRetryAttempts configures maximum attempt to retry.
type Rand ¶
type Rand struct {
// contains filtered or unexported fields
}
Rand is math.Rand wrapper with concurency protection.
type Randomizer ¶
Randomizer is the rand interface
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
Retrier denotes a retrier struct.
func NewRetrier ¶
NewRetrier initializes Retrier struct. This is handy if you want to re-use retrier with the same configuration.
If option is not provided, it will use default configuration. Example:
retrier := retry.NewRetrier( retry.WithMaxRetryAttempts(1), retry.WithBackoff( retry.NewBackoffTruncatedExponential( time.Second, time.Millisecond, retry.NewRand(time.Now().UnixNano()), ), ), )
type UnretryableError ¶
type UnretryableError struct {
// contains filtered or unexported fields
}
UnretryableError denotes an error that should be exit immediately.
func (*UnretryableError) Unwrap ¶
func (u *UnretryableError) Unwrap() error
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
withcontext
Example of retry with context.
|
Example of retry with context. |
withcustombackoff
Example of retry with custom back-off strategy by implementing Backoff interface.
|
Example of retry with custom back-off strategy by implementing Backoff interface. |
withinitialization
Example of retry with initialization and runs it concurently.
|
Example of retry with initialization and runs it concurently. |
withunretryable
Example of retry with unretryable error.
|
Example of retry with unretryable error. |
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |