backoff

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

README

backoff

Go reference

Calculate delays for retries, polling, and reconnect loops. This package does not wait, execute operations, or manage cancellation. Its Go version is specified in go.mod.

go get github.com/alextanhongpin/core/sync/backoff
import (
    "time"
    "github.com/alextanhongpin/core/sync/backoff"
)

policy := backoff.NewExponential(100*time.Millisecond, 2*time.Second)
delay := policy.At(1) // Full jitter: 0 <= delay < 200ms.
  • NewConstant(period) returns the same delay for every index; nonpositive periods return zero.
  • NewLinear(period) returns period * index, saturated at the maximum duration. Nonpositive periods or indices return zero.
  • NewExponential(base, cap) returns full jitter in [0, min(base * 2^index, cap)), with overflow capped. Negative indices use zero; indices above 62 use 62. Nonpositive base or cap returns zero.

Indices are supplied by the caller. The retry package uses index 1 for its first retry, giving exponential backoff an initial upper bound of twice the base.

The strategies implement Backoff, which requires At(int) time.Duration. Exported fields support struct literals and adjustment before use. Zero-valued strategies return zero. Concurrent calls are safe when fields are not mutated concurrently. Exponential calls sample a new random delay each time.

Use a context-aware timer when waiting. The caller decides when to stop and whether an operation is safe to repeat. See the runnable example in backoff_test.go.

Documentation

Overview

Package backoff calculates delays; callers own waiting and cancellation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	At(attempts int) time.Duration
}

Backoff calculates a delay for an attempt index. Retry uses index 1 for its first retry.

type Constant

type Constant struct {
	Period time.Duration
}

Constant returns a fixed delay; nonpositive periods produce zero. Concurrent calls are safe provided exported fields are not mutated concurrently.

func NewConstant

func NewConstant(period time.Duration) *Constant

NewConstant constructs a Constant delay policy.

func (*Constant) At

func (b *Constant) At(attempts int) time.Duration

At calculates the delay without waiting.

type Exponential

type Exponential struct {
	Base time.Duration
	Cap  time.Duration
}

Exponential returns full jitter below min(Base * 2^index, Cap). Negative indices use zero; indices above 62 use 62. Nonpositive Base or Cap produces zero. Concurrent calls are safe provided exported fields are not mutated concurrently.

func NewExponential

func NewExponential(base, cap time.Duration) *Exponential

NewExponential constructs a Exponential delay policy.

func (*Exponential) At

func (b *Exponential) At(attempts int) time.Duration

At calculates the delay without waiting.

type Linear

type Linear struct {
	Period time.Duration
}

Linear returns Period times the index, saturated at the maximum duration. Nonpositive periods or indices produce zero. Concurrent calls are safe provided exported fields are not mutated concurrently.

func NewLinear

func NewLinear(period time.Duration) *Linear

NewLinear constructs a Linear delay policy.

func (*Linear) At

func (b *Linear) At(attempts int) time.Duration

At calculates the delay without waiting.

Example
package main

import (
	"fmt"
	"time"

	"github.com/alextanhongpin/core/sync/backoff"
)

func main() {
	policy := backoff.NewLinear(100 * time.Millisecond)
	for index := 1; index <= 3; index++ {
		fmt.Println(policy.At(index))
	}
}
Output:
100ms
200ms
300ms

Jump to

Keyboard shortcuts

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