Documentation ¶
Overview ¶
backoff 重试算法
使用retry,或retryNotify 调用可能失败的方法
demo 在demo目录下
====================================================
# Copyright (C)2019 All rights reserved. # # Author : domchan # Email : 814172254@qq.com # File Name : service.go # Created : 2019/1/11 17:07 # Last Modified : 2019/1/11 17:07 # Describe : # # ====================================================
Index ¶
- Constants
- Variables
- func Retry(o Operation, b BackOff) error
- func RetryNotify(operation Operation, b BackOff, notify Notify) error
- type BackOff
- type BackOffContext
- type Clock
- type ConstantBackOff
- type ExponentialBackOff
- type Notify
- type Operation
- type PermanentError
- type StopBackOff
- type ZeroBackOff
Constants ¶
const ( DefaultInitialInterval = 500 * time.Millisecond DefaultRandomizationFactor = 0.5 DefaultMultiplier = 1.5 DefaultMaxInterval = 60 * time.Second DefaultMaxElapsedTime = 15 * time.Minute )
Default values for ExponentialBackOff.
const Stop time.Duration = -1
停止标识
Variables ¶
var SystemClock = systemClock{}
SystemClock implements Clock interface that uses time.Now().
Functions ¶
Types ¶
type BackOff ¶
type BackOff interface { // NextBackOff 返回retry前等待的时间 // 或返回 backoff.Stop 判断停止后是否要做一些事情. // // Example usage: // // duration := backoff.NextBackOff(); // if (duration == backoff.Stop) { // // Do not retry operation. // } else { // // Sleep for duration and retry operation. // } // NextBackOff() time.Duration // 重置状态. Reset() }
type BackOffContext ¶
在取消context后停止retry
func WithContext ¶
func WithContext(b BackOff, ctx context.Context) BackOffContext
WithContext 返回一个带context的 BackOffContext
ctx 不能为 nil
type ConstantBackOff ¶
ConstantBackOff 重试的时间间隔相同,会不断的重试,重试的时间间隔会随着调用NextBackOff() 而增长
func NewConstantBackOff ¶
func NewConstantBackOff(d time.Duration) *ConstantBackOff
func (*ConstantBackOff) NextBackOff ¶
func (b *ConstantBackOff) NextBackOff() time.Duration
func (*ConstantBackOff) Reset ¶
func (b *ConstantBackOff) Reset()
type ExponentialBackOff ¶
type ExponentialBackOff struct { InitialInterval time.Duration RandomizationFactor float64 Multiplier float64 MaxInterval time.Duration // After MaxElapsedTime the ExponentialBackOff stops. // It never stops if MaxElapsedTime == 0. MaxElapsedTime time.Duration Clock Clock // contains filtered or unexported fields }
ExponentialBackOff is a backoff implementation that increases the backoff period for each retry attempt using a randomization function that grows exponentially.
NextBackOff() is calculated using the following formula:
randomized interval = RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
In other words NextBackOff() will range between the randomization factor percentage below and above the retry interval.
For example, given the following parameters:
RetryInterval = 2 RandomizationFactor = 0.5 Multiplier = 2
the actual backoff period used in the next retry attempt will range between 1 and 3 seconds, multiplied by the exponential, that is, between 2 and 6 seconds.
Note: MaxInterval caps the RetryInterval and not the randomized interval.
If the time elapsed since an ExponentialBackOff instance is created goes past the MaxElapsedTime, then the method NextBackOff() starts returning backoff.Stop.
The elapsed time can be reset by calling Reset().
Example: Given the following default arguments, for 10 tries the sequence will be, and assuming we go over the MaxElapsedTime on the 10th try:
Request # RetryInterval (seconds) Randomized Interval (seconds) 1 0.5 [0.25, 0.75] 2 0.75 [0.375, 1.125] 3 1.125 [0.562, 1.687] 4 1.687 [0.8435, 2.53] 5 2.53 [1.265, 3.795] 6 3.795 [1.897, 5.692] 7 5.692 [2.846, 8.538] 8 8.538 [4.269, 12.807] 9 12.807 [6.403, 19.210] 10 19.210 backoff.Stop
Note: Implementation is not thread-safe.
func NewExponentialBackOff ¶
func NewExponentialBackOff() *ExponentialBackOff
NewExponentialBackOff creates an instance of ExponentialBackOff using default values.
func NewServiceBackOff ¶
func NewServiceBackOff() *ExponentialBackOff
NewServiceBackOff 返回一个贴合项目需要的backoff
func (*ExponentialBackOff) GetElapsedTime ¶
func (b *ExponentialBackOff) GetElapsedTime() time.Duration
GetElapsedTime returns the elapsed time since an ExponentialBackOff instance is created and is reset when Reset() is called.
The elapsed time is computed using time.Now().UnixNano(). It is safe to call even while the backoff policy is used by a running ticker.
func (*ExponentialBackOff) NextBackOff ¶
func (b *ExponentialBackOff) NextBackOff() time.Duration
NextBackOff calculates the next backoff interval using the formula:
Randomized interval = RetryInterval +/- (RandomizationFactor * RetryInterval)
func (*ExponentialBackOff) Reset ¶
func (b *ExponentialBackOff) Reset()
Reset the interval back to the initial retry interval and restarts the timer.
type PermanentError ¶
type PermanentError struct {
Err error
}
PermanentError 错误信号
func Permanent ¶
func Permanent(err error) *PermanentError
func (*PermanentError) Error ¶
func (e *PermanentError) Error() string
type StopBackOff ¶
type StopBackOff struct{}
停止retry
func (*StopBackOff) NextBackOff ¶
func (b *StopBackOff) NextBackOff() time.Duration
func (*StopBackOff) Reset ¶
func (b *StopBackOff) Reset()
type ZeroBackOff ¶
type ZeroBackOff struct{}
ZeroBackOff backoff时间为0,不等待直接retry
func (*ZeroBackOff) NextBackOff ¶
func (b *ZeroBackOff) NextBackOff() time.Duration
func (*ZeroBackOff) Reset ¶
func (b *ZeroBackOff) Reset()