backoff

package module
v0.0.0-...-22c86f0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2019 License: Apache-2.0 Imports: 2 Imported by: 1

README

Backoff

Backoff provides backoff policies for use in rate limiting.

Usage

A backoff policy can be created using the NewConstant, NewLinear, or NewExponential functions. Jitter can be applied using the NewJitter function. The Default function returns a default "safe" backoff policy, however it is better to configure a backoff policy for a specific use case.

Example

p := backoff.Default()
for {
    _, err := http.Get("http://example.com")
    if err != nil {
        break
    }
    p.Sleep()
}
p.Decrease()

Policies

Documentation

Documentation is available here.

License

This project is released under the Apache License, Version 2.0.

Documentation

Overview

Package backoff provides a backoff policies for use in rate limiting.

Example
package main

import (
	"net/http"

	"github.com/tradyfinance/backoff"
)

func main() {
	p := backoff.Default()
	for {
		_, err := http.Get("http://example.com")
		if err != nil {
			break
		}
		p.Sleep()
	}
	p.Decrease()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Nil = &Constant{}

Nil is a constant backoff policy with zero delay.

Functions

This section is empty.

Types

type Constant

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

Constant is a backoff policy with a constant delay.

func NewConstant

func NewConstant(delay time.Duration) *Constant

NewConstant returns a new Constant backoff policy.

func (*Constant) Decrease

func (con *Constant) Decrease()

Decrease implements the Policy interface.

func (*Constant) Increase

func (con *Constant) Increase() time.Duration

Increase implements the Policy interface.

func (*Constant) Sleep

func (con *Constant) Sleep()

Sleep implements the Policy interface.

type Exponential

type Exponential struct {
	Min    time.Duration
	Factor float64
	Max    time.Duration
	// contains filtered or unexported fields
}

Exponential is a backoff policy with an exponentially changing delay.

func NewExponential

func NewExponential(min time.Duration, factor float64, max time.Duration) *Exponential

NewExponential returns a new Exponential backoff policy.

func (*Exponential) Decrease

func (exp *Exponential) Decrease()

Decrease implements the Policy interface.

func (*Exponential) Increase

func (exp *Exponential) Increase() time.Duration

Increase implements the Policy interface.

func (*Exponential) Sleep

func (exp *Exponential) Sleep()

Sleep implements the Policy interface.

type Jitter

type Jitter struct {
	Source Policy
	Factor float64
}

Jitter is a backoff policy that applies jitter to the delay of another backoff policy.

func NewJitter

func NewJitter(source Policy, factor float64) *Jitter

NewJitter returns a new Jitter backoff policy given a source backoff policy and a jitter factor.

func (*Jitter) Decrease

func (jit *Jitter) Decrease()

Decrease implements the Policy interface.

func (*Jitter) Increase

func (jit *Jitter) Increase() time.Duration

Increase implements the Policy interface.

func (*Jitter) Sleep

func (jit *Jitter) Sleep()

Sleep implements the Policy interface.

type Linear

type Linear struct {
	Min  time.Duration
	Step time.Duration
	Max  time.Duration
	// contains filtered or unexported fields
}

Linear is a backoff policy with a linearly changing delay.

func NewLinear

func NewLinear(min, step, max time.Duration) *Linear

NewLinear returns a new Linear backoff policy.

func (*Linear) Decrease

func (lin *Linear) Decrease()

Decrease implements the Policy interface.

func (*Linear) Increase

func (lin *Linear) Increase() time.Duration

Increase implements the Policy interface.

func (*Linear) Sleep

func (lin *Linear) Sleep()

Sleep implements the Policy interface.

type Policy

type Policy interface {
	Decrease()
	Increase() time.Duration
	Sleep()
}

A Policy is an interface for implementing backoff policies.

func Default

func Default() Policy

Default returns default "safe" backoff policy.

Jump to

Keyboard shortcuts

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