backoff

package module
v0.0.0-...-7cb89f0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 3 Imported by: 0

README

PkgGoDev

Backoff policy

This package provides a backoff policy implementation for functions that need to be retried.

Usage

package main

import (
    "fmt"
    "time"

    "github.com/krhubert/backoff"
)

func main() {
    // Create a new contant interval backoff policy with
    // a maximum of 5 retries
    bo := backoff.WithMaxRetries(
        backoff.NewIntervalBackoff(time.Second),
        5,
    )

    // Retry a function that returns an error
    if err := backoff.Retry(bo, func() error {
        return nil
    }); err != nil {
        fmt.Println("Failed after 5 retries")
    }

    // Create a new exponential backoff policy with 
    // a maximum interval of 1 seconds
    bo = backoff.WithMaxInterval(
        backoff.NewExponentialBackoff(),
        time.Second,
    )

    backoff.Retry(bo, func() error {
        return nil
    })
}

Documentation

Index

Constants

View Source
const (
	DefaultExponentialInitial    = 100 * time.Millisecond
	DefaultExponentialFactor     = 0.5
	DefaultExponentialMultiplier = 1.5
)

Default values for ExponentialBackoff.

View Source
const Stop time.Duration = -1

Stop is a special backoff duration that stops the retry immediately.

Variables

This section is empty.

Functions

func Retry

func Retry(
	backoff Backoff,
	fn func() error,
) error

Retry retries the given function until it returns nil or Backoff stops.

func RetryCtx

func RetryCtx(
	ctx context.Context,
	backoff Backoff,
	fn func() error,
) error

RetryCtx retries the given function until it returns nil or ctx is done or Backoff stops.

Types

type Backoff

type Backoff interface {
	// NextBackoff returns the next backoff duration.
	// If the returned duration <= 0, then Retry stops immediately.
	NextBackoff() time.Duration
}

Backoff is a backoff policy for retrying operations.

func WithMaxInterval

func WithMaxInterval(backoff Backoff, max time.Duration) Backoff

WithMaxInterval returns a backoff policy that makes sure that the backoff duration never exceeds the given maximum.

func WithMaxRetries

func WithMaxRetries(backoff Backoff, max int) Backoff

WithMaxRetries returns a backoff policy that stops after the given number of retries.

type ExponentialBackoff

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

ExponentialBackoff is a policy that waits exponentially longer between retries.

func NewExponentialBackoff

func NewExponentialBackoff() *ExponentialBackoff

NewExponentialBackoff returns ExponentialBackoff with default settings.

func (*ExponentialBackoff) NextBackoff

func (b *ExponentialBackoff) NextBackoff() time.Duration

func (*ExponentialBackoff) WithFactor

func (b *ExponentialBackoff) WithFactor(factor float64) *ExponentialBackoff

WithFactor sets the factor for randomizing the backoff duration.

func (*ExponentialBackoff) WithInitial

func (b *ExponentialBackoff) WithInitial(initial time.Duration) *ExponentialBackoff

WithInitial sets the initial backoff duration.

func (*ExponentialBackoff) WithMultiplier

func (b *ExponentialBackoff) WithMultiplier(multiplier float64) *ExponentialBackoff

WithMultiplier sets the multiplier for increasing the backoff duration.

func (*ExponentialBackoff) WithRandom

func (b *ExponentialBackoff) WithRandom(random Random) *ExponentialBackoff

WithRandom sets the random number generator.

type IntervalBackoff

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

IntervalBackoff is a backoff policy that waits a fixed interval between retries.

func NewIntervalBackoff

func NewIntervalBackoff(interval time.Duration) *IntervalBackoff

NewIntervalBackoff returns IntervalBackoff with the given interval.

func (*IntervalBackoff) NextBackoff

func (b *IntervalBackoff) NextBackoff() time.Duration

type Random

type Random interface {
	Float64() float64
}

Random is a source of random numbers.

Jump to

Keyboard shortcuts

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