backoff

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2019 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-backoff

Build Status

A library to retry callback using Exponential Backoff delay strategy.


Try imagine obvious situation. You app has to retrieve an Url, like for example:

resp, err := http.Get(url)

In an ideal world, you get your response and continue. In a real world, it might fail sometimes. In such a situation you probably want to retry the attempt, as internet might be down for a moment, or whatever else. That's exact situation where backoff.Try() helps. Let's see the example above in a new perspective:

var body []byte

err := backoff.Try(func() (err error) {
    resp, err := http.Get("https://example.com")
    if err != nil {
        return
    }
    defer resp.Body.Close()
    body, err = ioutil.ReadAll(resp.Body)
    return
})

The Try() function will, wonder of wonders, tries to execute given callback until the result is successful (in other words, returned error is nil). It will not try it more than by default 3 attempts. It waits between each attempt 100ms * 2^attempt_counter + random_ms_not_bigger_than_100ms.

The amount of attempts as delay base unit is configurable, of course. Let's see it in action:

err := backoff.Try(
    myRetriableFunc,
    backoff.MaxAttempts(7),
    backoff.DelayUnit(2 * time.Second),
)

For your convenience, there are few more options to use:

err := backoff.Try(
    myRetriableFunc,
    backoff.OnRetry(func (err error) {
        log.Error(err)
    }),
    backoff.RetryIf(func (err error) bool {
        return IsThereAReasonToContinueToTry()
    }),
)

Hansate Rahen

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Try

func Try(fn func() error, options ...Option) (err error)

Try ...

Types

type Option

type Option func(*config)

Option represents method to call to configure given config.

func DelayUnit

func DelayUnit(delay time.Duration) Option

DelayUnit specifies initial time durration to wait between tries. Default is 100ms.

func MaxAttempts

func MaxAttempts(attempts uint) Option

MaxAttempts set maxium amount of retries, default is 3.

func OnRetry

func OnRetry(fn func(error)) Option

OnRetry accepts a function to call between attempts. The Error from failed attempt is passed to given func. Default is nil.

func RetryIf

func RetryIf(fn func(error) bool) Option

RetryIf receives a function to control the retry mechanism. Given callback func receives error and must respons with bool siglaing if to continue or not.

Jump to

Keyboard shortcuts

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