Categorygithub.com/bitdabbler/backoff
modulepackage
0.0.0-20231030051445-9b1b5b19230b
Repository: https://github.com/bitdabbler/backoff.git
Documentation: pkg.go.dev

# README

Backoff

import (
    // ...
    "github.com/bitdabbler/backoff"
)

Purpose

backoff is a tiny Go library enabling simple and consistent exponential backoff with jitter.

Basic Usage

    b := backoff.CoerceNew()

    for i := 0; i < maxRetries; i++ {
        ok := somethingFailableAndRetryable()
        if !ok {
            b.Sleep()
        }
    } 

Details

Constructors

  1. func CoerceNew(options ...BackoffOption) *Backoff
  2. func New(options ...BackoffOption) (*Backoff, error)

The CoerceNew constructor clamps option inputs to valid values to guarantee that it returns a valid Backoff.

Options

OptionDefault
backoff.WithInitialDelay(time.Duration)default 100ms
backoff.WithBaseDelay(time.Duration)default 100ms
backoff.WithExponentialLimit(time.Duration)default 3 mins
backoff.WithJitterFactor(float64)default 0.3

If the initial backoff is 0, then the second backoff will use the base backoff value, and then grow exponentially in each subsequent backoff round.

# Functions

CoerceNew creates a new exponential backoff object, coercing invalid options to valid values, to guarantee that it returns a valid backoff.
New creates a new exponential backoff object.
WithBaseDelay configuration BackoffOption allows customization of the backoff delay (before jitter), used after the initial delay, if the initial delay is 0 (so, on the second call to `backoff.Sleep()` in that case).
WithExponentialLimit configuration BackoffOption allows customization of the backoff delay beyond which it stops growing exponentially.
WithInitialDelay configuration BackoffOption allows customization of the initial backoff delay (before jitter).
WithJitterFactor configuration BackoffOption allows customization of the jitter factor.

# Structs

Backoff provides exponential backoff with jitter.