Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 )
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 RandomGenerator ¶
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).