retry

package
v0.0.66 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

View Source
const Infinite = -1

Variables

View Source
var (
	ErrReachedMaxRetries = errors.New("retry: reached max retries")
	ErrUnretryableError  = errors.New("retry: unretryable error")
)

Functions

This section is empty.

Types

type Backoff

type Backoff func(initialInterval time.Duration, retries int) (intervalForThisRetry time.Duration)

func DefaultBackoff

func DefaultBackoff() Backoff

type Config

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

func NewConfig

func NewConfig(initialInterval, maxInterval time.Duration, opts ...Option) *Config

func (*Config) Build

func (c *Config) Build(ctx context.Context) *Retryer

type DoOption

type DoOption func(c *doConfig)

func WithErrorHandler added in v0.0.63

func WithErrorHandler(f func(ctx context.Context, r *Retryer, err error)) DoOption

func WithRetryableErrors added in v0.0.63

func WithRetryableErrors(errs ...error) DoOption

If UnretryableErrors and RetryableErrors are both applied, UnretryableErrors will be prioritized.

func WithUnretryableErrors

func WithUnretryableErrors(errs ...error) DoOption

If UnretryableErrors and RetryableErrors are both applied, UnretryableErrors will be prioritized.

type Jitter

type Jitter func(duration time.Duration) (durationWithJitter time.Duration)

func DefaultJitter

func DefaultJitter(opts ...JitterOption) Jitter

type JitterOption

type JitterOption func(j *jitterConfig)

func WithDefaultJitterRand added in v0.0.62

func WithDefaultJitterRand(rnd *rand.Rand) JitterOption

func WithDefaultJitterRange added in v0.0.63

func WithDefaultJitterRange(minJitter, maxJitter time.Duration) JitterOption

type Option

type Option func(c *Config)

func WithBackoff

func WithBackoff(backoffFunc Backoff) Option

func WithJitter

func WithJitter(jitterFunc Jitter) Option

func WithMaxRetries

func WithMaxRetries(maxRetries int) Option

type Retryer

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

WARNING: Retryer should not be used across goroutines. Generate Retryer from Config for each goroutine.

func New

func New(ctx context.Context, config *Config) *Retryer

New returns *retry.Retryer. *retry.Retryer provides methods to facilitate retry execution.

WARNING: Retryer should not be used across goroutines. Generate Retryer from Config for each goroutine.

Is used as follows:

ctx := context.Background()
c := retry.NewConfig(10*time.Millisecond, 500*time.Millisecond, retry.WithMaxRetries(10))
r := retry.New(ctx, c)

for r.Retry() {
	if r.Retries() == 0 {
		fmt.Printf("FIRSTTRY! time=%s retries=%d retryAfter=%s\n", time.Now(), r.Retries(), r.RetryAfter())
		continue
	}
	fmt.Printf("RETRYING! time=%s retries=%d retryAfter=%s\n", time.Now(), r.Retries(), r.RetryAfter())
}
fmt.Printf("COMPLETE! time=%s retries=%d error=%v\n", time.Now(), r.Retries(), r.Err())

Then, yields the following results:

FIRSTTRY! time=2009-11-10 23:00:00 +0000 UTC m=+0.000000001 retries=0 retryAfter=10ms
RETRYING! time=2009-11-10 23:00:00.01 +0000 UTC m=+0.010000001 retries=1 retryAfter=20ms
RETRYING! time=2009-11-10 23:00:00.03 +0000 UTC m=+0.030000001 retries=2 retryAfter=40ms
RETRYING! time=2009-11-10 23:00:00.07 +0000 UTC m=+0.070000001 retries=3 retryAfter=80ms
RETRYING! time=2009-11-10 23:00:00.15 +0000 UTC m=+0.150000001 retries=4 retryAfter=160ms
RETRYING! time=2009-11-10 23:00:00.31 +0000 UTC m=+0.310000001 retries=5 retryAfter=320ms
RETRYING! time=2009-11-10 23:00:00.63 +0000 UTC m=+0.630000001 retries=6 retryAfter=500ms
RETRYING! time=2009-11-10 23:00:01.13 +0000 UTC m=+1.130000001 retries=7 retryAfter=500ms
RETRYING! time=2009-11-10 23:00:01.63 +0000 UTC m=+1.630000001 retries=8 retryAfter=500ms
RETRYING! time=2009-11-10 23:00:02.13 +0000 UTC m=+2.130000001 retries=9 retryAfter=500ms
RETRYING! time=2009-11-10 23:00:02.63 +0000 UTC m=+2.630000001 retries=10 retryAfter=500ms
COMPLETE! time=2009-11-10 23:00:02.63 +0000 UTC m=+2.630000001 retries=10 error=retry: reached max retries

If the maximum count of attempts is not given via retry.WithMaxRetries(), *retry.Retryer that retry.New() returned will retry infinitely many times.

func (*Retryer) Do

func (r *Retryer) Do(f func(ctx context.Context) error, opts ...DoOption) error

func (*Retryer) Err

func (r *Retryer) Err() (reason error)

func (*Retryer) MaxRetries added in v0.0.63

func (r *Retryer) MaxRetries() (retries int)

func (*Retryer) Retries

func (r *Retryer) Retries() (retries int)

func (*Retryer) Retry

func (r *Retryer) Retry() bool

func (*Retryer) RetryAfter

func (r *Retryer) RetryAfter() (retryAfter time.Duration)

Jump to

Keyboard shortcuts

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