zbackoff

package
v0.0.0-alpha.18 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// Attempt returns the n-th backoff duration.
	// Used algorithm depends on implementers.
	// Returned duration can be zero or positive.
	Attempt(attempt int) time.Duration
}

Backoff provides backoff algorithm.

type BackoffError

type BackoffError struct {
	Type string
	Info string
}

BackoffError reports errors in backoff.

func (*BackoffError) Error

func (e *BackoffError) Error() string

func (*BackoffError) Is

func (e *BackoffError) Is(target error) bool

type ExponentialBackoff

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

ExponentialBackoff provides exponential backoff strategy. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for exponential function. (>=0)
  fluctuation: value range of fluctuation part. (>=0)
Input:
  attempt : the count of attempts.
Output:
  Calculate fluctuation value flc := (coeff * 2^attempt)
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + flc
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation

Graph:

      y:backoff
      |
offset|------------────────────────────
  +   |          /
fluc. |         /  y=offset+coeff*2^x
      |        /
      |      /
      |    /
offset|──/----------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewExponentialBackoff

func NewExponentialBackoff(offset, fluctuation, coeff time.Duration) *ExponentialBackoff

NewExponentialBackoff returns a new instance of ExponentialBackoff. See comments on the ExponentialBackoff for details. Allowed value range is offset>=0, fluctuation>=0, coeff>=0, otherwise it panics.

func (*ExponentialBackoff) Attempt

func (b *ExponentialBackoff) Attempt(attempt int) time.Duration

type ExponentialBackoffEqualJitter

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

ExponentialBackoffEqualJitter provides exponential backoff strategy with equal jitter. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for exponential function. (>0)
  fluctuation: value range of fluctuation part. (>0)
Input:
  attempt : the count of attempts.
Output:
  Calculate fluctuation value flc := (coeff * 2^attempt)
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + (flc/2 + RandomRange(0,flc/2))
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation

Graph:

      y:backoff
      |            y=offset+coeff*2^x
offset|------------────────────────────
  +   |          ////////// ↑ /////////
fluc. |         /////////// ↓ random //
      |        ////────────────────────
      |      ///
      |    ///
offset|──/----------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewExponentialBackoffEqualJitter

func NewExponentialBackoffEqualJitter(offset, fluctuation, coeff time.Duration) *ExponentialBackoffEqualJitter

NewExponentialBackoffEqualJitter returns a new instance of ExponentialBackoffEqualJitter. See comments on the ExponentialBackoffEqualJitter for details. Allowed value range is offset>=0, fluctuation>0, coeff>0, otherwise it panics.

func (*ExponentialBackoffEqualJitter) Attempt

func (b *ExponentialBackoffEqualJitter) Attempt(attempt int) time.Duration

type ExponentialBackoffFullJitter

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

ExponentialBackoffFullJitter provides exponential backoff strategy with full jitter. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for exponential function. (>0)
  fluctuation: value range of fluctuation part. (>0)
Input:
  attempt : the count of attempts.
Output:
  Calculate fluctuation value flc := (coeff * 2^attempt)
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + RandomRange(0,flc)
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation

Graph:

      y:backoff
      |            y=offset+coeff*2^x
offset|------------────────────────────
  +   |          ////////// ↑ /////////
fluc. |         /////////// | /////////
      |        //////////// | random //
      |      ///////////// | /////////
      |    /////////////// ↓ /////////
offset|──/----------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewExponentialBackoffFullJitter

func NewExponentialBackoffFullJitter(offset, fluctuation, coeff time.Duration) *ExponentialBackoffFullJitter

NewExponentialBackoffFullJitter returns a new instance of ExponentialBackoffFullJitter. See comments on the ExponentialBackoffFullJitter for details. Allowed value range is offset>=0, fluctuation>0, coeff>0, otherwise it panics.

func (*ExponentialBackoffFullJitter) Attempt

func (b *ExponentialBackoffFullJitter) Attempt(attempt int) time.Duration

type FibonacciBackoff

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

FibonacciBackoff provides backoff strategy using fibonacci sequence. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for fibonacci number. (>=0)
  fluctuation: value range of fluctuation part. (>=0)
Input:
  attempt : the count of attempts.
Output:
  Calculate fluctuation value flc := coeff * fibonacci(attempt).
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + flc
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation
Limitations:
  The "attempt" must be attempt<=46. If exceeded, 46 is used.

func NewFibonacciBackoff

func NewFibonacciBackoff(offset, fluctuation, coeff time.Duration) *FibonacciBackoff

NewFibonacciBackoff returns a new instance of FibonacciBackoff. See comments on the FibonacciBackoff for details. Allowed value range is offset>=0, fluctuation>=0, coeff>=0, otherwise it panics.

func (*FibonacciBackoff) Attempt

func (b *FibonacciBackoff) Attempt(attempt int) time.Duration

type FixedBackoff

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

FixedBackoff provides fixed interval backoff strategy.

Algorithm:

Parameter:
  value: fixed duration. (>=0)
Input:
  attempt: the count of attempts.
Output:
  Always return the value.
Value range:
  Min: value
  Max: value

Graph:

     y:backoff
     |
     |
     |
     |      y=value
value|-------------------------------
     |
     |
     |
     |
     └─────────────────────────────── x:attempts
     0

func NewFixedBackoff

func NewFixedBackoff(value time.Duration) *FixedBackoff

NewFixedBackoff returns a new instance of FixedBackoff. See comments on the FixedBackoff for details. Allowed value range is value>=0, otherwise it panics.

func (*FixedBackoff) Attempt

func (b *FixedBackoff) Attempt(_ int) time.Duration

type LinearBackoff

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

LinearBackoff provides fixed backoff strategy. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for linear function. (>=0)
  fluctuation: value range of fluctuation part. (>=0)
Input:
  attempt : the count of attempts.
Output:
  Calculate fluctuation value flc := (coeff * attempt)
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + flc
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation

Graph:

      y:backoff
      |
offset|------------───────────────────
  +   |         /
fluc. |       /
      |     / y=offset+coeff*x
      |   /
      | /
offset|-------------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewLinearBackoff

func NewLinearBackoff(offset, fluctuation, coeff time.Duration) *LinearBackoff

NewLinearBackoff returns a new instance of LinearBackoff. See comments on the LinearBackoff for details. Allowed value range is offset>=0, fluctuation>=0, coeff>=0, otherwise it panics.

func (*LinearBackoff) Attempt

func (b *LinearBackoff) Attempt(attempt int) time.Duration

type PolynomialBackoff

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

PolynomialBackoff provides polynomial backoff strategy. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset duration. (>=0)
  coeff: coefficient for polynomial function. (>=0)
  fluctuation: value range of fluctuation part. (>=0)
  exponent: exponent value for exponential function.
Input:
  attempt : the count of attempts. (>0)
Output:
  Calculate fluctuation value flc := (coeff * attempt^exponent)
  If flc > fluctuation, then let flc fluctuation.
  Calculate backoff duration bod := offset + flc
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation
Limitations:
  The "attempt" must be attempt>0. If not, 1 is used.

Graph:

      y:backoff
      |
offset|------------────────────────────
  +   |          /
fluc. |         /  y=offset+coeff*x^exponent
      |        /
      |      /
      |    /
offset|──/----------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewPolynomialBackoff

func NewPolynomialBackoff(offset, fluctuation, coeff time.Duration, exponent float64) *PolynomialBackoff

NewPolynomialBackoff returns a new instance of PolynomialBackoff. See comments on the PolynomialBackoff for details. Allowed value range is offset>=0, fluctuation>=0, coeff>=0, otherwise it panics.

func (PolynomialBackoff) Attempt

func (b PolynomialBackoff) Attempt(attempt int) time.Duration

type RandomBackoff

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

RandomBackoff provides random backoff strategy. It will panic if the parameter range is not satisfied.

Algorithm:

Parameter:
  offset: offset of the backoff duration. (>=0)
  fluctuation: backoff fluctuation range. (>0)
Input:
  attempt: the count of attempts.
Output:
  Calculate backoff duration bod := offset + RandomRange(0, fluctuation)
  Return bod.
Value range:
  Min: offset
  Max: offset + fluctuation

Graph:

      y:backoff
      |
offset|-------------------------------
  +   |      /\        /──\
fluc. |     /  | /|    |  |   /\  y=offset+random(fluc)
      |    /   | | \   |  \  /  \
      |   /    |/   \  /   \/    \
      |  /           \/           \
offset|-------------------------------
      |
      |
      └─────────────────────────────── x:attempts
      0

func NewRandomBackoff

func NewRandomBackoff(offset, fluctuation time.Duration) *RandomBackoff

NewRandomBackoff returns a new instance of RandomBackoff. See comments on the RandomBackoff for details. Allowed value range is offset>=0, fluctuation>0, otherwise it panics.

func (*RandomBackoff) Attempt

func (b *RandomBackoff) Attempt(_ int) time.Duration

Jump to

Keyboard shortcuts

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