Help us shape the future of Go! Take the 2026 Go Developer Survey

tryagain

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2019 License: MIT Imports: 4 Imported by: 0

README

Tryagain

Go Report Card CircleCI

Simple golang module using retry pattern.

Features

  • Max retries
  • Max time in ms
  • Time between retries in ms
  • Linear backoff
  • Exponential backoff
  • Random backoff

Installation

go get github.com/mcoup/tryagain

Usage

Simple try:

url := "https://127.0.0.1"

err := tryagain.Try().
    MaxTries(3).
    MaxTime(400).    // ms
    TimeBetween(50). // ms
    LinearBackoff().
    Do(func() error {
        _, err := http.Get(url)
        if err != nil {
            return err
        }
        return nil
    })

if err != nil {
    fmt.Println(err)
}

// Get https://127.0.0.1: dial tcp 127.0.0.1:443: connect: connection refused
// Get https://127.0.0.1: dial tcp 127.0.0.1:443: connect: connection refused
// Get https://127.0.0.1: dial tcp 127.0.0.1:443: connect: connection refused
// max retries reached

Or reuse:

r := tryagain.
    Try().
    MaxTries(3).
    MaxTime(400).
    TimeBetween(50)

// first
err = r.Do(func() error {
    return fmt.Errorf("an err")
})
if err != nil {
    fmt.Println(err)
}

// second
err = r.Do(func() error {
    return fmt.Errorf("an err")
})
if err != nil {
    fmt.Println(err)
}

Backoff strategies:

// linear
r := tryagain.Try().
    MaxTries(3).
    MaxTime(400).
    TimeBetween(50).
    LinearBackoff()

// exponential
r := tryagain.Try().
    MaxTries(3).
    MaxTime(400).
    TimeBetween(50).
    ExponentialBackoff()

// random
r := tryagain.Try().
    MaxTries(3).
    MaxTime(400).
    TimeBetween(50).
    RandomBackoff()

Contributing

PRs are always welcome.

Why

Quite a few go modules implement some retry mechanism but so far I have not found one that is as straightforward as I would like it to be.

Some references:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors []error

Errors represents all errors during execution.

func (Errors) Error added in v0.2.0

func (errs Errors) Error() string

type Retry

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

Retry represents the retry settings.

func Try

func Try() *Retry

Try creates a new retry of something.

func (*Retry) Do

func (r *Retry) Do(f retryableFunc) Errors

Do executes the function given the retry settings.

func (*Retry) ExponentialBackoff

func (r *Retry) ExponentialBackoff() *Retry

ExponentialBackoff will back off exponentially.

func (*Retry) LinearBackoff

func (r *Retry) LinearBackoff() *Retry

LinearBackoff will back off linearly.

func (*Retry) MaxTime

func (r *Retry) MaxTime(ms int) *Retry

MaxTime sets the max time in miliseconds.

func (*Retry) MaxTries

func (r *Retry) MaxTries(count int) *Retry

MaxTries sets the maximum number of tries.

func (*Retry) RandomBackoff

func (r *Retry) RandomBackoff() *Retry

RandomBackoff will randomly multiply TimeBetween 1 to 10 times.

func (*Retry) TimeBetween

func (r *Retry) TimeBetween(ms int) *Retry

TimeBetween sets the time between tries in miliseconds.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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