retry

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Permanent is a wrapper function when you don't want retry
	// with some inner error.
	// 对于某些不想重试的错误, 可以用次方法包裹该错误, retry 便不再重试.
	Permanent = backoff.Permanent
)

Functions

func NotifyRecover

func NotifyRecover(operation backoff.Operation, b backoff.BackOff, notify NotifyWithTimes, recovered func(times int), verbose bool) error

NotifyRecover is a wrapper around backoff.RetryNotify that adds another callback for when an operation previously failed but has since recovered. The main purpose of this wrapper is to call `notify` only when the operations fails the first time and `recovered` when it finally succeeds. This can be helpful in limiting log messages to only the events that operators need to be alerted on.

func RetryWithLog

func RetryWithLog(ctx context.Context, operation backoff.Operation, b backoff.BackOff, taskId string) error

RetryWithLog 是对于 NotifyRecover 函数的封装, 会在第一次失败时和最终成功时打印日志, 优先使用此函数. 这个 ctx 只是为了拿到 blog, 不会自动传入 retry, 如果想依赖 context 生命周期请使用 NewBackOffWithContext.

Types

type Config

type Config struct {
	Policy PolicyType

	// Constant back off
	Duration time.Duration

	// Exponential back off
	InitialInterval     time.Duration
	RandomizationFactor float32
	Multiplier          float32
	MaxInterval         time.Duration
	MaxElapsedTime      time.Duration

	// Additional options
	MaxRetries int64
}

Config encapsulates the back off policy configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig represents the default configuration for a `Config`.

func DefaultConfigWithNoRetry

func DefaultConfigWithNoRetry() Config

DefaultConfigWithNoRetry represents the default configuration with `MaxRetries` set to 0. This may be useful for those brokers which can handles retries on its own.

func (*Config) NewBackOff

func (c *Config) NewBackOff() backoff.BackOff

func (*Config) NewBackOffWithContext

func (c *Config) NewBackOffWithContext(ctx context.Context) backoff.BackOff

NewBackOffWithContext 为 backoff 增加 ctx 控制, 可以 timeout 和 cancel

type NotifyWithTimes

type NotifyWithTimes func(err error, duration time.Duration, times int)

type PolicyType

type PolicyType int

PolicyType denotes if the back off delay should be constant or exponential.

const (
	// PolicyConstant is a backoff policy that always returns the same backoff delay.
	PolicyConstant PolicyType = iota + 1
	// PolicyExponential is a backoff implementation that increases the backoff period
	// for each retry attempt using a randomization function that grows exponentially.
	PolicyExponential
)

Jump to

Keyboard shortcuts

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