backoff

package
v0.0.0-...-54413df Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &Config{
	Base:   time.Second,
	Max:    time.Second * 30,
	Jitter: FullJitter,
}

Functions

func EqualJitter

func EqualJitter(randomGen RandomGenerator, factor int64) time.Duration

func FullJitter

func FullJitter(randomGen RandomGenerator, factor int64) time.Duration

Types

type Backoff

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

Backoff is used to calculate the next backoff duration using a Jitter.

Check here why to use Jitter - https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/

The backoff is calculated by min(Max, rand(0, Base*(2^retries))).

Example:

	b := backoff.NewBackoff(backoff.DefaultConfig)

 for {
		err := someFunc()
		if err == nil {
			// all good
			return nil
		}

		// get the next sleep duration
		sleep := b.Next()

		select {
		case <-time.After(sleep):
			continue
		case <-ctx.Done():
			return errors.Wrap(err, "retry canceled")
		}
	}

func NewBackoff

func NewBackoff(config *Config) *Backoff

NewBackoff uses the golang rand generator from the standard library.

It is NOT safe to be used in multiple go routines.

If you need to repeatedly create backoff objects in multiple go routines and want to share a random generator see BackoffFactory.

Sharing a random generator is good, since every time a generator is created it must be seeded, which is expensive operation.

func NewBackoffWithRandomGen

func NewBackoffWithRandomGen(randomGen RandomGenerator, config *Config) *Backoff

NewBackoffWithRandomGen is used when you want to pass a custom RandomGenerator.

Example:

 // Note that the backoff created bellow can be used safely only in a single go routine.
	b := backoff.New(
		rand.New(rand.NewSource(time.Now().UnixNano())),
		backoff.DefaultConfig
	)

func (*Backoff) Next

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

Next returns the next duration for the retry.

type BackoffFactory

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

BackoffFactory is used for creating backoff objects with a single shared RandomGenerator.

BackoffFactory is useful if you need to repeatedly create backoff objects in multiple go routines.

Sharing a random generator is good, since every time a generator is created it must be seeded, which is expensive operation.

Example:

 // the shared RandomGenerator seeding happens here
	backoffFactory := backoff.NewBackoffFactory()

	for i := 0; i < 10; i++ {
		go func() {
			// created backoff instances use a shared RandomGenerator
			backoff := backoffFactory.CreateBackoff(backoff.DefaultConfig)

			// use the backoff
		}()
	}

func NewBackoffFactory

func NewBackoffFactory() *BackoffFactory

NewBackoffFactory creates a BackoffFactory instance using a standard random generator.

The shared random generator is seeded with the current time in nanoseconds, and wrapped with SyncRandomGenerator for multi go routine safety.

func (*BackoffFactory) CreateBackoff

func (bf *BackoffFactory) CreateBackoff(config *Config) *Backoff

CreateBackoff returns a Backoff instance using a shared RandomGenerator.

type Config

type Config struct {
	// Base is the duration used for the next duration calculation.
	Base time.Duration
	// Max is the maximum duration the backoff can return.
	Max time.Duration
	// Jitter is the Jitter used to randomize the next duration.
	Jitter Jitter
}

Config is used for the backoff constructor if you don't want to use the default config.

type Jitter

type Jitter func(randomGen RandomGenerator, factor int64) time.Duration

type RandomGenerator

type RandomGenerator interface {
	Int63n(n int64) int64
}

RandomGenerator interface for the random generator.

Standard library can be used as follows: rand.NewSource(time.Now().UnixNano()).

Note that the standard generator returned by rand.NewSource is multi go routine unsafe. For multi go routine safety use NewSyncRandomGenerator(rand.NewSource(time.Now().UnixNano())).

type SyncRandomGenerator

type SyncRandomGenerator struct {
	RandomGenerator
	// contains filtered or unexported fields
}

SyncRandomGenerator is a wrapper that makes a RandomGenerator multi go routine safe.

func NewSyncRandomGenerator

func NewSyncRandomGenerator(generator RandomGenerator) *SyncRandomGenerator

NewSyncRandomGenerator creates SyncRandomGenerator instance.

func (*SyncRandomGenerator) Int63n

func (g *SyncRandomGenerator) Int63n(n int64) int64

Int63n returns a int64 random integer in the half open interval [0, n).

Jump to

Keyboard shortcuts

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