Skip to content

:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.

License

Notifications You must be signed in to change notification settings

go-playground/backoff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages