Giter Club home page Giter Club logo

backoff's Introduction

Fibonacci backoff implementation

GoDoc Build Status Code Coverage Go Report Card

The package backoff implements a Fibonacci backoff algorithm.

Installation

To install it, you need to install Go and set your Go workspace first. Then, download and install it:

$ go get -u github.com/rvflash/backoff

Import it in your code:

import "github.com/rvflash/backoff"

Prerequisite

backoff uses the Go modules that required Go 1.11 or later. The go.mod file refers to Go 1.13 because the tests uses the new error method As.

Features

Based on the Fibonacci suite (1, 1, 2, 3, 5, 8, 13, 21, etc.), the backoff strategy do or retry a given task. By default, DefaultInterval is used as interval, so the sleep duration is the current Fibonacci value * 500 * time.Millisecond. With the New method, you can create your own Backoff strategy but by default, the following implementation are available:
See the documentation for more details and samples.

Do

Do guarantees to execute at least once the task if the context is not already cancelled. As long as the task return in success and the context not done, BackOff will continue to call it, with a sleep duration based the Fibonacci suite and the BackOff's interval.

  • DoN: does the same job as Do but limits the number of attempt.
  • DoUntil: does the same job as Do but limits the execution to the given deadline.

Retry

Retry retries the task until it does not return error or BackOff stops.

  • RetryN: does the same job as Do but limits the number of attempt.
  • RetryUntil: does the same job as Do but limits the execution to the given deadline.

Quick start

Assuming the following code that retry 3 times to run the task if it returns in error.

import (
	"context"
	"log"

	"github.com/rvflash/backoff"
)

func main() {
	// task implements the backoff.Func interface.
	task := func(ctx context.Context) error {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}
		return nil
	}
	n, err := backoff.RetryN(context.Background(), 3, task)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Nice boy: %d retry, first try in success", n)
	// Output: Nice boy: 0 retry, first try in success
}

backoff's People

Contributors

rvflash avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.