retry

package
v0.0.59-rc.7 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

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

type DoOption

type DoOption func(c *doConfig)

func WithRetryableErrorHandler

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

func WithUnretryableErrors

func WithUnretryableErrors(errs []error) DoOption

type Jitter

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

func DefaultJitter

func DefaultJitter(minJitter, maxJitter time.Duration, opts ...JitterOption) Jitter

type JitterOption

type JitterOption func(j *jitter)

func WithJitterRand

func WithJitterRand(rnd *rand.Rand) 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
}

func New

func New(config *Config) *Retryer

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

Is used as follows ( https://go.dev/play/p/nIWIWq-ib6b ):

c := retry.NewConfig(10*time.Millisecond, 500*time.Millisecond, retry.WithMaxRetries(10))
r := retry.New(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(ctx context.Context, do func(ctx context.Context) error, opts ...DoOption) (err error)

func (*Retryer) Err

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

func (*Retryer) Retries

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

func (*Retryer) Retry

func (r *Retryer) Retry(ctx context.Context) 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