backoff

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2017 License: MIT Imports: 4 Imported by: 0

README

Backoff library

Project status Build Status Coverage Status Go Report Card GoDoc License

Backoff library uses an exponential backoff algorithm to backoff between retries.

What makes this different from other backoff libraries?

  1. Simple, by automatically calculating the exponential factor between the min and max backoff times; which properly tunes to your desired values.
  2. Provides an optional AutoTune function which will adjust the min backoff duration based upon successful backoff duration retries.

Why AutoTune?

For long running services that hit external services such as writing to a DB or that hit a 3rd party API's, where successful attempts backoff durations can vary over time as load changes. Additionally by using AutoTune it should provide an automatic jitter when multiple copies of a service are running.

Example Basic

package main

import (
	"errors"
	"time"

	"fmt"

	"github.com/go-playground/backoff"
)

func main() {
        // backoff.New(retries, min, max)
	bo := backoff.New(5, time.Second, time.Minute)
	defer bo.Close()

	// retry function and notification function to log failures etc...
	bo.Run(func() (bail bool, err error) {
		// do something
		return false, errors.New("ERR")
	}, func(attempt uint16, waiting time.Duration, err error) {
		fmt.Printf("Attempt:%d Waiting:%s Error:%s\n", attempt, waiting, err)

	})
}

Example AutoTune

package main

import (
	"errors"
	"fmt"
	"time"

	"os"

	"os/signal"
	"syscall"

	"math/rand"

	"github.com/go-playground/backoff"
)

func main() {
        // backoff.New(retries, min, max)
	bo := backoff.New(5, time.Second, 10*time.Second)
	// bo.AutoTune(poll, reset)
	bo.AutoTune(30*time.Second, 2*time.Minute)
	defer bo.Close()

	go func() {
		for {
			time.Sleep(time.Millisecond * 500)
			go func() {
				bo.Run(func() (bool, error) {
					// simulating random success/failure
					count := rand.Intn(5)
					switch count {
					case 1:
						return false, errors.New("ERR")
					case 2:
						return false, errors.New("ERR")
					case 3:
						return false, nil
					case 4:
						return false, errors.New("ERR")
					default: //case 5:
						return false, nil
					}
				}, func(attempt uint16, waiting time.Duration, err error) {
					fmt.Printf("Attempt:%d Waiting: %s Error:%s\n", attempt, waiting, err)
				})
			}()
		}
	}()
	s := make(chan os.Signal, 1)
	signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
	<-s
}

Requirements

>= Go v1.9

Package Versioning

I'm jumping on the vendoring bandwagon, you should vendor this package as I will not be creating different version with gopkg.in like allot of my other libraries.

Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE, it is so freeing not to worry about it and will help me keep pouring out bigger and better things for you the community.

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instance

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

Instance defines a backoff instance

func New

func New(retries uint16, min, max time.Duration) *Instance

New returns a new backoff instance for use with sane defaults

func (*Instance) AutoTune

func (i *Instance) AutoTune(poll, reset time.Duration)

AutoTune automatically adjusts the minimum delay time based on past successes and failures to an acceptable rate. poll is the time interval after reset, or on initial setup, the value is calculated while reset is the time interval in which the values are set back to their original values and value will be recalculates after the poll duration.

func (*Instance) Close

func (i *Instance) Close()

Close cleans up any outstanding processes/goroutines

func (*Instance) Run

func (i *Instance) Run(fn RetryFn, notifyFn NotifyFn) error

Run executes the provided function with exponential backoff upon failure

type NotifyFn

type NotifyFn func(attempt uint16, waiting time.Duration, err error)

NotifyFn describes the notify function signature

type RetryFn

type RetryFn = retry.Fn

RetryFn describes the retry function signature

Source Files

  • backoff.go

Directories

Path Synopsis
_examples
auto-tune command
basic command

Jump to

Keyboard shortcuts

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