backoff

package module
v0.0.0-...-53511d3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2016 License: MIT Imports: 2 Imported by: 76

README

backoff

GoDoc Build Status

This is a fork from the awesome cenkalti/backoff which is a go port from google-http-java-client.

This BackOff sleeps upon BackOff() and calculates its next backoff time instead of returning the duration to sleep.

Exponential backoff is an algorithm that uses feedback to multiplicatively decrease the rate of some process, in order to gradually find an acceptable rate. The retries exponentially increase and stop increasing when a certain threshold is met.

Install

go get github.com/azr/backoff

Documentation

Overview

Package backoff helps you at backing off !

It was forked from github.com/cenkalti/backoff which is awesome.

This BackOff sleeps upon BackOff() and calculates its next backoff time instead of returning the duration to sleep.

Index

Examples

Constants

View Source
const (
	DefaultInitialInterval     = 500 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	DefaultMultiplier          = 1.5
	DefaultMaxInterval         = 60 * time.Second
)

Default values for ExponentialBackOff.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConstantBackOff

type ConstantBackOff struct {
	Interval time.Duration
}

func NewConstant

func NewConstant(d time.Duration) *ConstantBackOff

func (*ConstantBackOff) BackOff

func (b *ConstantBackOff) BackOff()

func (*ConstantBackOff) Reset

func (b *ConstantBackOff) Reset()

type ExponentialBackOff

type ExponentialBackOff struct {
	InitialInterval time.Duration

	MaxInterval time.Duration

	RandomizationFactor float64
	Multiplier          float64
	// contains filtered or unexported fields
}

ExponentialBackOff is an implementation of BackOff that increases it's back off period for each retry attempt using a randomization function that grows exponentially. Backoff() time is calculated using the following formula:

randomized_interval =
    retry_interval * (random value in range [1 - randomization_factor, 1 + randomization_factor])

In other words BackOff() will sleep for times between the randomization factor percentage below and above the retry interval. For example, using 2 seconds as the base retry interval and 0.5 as the randomization factor, the actual back off period used in the next retry attempt will be between 1 and 3 seconds.

Note: max_interval caps the retry_interval and not the randomized_interval.

Example: The default retry_interval is .5 seconds, default randomization_factor is 0.5, default multiplier is 1.5 and the max_interval is set to 25 seconds. For 12 tries the sequence will sleep (values in seconds) (output from ExampleExpBackOffTimes) :

request#     retry_interval     randomized_interval

1             0.5                [0.25,   0.75]
2             0.75               [0.375,  1.125]
3             1.125              [0.562,  1.687]
4             1.687              [0.8435, 2.53]
5             2.53               [1.265,  3.795]
6             3.795              [1.897,  5.692]
7             5.692              [2.846,  8.538]
8             8.538              [4.269, 12.807]
9            12.807              [6.403, 19.210]
10           19.22               [9.611, 28.833]
11           25                  [12.5,  37.5]
12           25                  [12.5,  37.5]

Implementation is not thread-safe.

func NewExponential

func NewExponential() *ExponentialBackOff

NewExponential creates an instance of ExponentialBackOff using default values.

Example (DefaultWaitingIntervals)
package main

import (
	"fmt"
	"time"

	"github.com/azr/backoff"
)

func main() {
	exp := backoff.NewExponential()

	for i := 0; i < 25; i++ {
		d := exp.GetSleepTime()
		fmt.Printf("Random duration was %2.2fs, interval: %2.2fs in [ %2.2fs , %2.2fs ]\n",
			d.Seconds(),
			exp.Inverval().Seconds(),
			(exp.Inverval() - time.Duration(exp.RandomizationFactor*float64(exp.Inverval()))).Seconds(),
			(exp.Inverval() + time.Duration(exp.RandomizationFactor*float64(exp.Inverval()))).Seconds(),
		)
		exp.IncrementCurrentInterval()
		// exp.BackOff() would have executed time.Sleep(exp.GetSleepTime()) and exp.IncrementCurrentInterval()
	}
}
Output:

Random duration was 0.51s, interval: 0.50s in [ 0.25s , 0.75s ]
Random duration was 0.99s, interval: 0.75s in [ 0.38s , 1.12s ]
Random duration was 0.80s, interval: 1.12s in [ 0.56s , 1.69s ]
Random duration was 1.49s, interval: 1.69s in [ 0.84s , 2.53s ]
Random duration was 2.07s, interval: 2.53s in [ 1.27s , 3.80s ]
Random duration was 3.68s, interval: 3.80s in [ 1.90s , 5.70s ]
Random duration was 4.46s, interval: 5.70s in [ 2.85s , 8.54s ]
Random duration was 6.78s, interval: 8.54s in [ 4.27s , 12.81s ]
Random duration was 15.11s, interval: 12.81s in [ 6.41s , 19.22s ]
Random duration was 13.81s, interval: 19.22s in [ 9.61s , 28.83s ]
Random duration was 20.27s, interval: 28.83s in [ 14.42s , 43.25s ]
Random duration was 37.23s, interval: 43.25s in [ 21.62s , 64.87s ]
Random duration was 64.24s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 81.75s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 47.59s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 47.82s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 75.15s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 42.39s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 81.92s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 71.80s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 61.43s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 31.70s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 39.50s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 66.44s, interval: 60.00s in [ 30.00s , 90.00s ]
Random duration was 88.51s, interval: 60.00s in [ 30.00s , 90.00s ]

func (*ExponentialBackOff) BackOff

func (b *ExponentialBackOff) BackOff()

func (*ExponentialBackOff) GetSleepTime

func (b *ExponentialBackOff) GetSleepTime() time.Duration

func (*ExponentialBackOff) IncrementCurrentInterval

func (b *ExponentialBackOff) IncrementCurrentInterval()

Increments the current interval by multiplying it with the multiplier.

func (*ExponentialBackOff) Inverval

func (b *ExponentialBackOff) Inverval() time.Duration

func (*ExponentialBackOff) Reset

func (b *ExponentialBackOff) Reset()

Reset the interval back to the initial retry interval and restarts the timer.

type Interface

type Interface interface {
	// Example usage:
	//
	//   for ;; {
	//       err, canRetry := somethingThatCanFail()
	//       if err != nil && canRetry {
	//           backoffer.Backoff()
	//       }
	//   }
	BackOff()

	// Reset to initial state.
	Reset()
}

Interface interface to use after a retryable operation failed. A Interface.BackOff sleeps.

type LinearBackOff

type LinearBackOff struct {
	InitialInterval time.Duration
	Multiplier      float64
	Increment       time.Duration
	MaxInterval     time.Duration
	// contains filtered or unexported fields
}

grows linearly until

func NewLinear

func NewLinear(from, to, incr time.Duration, mult float64) *LinearBackOff

func (*LinearBackOff) BackOff

func (lb *LinearBackOff) BackOff()

func (*LinearBackOff) Reset

func (lb *LinearBackOff) Reset()

type ZeroBackOff

type ZeroBackOff struct{}

ZeroBackOff is a fixed back-off policy whose back-off time is always zero, meaning that the operation is retried immediately without waiting.

func (*ZeroBackOff) BackOff

func (b *ZeroBackOff) BackOff()

func (*ZeroBackOff) Reset

func (b *ZeroBackOff) Reset()

Jump to

Keyboard shortcuts

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