retry

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBackOff = BackOff{
	Rand:   rand.New(rand.NewSource(time.Now().UnixNano())),
	Min:    500 * time.Millisecond,
	Max:    5 * time.Second,
	Jitter: 0.2,
	Factor: 2,
}
View Source
var OnRetryable = Policy{
	Interval: DefaultBackOff,
	OnCodes:  RetryableCodes,
	Attempts: 0,
}

OnRetryable is intended to be used by clients interacting with a duh rpc service. It will retry indefinitely as long as the service returns a retryable error. Users who which to cancel the indefinite retry should cancel the context.

View Source
var RetryableCodes = []int{duh.CodeRetryRequest, duh.CodeTooManyRequests, duh.CodeInternalError,
	http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout}

RetryableCodes is a list of duh return codes which are retryable.

View Source
var Twice = Policy{
	Interval: DefaultBackOff,
	Attempts: 2,
}

Twice policy will retry 'twice' if there was an error. Uses the default back off policy

View Source
var UntilSuccess = Policy{
	Interval: DefaultBackOff,
	Attempts: 0,
}

Functions

func On

func On(ctx context.Context, p Policy, operation func(context.Context, int) error) error

Types

type BackOff

type BackOff struct {
	Min    time.Duration
	Max    time.Duration
	Factor float64
	Jitter float64
	Rand   *rand.Rand
}

func (BackOff) Next

func (b BackOff) Next(attempts int) time.Duration

type Interval

type Interval interface {
	Next(attempts int) time.Duration
}

type Policy

type Policy struct {
	// Interval is an interface which dictates how long the retry should sleep between attempts. Retry comes with
	// two implementations called retry.BackOff which implements a backoff and retry.Sleep which is a static sleep
	// value with no backoff.
	//
	// 	backoffPolicy := retry.Policy{
	//		Interval: retry.BackOff{
	//			Min:    time.Millisecond,
	//			Max:    time.Millisecond * 100,
	//			Factor: 2,
	//		},
	//		Attempts: 5,
	//	}
	//
	// 	sleepPolicy := retry.Policy{
	//		Interval: retry.Sleep(5 * time.Seconds),
	//		Attempts: 5,
	//	}
	//
	Interval Interval // BackOff or Sleep
	// OnCodes is a list of codes which will cause a retry. If an error occurs which is not an implementation
	// of duh.Error and OnCodes then a retry will NOT occur.
	OnCodes []int
	// Attempts is the number of "attempts" before retry returns an error to the caller.
	// Attempts includes the first attempt, it is a count of the number of "total attempts" that
	// will be attempted.
	Attempts int // 0 for infinite
}

type Sleep

type Sleep time.Duration

func (Sleep) Next

func (s Sleep) Next(_ int) time.Duration

Jump to

Keyboard shortcuts

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