backoff

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2024 License: MIT Imports: 8 Imported by: 21

Documentation

Index

Constants

View Source
const NoBackoff = time.Duration(-1)

NoBackoff is used to represent backoff when no cron backoff is needed

View Source
const (
	// NoInterval represents Maximim interval
	NoInterval = 0
)

Variables

View Source
var SystemClock = systemClock{}

SystemClock implements Clock interface that uses time.Now().

Functions

func GetBackoffForNextSchedule added in v0.6.0

func GetBackoffForNextSchedule(
	sched cron.Schedule,
	startTime time.Time,
	closeTime time.Time,
	jitterStartSeconds int32,
) (time.Duration, error)

GetBackoffForNextSchedule calculates the backoff time for the next run given a cronSchedule, workflow start time and workflow close time

func GetBackoffForNextScheduleInSeconds added in v0.6.0

func GetBackoffForNextScheduleInSeconds(
	cronSchedule string,
	startTime time.Time,
	closeTime time.Time,
	jitterStartSeconds int32,
) (int32, error)

GetBackoffForNextScheduleInSeconds calculates the backoff time in seconds for the next run given a cronSchedule and current time

func IgnoreErrors

func IgnoreErrors(errorsToExclude []error) func(error) bool

IgnoreErrors can be used as IsRetryable handler for Retry function to exclude certain errors from the retry list

func JitDuration added in v0.9.3

func JitDuration(duration time.Duration, coefficient float64) time.Duration

JitDuration return random duration from (1-coefficient)*duration to (1+coefficient)*duration, inclusive, exclusive

func JitFloat64 added in v0.9.3

func JitFloat64(input float64, coefficient float64) float64

JitFloat64 return random number from (1-coefficient)*input to (1+coefficient)*input, inclusive, exclusive

func JitInt64 added in v0.9.3

func JitInt64(input int64, coefficient float64) int64

JitInt64 return random number from (1-coefficient)*input to (1+coefficient)*input, inclusive, exclusive

func ValidateSchedule added in v0.6.0

func ValidateSchedule(cronSchedule string) (cron.Schedule, error)

ValidateSchedule validates a cron schedule spec

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock used by ExponentialRetryPolicy implementation to get the current time. Mainly used for unit testing

type ConcurrentRetrier

type ConcurrentRetrier struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ConcurrentRetrier is used for client-side throttling. It determines whether to throttle outgoing traffic in case downstream backend server rejects requests due to out-of-quota or server busy errors.

func NewConcurrentRetrier

func NewConcurrentRetrier(retryPolicy RetryPolicy) *ConcurrentRetrier

NewConcurrentRetrier returns an instance of concurrent backoff retrier.

func (*ConcurrentRetrier) Failed

func (c *ConcurrentRetrier) Failed()

Failed marks client request failed because backend is busy.

func (*ConcurrentRetrier) Succeeded

func (c *ConcurrentRetrier) Succeeded()

Succeeded marks client request succeeded.

func (*ConcurrentRetrier) Throttle

func (c *ConcurrentRetrier) Throttle()

Throttle Sleep if there were failures since the last success call.

type ExponentialRetryPolicy

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

ExponentialRetryPolicy provides the implementation for retry policy using a coefficient to compute the next delay. Formula used to compute the next delay is: initialInterval * math.Pow(backoffCoefficient, currentAttempt)

func NewExponentialRetryPolicy

func NewExponentialRetryPolicy(initialInterval time.Duration) *ExponentialRetryPolicy

NewExponentialRetryPolicy returns an instance of ExponentialRetryPolicy using the provided initialInterval

func (*ExponentialRetryPolicy) ComputeNextDelay

func (p *ExponentialRetryPolicy) ComputeNextDelay(elapsedTime time.Duration, numAttempts int) time.Duration

ComputeNextDelay returns the next delay interval. This is used by Retrier to delay calling the operation again

func (*ExponentialRetryPolicy) SetBackoffCoefficient

func (p *ExponentialRetryPolicy) SetBackoffCoefficient(backoffCoefficient float64)

SetBackoffCoefficient sets the coefficient used by ExponentialRetryPolicy to compute next delay for each retry All retries are computed using the following formula: initialInterval * math.Pow(backoffCoefficient, currentAttempt)

func (*ExponentialRetryPolicy) SetExpirationInterval

func (p *ExponentialRetryPolicy) SetExpirationInterval(expirationInterval time.Duration)

SetExpirationInterval sets the absolute expiration interval for all retries

func (*ExponentialRetryPolicy) SetInitialInterval

func (p *ExponentialRetryPolicy) SetInitialInterval(initialInterval time.Duration)

SetInitialInterval sets the initial interval used by ExponentialRetryPolicy for the very first retry All later retries are computed using the following formula: initialInterval * math.Pow(backoffCoefficient, currentAttempt)

func (*ExponentialRetryPolicy) SetMaximumAttempts

func (p *ExponentialRetryPolicy) SetMaximumAttempts(maximumAttempts int)

SetMaximumAttempts sets the maximum number of retry attempts

func (*ExponentialRetryPolicy) SetMaximumInterval

func (p *ExponentialRetryPolicy) SetMaximumInterval(maximumInterval time.Duration)

SetMaximumInterval sets the maximum interval for each retry

type IsRetryable

type IsRetryable func(error) bool

IsRetryable handler can be used to exclude certain errors during retry

type MultiPhasesRetryPolicy added in v0.16.0

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

MultiPhasesRetryPolicy implements a policy that first use one policy to get next delay, and once expired use the next policy for the following retry. It can achieve fast retries in first phase then slowly retires in second phase. The supported retry policy is ExponentialRetryPolicy. To have the correct next delay, set the maximumAttempts in the non-final policy.

func NewMultiPhasesRetryPolicy added in v0.16.0

func NewMultiPhasesRetryPolicy(policies ...*ExponentialRetryPolicy) *MultiPhasesRetryPolicy

NewMultiPhasesRetryPolicy creates MultiPhasesRetryPolicy

func (MultiPhasesRetryPolicy) ComputeNextDelay added in v0.16.0

func (tp MultiPhasesRetryPolicy) ComputeNextDelay(elapsedTime time.Duration, numAttempts int) time.Duration

ComputeNextDelay returns the next delay interval.

type Operation

type Operation func() error

Operation to retry

type Retrier

type Retrier interface {
	NextBackOff() time.Duration
	Reset()
}

Retrier manages the state of retry operation

func NewRetrier

func NewRetrier(policy RetryPolicy, clock Clock) Retrier

NewRetrier is used for creating a new instance of Retrier

type RetryPolicy

type RetryPolicy interface {
	ComputeNextDelay(elapsedTime time.Duration, numAttempts int) time.Duration
}

RetryPolicy is the API which needs to be implemented by various retry policy implementations

type ThrottleRetry added in v0.24.0

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

ThrottleRetry is used to run operation with retry and also avoid throttling dependencies

func NewThrottleRetry added in v0.24.0

func NewThrottleRetry(opts ...ThrottleRetryOption) *ThrottleRetry

NewThrottleRetry returns a retry handler with given options

func (*ThrottleRetry) Do added in v0.24.0

func (tr *ThrottleRetry) Do(ctx context.Context, op Operation) error

Do function can be used to wrap any call with retry logic

type ThrottleRetryOption added in v0.24.0

type ThrottleRetryOption func(*ThrottleRetry)

ThrottleRetryOption sets the options of ThrottleRetry

func WithRetryPolicy added in v0.24.0

func WithRetryPolicy(policy RetryPolicy) ThrottleRetryOption

WithRetryPolicy returns a setter setting the retry policy of ThrottleRetry

func WithRetryableError added in v0.24.0

func WithRetryableError(isRetryable IsRetryable) ThrottleRetryOption

WithRetryableError returns a setter setting the retryable error of ThrottleRetry

func WithThrottleError added in v0.24.0

func WithThrottleError(isThrottle IsRetryable) ThrottleRetryOption

WithThrottleError returns a setter setting the throttle error of ThrottleRetry

func WithThrottlePolicy added in v0.24.0

func WithThrottlePolicy(throttlePolicy RetryPolicy) ThrottleRetryOption

WithThrottlePolicy returns setter setting the retry policy when operation returns throttle error

Jump to

Keyboard shortcuts

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