backoff

package module
v0.0.0-...-efdd7e5 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2019 License: MIT Imports: 3 Imported by: 1

README

Build Status codecov Test Coverage Maintainability Go Report Card Documentation

Backoff

Backoff strategy written in Go

Description

Backoff is an implementation of the popular backoff strategy. It is written in Go.

Installation

go get github.com/indrasaputra/backoff

Usage

There are two actual backoff implementations. The first one is ConstantBackoff and the second one is ExponentialBackoff.

package main

import (
  "time"
  "github.com/indrasaputra/backoff"
)

func main() {
  b := &backoff.ConstantBackoff{
    BackoffInterval: 200 * time.Millisecond,
    JitterInterval:  50 * time.Millisecond,
  }

  // use NextInterval() to get the next interval
  interval := b.NextInterval()

  // use Reset() to reset the backoff
  b.Reset()
}

If you want to use ExponentialBackoff, simply follow this code

package main

import (
  "time"
  "github.com/indrasaputra/backoff"
)

func main() {
  b := backoff.ExponentialBackoff{
    BackoffInterval: 300 * time.Millisecond,
    JitterInterval: 100 * time.Millisecond,
    MaxInterval: 3 * time.Second,
    Multiplier: 2,
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// NextInterval returns an interval before the next process is executed.
	NextInterval() time.Duration
	// Reset resets backoff to its initial state.
	Reset()
}

Backoff is an interface for backoff-strategy.

type ConstantBackoff

type ConstantBackoff struct {
	// BackoffInterval defines how long the next interval will be, compared to the previous one.
	BackoffInterval time.Duration
	// JitterInterval defines the additional value for interval.
	// The additional value will be in range [0,JitterInterval).
	JitterInterval time.Duration
}

ConstantBackoff implements Backoff using constant interval.

func (*ConstantBackoff) NextInterval

func (c *ConstantBackoff) NextInterval() time.Duration

NextInterval returns next interval.

func (*ConstantBackoff) Reset

func (c *ConstantBackoff) Reset()

Reset resets Constant Backoff. Actually, it does nothing since constant backoff will always constant all the time.

type ExponentialBackoff

type ExponentialBackoff struct {
	// BackoffInterval defines how long the next interval will be, compared to the previous one.
	BackoffInterval time.Duration
	// JitterInterval defines the additional value for interval.
	// The additional value will be in range [0,JitterInterval).
	JitterInterval time.Duration
	// MaxInterval defines the maximum interval allowed.
	// If this field is let empty,
	// it means that there is no maximum value for interval.
	// Please, keep in mind that if this field is empty,
	// the interval can be a very long time.
	MaxInterval time.Duration
	// Multipler defines the multipler for the next interval.
	// Default value for Multiplier is 1.
	// Using `Multiplier = 1` means ExponentialBackoff can behave
	// like ConstantBackoff.
	Multiplier int
	// contains filtered or unexported fields
}

ExponentialBackoff implements Backoff using exponential interval.

func (*ExponentialBackoff) NextInterval

func (e *ExponentialBackoff) NextInterval() time.Duration

NextInterval returns next interval.

func (*ExponentialBackoff) Reset

func (e *ExponentialBackoff) Reset()

Reset resets Exponential Backoff.

Jump to

Keyboard shortcuts

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