backoff

package
v0.0.0-...-3d0adf2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2019 License: MIT Imports: 3 Imported by: 0

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

View Source
const (
	DefaultInitialInterval     = 500 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	DefaultMultiplier          = 1.5
	DefaultMaxInterval         = 60 * time.Second
	DefaultMaxElapsedTime      = 15 * time.Minute
)

Default values for ExponentialBackOff.

View Source
const Stop time.Duration = -1

停止标识

Variables

View Source
var SystemClock = systemClock{}

SystemClock implements Clock interface that uses time.Now().

Functions

func Retry

func Retry(o Operation, b BackOff) error

重试有可能失败的操作

func RetryNotify

func RetryNotify(operation Operation, b BackOff, notify Notify) error

带提醒操作的retry

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

type BackOffContext interface {
	BackOff
	Context() context.Context
}

在取消context后停止retry

func WithContext

func WithContext(b BackOff, ctx context.Context) BackOffContext

WithContext 返回一个带context的 BackOffContext

ctx 不能为 nil

type Clock

type Clock interface {
	Now() time.Time
}

Clock is an interface that returns current time for BackOff.

type ConstantBackOff

type ConstantBackOff struct {
	Interval time.Duration
}

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 Notify

type Notify func(error, time.Duration)

retrynotify 的提醒回掉方法,不论成功失败都会执行

type Operation

type Operation func() error

retry或retryNorify执行的事情

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()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL