Documentation ¶
Index ¶
- Variables
- func ExponentialBackoff(power int64) time.Duration
- func FixedBackoff(interval int64) time.Duration
- func RandomBackoff(maxInterval int64) time.Duration
- type BackoffFunc
- type Callback
- type Config
- func (c *Config) WithAttempts(attempts uint64) *Config
- func (c *Config) WithAttemptsByError(attemptsByError map[error]uint64) *Config
- func (c *Config) WithBackOffFunc(backoff BackoffFunc) *Config
- func (c *Config) WithCallback(cb Callback) *Config
- func (c *Config) WithContext(ctx context.Context) *Config
- func (c *Config) WithDetail(detail bool) *Config
- func (c *Config) WithFactor(factor float64) *Config
- func (c *Config) WithInitDelay(delay time.Duration) *Config
- func (c *Config) WithJitter(jitter float64) *Config
- func (c *Config) WithRetryIfFunc(retryIf RetryIfFunc) *Config
- type Result
- type Retry
- type RetryIfFunc
- type RetryResult
- type RetryableFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorRetryIf 表示重试检查函数的结果为FALSE的错误 // ErrorRetryIf represents an error when the retry check function result is FALSE ErrorRetryIf = errors.New("retry check func result is FALSE") // ErrorRetryAttemptsExceeded 表示重试次数超过限制的错误 // ErrorRetryAttemptsExceeded represents an error when the retry attempts exceeded the limit ErrorRetryAttemptsExceeded = errors.New("retry attempts exceeded") // ErrorRetryAttemptsByErrorExceeded 表示由于特定错误导致的重试次数超过限制的错误 // ErrorRetryAttemptsByErrorExceeded represents an error when the retry attempts exceeded the limit due to a specific error ErrorRetryAttemptsByErrorExceeded = errors.New("retry attempts by spec error exceeded") // ErrorExecErrByIndexOutOfBound 表示由于索引越界导致的执行错误 // ErrorExecErrByIndexOutOfBound represents an execution error caused by index out of bound ErrorExecErrByIndexOutOfBound = errors.New("exec error by index out of bound") // ErrorExecErrNotFound 表示未找到执行错误 // ErrorExecErrNotFound represents an error when the execution error is not found ErrorExecErrNotFound = errors.New("exec error not found") )
Functions ¶
func ExponentialBackoff ¶ added in v0.1.9
ExponentialBackoff 返回指数增长的退避策略 ExponentialBackoff returns an exponential backoff strategy
func FixedBackoff ¶ added in v0.1.9
FixedBackoff 返回固定时间间隔的退避策略 FixedBackoff returns a fixed-interval backoff strategy
func RandomBackoff ¶ added in v0.1.9
RandomBackoff 返回随机时间间隔的退避策略 RandomBackoff returns a random-interval backoff strategy
Types ¶
type BackoffFunc ¶
BackoffFunc 定义了退避策略函数的类型 BackoffFunc defines the type for backoff strategy functions
func CombineBackoffs ¶ added in v0.1.9
func CombineBackoffs(backoffs ...BackoffFunc) BackoffFunc
CombineBackoffs 将多个退避策略组合成一个 CombineBackoffs combines multiple backoff strategies into one
type Callback ¶
type Callback interface { // OnRetry 方法在每次重试时调用,传入当前的重试次数、延迟时间和错误信息 // The OnRetry method is called on each retry, passing in the current retry count, delay time, and error information OnRetry(count int64, delay time.Duration, err error) }
Callback 接口用于定义重试回调函数 The Callback interface is used to define the retry callback function.
func NewEmptyCallback ¶ added in v0.1.4
func NewEmptyCallback() Callback
NewEmptyCallback 函数返回一个新的空回调实例 The NewEmptyCallback function returns a new empty callback instance
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config 结构体定义了重试的配置 The Config structure defines the configuration for retries
func DefaultConfig ¶ added in v0.1.1
func DefaultConfig() *Config
DefaultConfig 函数返回一个新的默认配置的 Config 实例 The DefaultConfig function returns a new Config instance with the default configuration
func FixConfig ¶ added in v0.1.1
func FixConfig() *Config
FixConfig 函数返回一个新的固定退避时间的 Config 实例 The FixConfig function returns a new Config instance with a fixed backoff time
func NewConfig ¶
func NewConfig() *Config
NewConfig 函数返回一个新的 Config 实例,使用默认的配置 The NewConfig function returns a new Config instance with the default configuration
func (*Config) WithAttempts ¶
WithAttempts 方法设置 Config 的重试次数并返回 Config 实例 The WithAttempts method sets the number of retries of the Config and returns the Config instance
func (*Config) WithAttemptsByError ¶
WithAttemptsByError 方法设置 Config 的错误重试次数并返回 Config 实例 The WithAttemptsByError method sets the number of error retries of the Config and returns the Config instance
func (*Config) WithBackOffFunc ¶ added in v0.1.4
func (c *Config) WithBackOffFunc(backoff BackoffFunc) *Config
WithBackOffFunc 方法设置 Config 的退避函数并返回 Config 实例 The WithBackOffFunc method sets the backoff function of the Config and returns the Config instance
func (*Config) WithCallback ¶
WithCallback 方法设置 Config 的回调函数并返回 Config 实例 The WithCallback method sets the callback function of the Config and returns the Config instance
func (*Config) WithContext ¶
WithContext 方法设置 Config 的上下文并返回 Config 实例 The WithContext method sets the context of the Config and returns the Config instance
func (*Config) WithDetail ¶
WithDetail 方法设置 Config 的详细错误信息显示选项并返回 Config 实例 The WithDetail method sets the detailed error information display option of the Config and returns the Config instance
func (*Config) WithFactor ¶
WithFactor 方法设置 Config 的因子并返回 Config 实例 The WithFactor method sets the factor of the Config and returns the Config instance
func (*Config) WithInitDelay ¶ added in v0.1.4
WithInitDelay 方法设置 Config 的初始延迟时间并返回 Config 实例 The WithInitDelay method sets the initial delay time of the Config and returns the Config instance
func (*Config) WithJitter ¶
WithJitter 方法设置 Config 的抖动并返回 Config 实例 The WithJitter method sets the jitter of the Config and returns the Config instance
func (*Config) WithRetryIfFunc ¶ added in v0.1.4
func (c *Config) WithRetryIfFunc(retryIf RetryIfFunc) *Config
WithRetryIfFunc 方法设置 Config 的重试条件函数并返回 Config 实例 The WithRetryIfFunc method sets the retry condition function of the Config and returns the Config instance
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result 结构体用于存储执行结果 The Result struct is used to store the execution result
func NewResult ¶
func NewResult() *Result
NewResult 函数用于创建一个新的 Result 实例 The NewResult function is used to create a new Result instance
func (*Result) ExecErrorByIndex ¶
ExecErrorByIndex 方法返回指定索引处的执行错误 The ExecErrorByIndex method returns the execution error at the specified index
func (*Result) ExecErrors ¶
ExecErrors 方法返回所有执行错误的列表 The ExecErrors method returns a list of all execution errors
func (*Result) FirstExecError ¶
FirstExecError 方法返回第一次执行的错误 The FirstExecError method returns the error of the first execution
func (*Result) IsSuccess ¶
IsSuccess 方法返回执行是否成功 The IsSuccess method returns whether the execution was successful
func (*Result) LastExecError ¶
LastExecError 方法返回最后一次执行的错误 The LastExecError method returns the error of the last execution
type Retry ¶ added in v0.1.2
type Retry struct {
// contains filtered or unexported fields
}
Retry 结构体用于定义重试的配置 The Retry struct is used to define the retry configuration
func New ¶ added in v0.1.2
New 函数用于创建一个新的 Retry 实例。它接受一个 Config 结构体作为参数,该结构体包含了重试的配置信息。 The New function is used to create a new Retry instance. It accepts a Config structure as a parameter, which contains the configuration information for retrying.
func (*Retry) TryOnConflict ¶ added in v0.1.2
func (r *Retry) TryOnConflict(fn RetryableFunc) *Result
TryOnConflict 方法尝试执行 fn 函数,如果遇到冲突则进行重试 The TryOnConflict method attempts to execute the fn function, and retries if a conflict is encountered
func (*Retry) TryOnConflictVal ¶ added in v0.1.5
func (r *Retry) TryOnConflictVal(fn RetryableFunc) RetryResult
TryOnConflict 方法尝试执行 RetryableFunc 函数,如果发生冲突,则进行重试 The TryOnConflict method tries to execute the RetryableFunc function, and retries if a conflict occurs
type RetryIfFunc ¶
RetryIfFunc 类型定义了一个接受错误并返回布尔值的函数类型 The RetryIfFunc type defines a function type that accepts an error and returns a boolean value
type RetryResult ¶ added in v0.1.5
type RetryResult = interface { // Data 方法返回执行结果的数据 // The Data method returns the data of the execution result Data() any // TryError 方法返回尝试执行时的错误 // The TryError method returns the error when trying to execute TryError() error // ExecErrors 方法返回所有执行错误的列表 // The ExecErrors method returns a list of all execution errors ExecErrors() []error // IsSuccess 方法返回执行是否成功 // The IsSuccess method returns whether the execution was successful IsSuccess() bool // LastExecError 方法返回最后一次执行的错误 // The LastExecError method returns the error of the last execution LastExecError() error // FirstExecError 方法返回第一次执行的错误 // The FirstExecError method returns the error of the first execution FirstExecError() error // ExecErrorByIndex 方法返回指定索引处的执行错误 // The ExecErrorByIndex method returns the execution error at the specified index ExecErrorByIndex(idx int) error // Count 方法返回执行的次数 // The Count method returns the number of executions Count() int64 }
RetryResult 接口定义了执行结果的相关方法 The RetryResult interface defines methods related to execution results
func Do ¶
func Do(fn RetryableFunc, conf *Config) RetryResult
Do 函数尝试执行 fn 函数,如果遇到冲突则根据 conf 配置进行重试 The Do function attempts to execute the fn function, and retries according to the conf configuration if a conflict is encountered
func DoWithDefault ¶
func DoWithDefault(fn RetryableFunc) RetryResult
DoWithDefault 函数尝试执行 fn 函数,如果遇到冲突则使用默认配置进行重试 The DoWithDefault function attempts to execute the fn function, and retries with the default configuration if a conflict is encountered
type RetryableFunc ¶
RetryableFunc 类型定义了一个可重试的函数 The RetryableFunc type defines a retryable function