retry

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorAbort                 = errors.New("stop retry")
	ErrorTimeout               = errors.New("retry timeout")
	ErrorContextDeadlineExceed = errors.New("context deadline exceeded")
	ErrorEmptyRetryFunc        = errors.New("empty retry function")
	ErrorTimeFormat            = errors.New("time out err")
)
View Source
var (
	DefaultMaxRetryTimes = 3
	DefaultTimeout       = time.Minute
	DefaultInterval      = time.Second * 2
	DefaultRetryChecker  = func(err error) bool {
		return !errors.Is(err, ErrorAbort)
	}
)

Functions

func Do

func Do(ctx context.Context, fn RetriesFunc, opts ...Option) error

Types

type BackoffStrategy

type BackoffStrategy int
const (
	StrategyConstant BackoffStrategy = iota
	StrategyLinear
	StrategyFibonacci
)

type Config

type Config struct {
	MaxRetryTimes int
	Timeout       time.Duration
	RetryChecker  RetriesChecker
	Strategy      Strategy
	RecoverPanic  bool
	BeforeTry     HookFunc
	AfterTry      HookFunc
}

type Constant

type Constant struct {
	// contains filtered or unexported fields
}

func NewConstant

func NewConstant(d time.Duration) *Constant

func (*Constant) Sleep

func (c *Constant) Sleep(_ int) time.Duration

type Fibonacci

type Fibonacci struct {
	// contains filtered or unexported fields
}

func NewFibonacci

func NewFibonacci(d time.Duration) *Fibonacci

func (*Fibonacci) Sleep

func (f *Fibonacci) Sleep(times int) time.Duration

type HookFunc

type HookFunc func()

type Linear

type Linear struct {
	// contains filtered or unexported fields
}

func NewLinear

func NewLinear(d time.Duration) *Linear

func (*Linear) Sleep

func (l *Linear) Sleep(times int) time.Duration

type Option

type Option func(c *Config)

func WithAfterHook

func WithAfterHook(hook HookFunc) Option

func WithBackOffStrategy

func WithBackOffStrategy(s BackoffStrategy, duration time.Duration) Option

func WithBeforeHook

func WithBeforeHook(hook HookFunc) Option

func WithCustomStrategy

func WithCustomStrategy(s Strategy) Option

func WithMaxRetryTimes

func WithMaxRetryTimes(times int) Option

func WithRecoverPanic

func WithRecoverPanic() Option

func WithRetryChecker

func WithRetryChecker(checker RetriesChecker) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

type RetriesChecker

type RetriesChecker func(err error) (needRetry bool)

type RetriesFunc

type RetriesFunc func() error

type Strategy

type Strategy interface {
	Sleep(times int) time.Duration
}

Jump to

Keyboard shortcuts

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