backoff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0 Package backoff provides an exponential-backoff implementation. https://github.com/jpillora/backoff inlined for customizations.

Copyright 2020 New Relic Corporation. All rights reserved. SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	DefaultFactor = 2
	DefaultJitter = true
	DefaultMin    = 1 * time.Second
	DefaultMax    = 5 * time.Minute
)

Default values

Variables

This section is empty.

Functions

func GetMaxBackoffByCause

func GetMaxBackoffByCause(cause backendhttp.ErrorCause) time.Duration

GetMaxBackoffByCause will return the maximum backoff value based on the error cause.

Types

type Backoff

type Backoff struct {

	//Factor is the multiplying factor for each increment step
	Factor float64
	//Jitter eases contention by randomizing backoff steps
	Jitter bool
	//Min and Max are the minimum and maximum values of the counter
	Min, Max time.Duration
	// GetBackoffTimer function to handle the backoff.
	GetBackoffTimer func(time.Duration) *time.Timer
	// contains filtered or unexported fields
}

Backoff is a time.Duration counter, starting at Min. After every call to the Duration method the current timing is multiplied by Factor, but it never exceeds Max.

Backoff is not generally concurrent-safe, but the ForAttempt method can be used concurrently.

func NewDefaultBackoff

func NewDefaultBackoff() *Backoff

NewDefaultBackoff default behaviour for Vortex.

func (*Backoff) Attempt

func (b *Backoff) Attempt() float64

Attempt returns the current attempt counter value.

func (*Backoff) Backoff

func (b *Backoff) Backoff(ctx context.Context, d time.Duration)

Backoff waits for the specified duration or a signal from the ctx channel, whichever happens first.

func (*Backoff) Copy

func (b *Backoff) Copy() *Backoff

Copy returns a backoff with equals constraints as the original

func (*Backoff) Duration

func (b *Backoff) Duration() time.Duration

Duration returns the duration for the current attempt.

func (*Backoff) DurationWithMax

func (b *Backoff) DurationWithMax(max time.Duration) time.Duration

Duration returns the duration for the current attempt. The result will be limited to max value.

func (*Backoff) ForAttempt

func (b *Backoff) ForAttempt(attempt float64) time.Duration

ForAttempt calls forAttempt with configured max/min values.

func (*Backoff) ForAttemptWithMax

func (b *Backoff) ForAttemptWithMax(attempt float64, max time.Duration) time.Duration

ForAttemptWithMax calls forAttempt with configured a max value limit.

func (*Backoff) IncreaseAttempt

func (b *Backoff) IncreaseAttempt()

IncreaseAttempt increases attempt counter value.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset restarts the current attempt counter at zero.

type RetryManager

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

RetryManager retries using 2 different approaches: - fixed time using SetNextRetryAfter - exponential backoff using SetNextRetryWithBackoff

func NewRetrier

func NewRetrier() *RetryManager

NewRetrier creates a new retrier

func NewRetrierWithBackoff

func NewRetrierWithBackoff(backoff *Backoff) *RetryManager

NewRetrierWithBackoff creates a new retrier with a specific backoff

func (*RetryManager) RetryAfter

func (m *RetryManager) RetryAfter() time.Duration

RetryAfter returns time to wait until next retry

func (*RetryManager) SetNextRetryAfter

func (m *RetryManager) SetNextRetryAfter(duration time.Duration)

SetNextRetryAfter : sets a provided time to wait

func (*RetryManager) SetNextRetryWithBackoff

func (m *RetryManager) SetNextRetryWithBackoff()

SetNextRetryWithBackoff sets a backoff for next retry that when called 2nd time it'll be increase exponentially

Jump to

Keyboard shortcuts

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