retry

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 6 Imported by: 3

README

PkgGoDev License Go Version Tag

CI Go Report Card Maintainability Test Coverage

retry

Small, full-featured, 100% test-covered retry package for golang

features

  • small (less than 200 sloc), 100% test-covered codebase
  • fully-customizable, you can specify number of retries, sleep (and sleep-jitter) between them, and stdlog verbosity
  • 4 backoff strategies - simple, linear, binary-exponential and fibonacci
  • 3 ways to retry - single function, chain (one-by-one) and parallel execution

examples

simple

import (
    "log"

    "github.com/s0rg/retry"
)

func main() {
    try := retry.New()

    // single
    if err := try.Single("single-func", func() error {
        return initSomeResource()
    }); err != nil {
        log.Fatal("retry:", err)
    }
}

with config

import (
    "log"
    "time"

    "github.com/s0rg/retry"
)

func main() {
    try := retry.New(
        retry.Count(5),
        retry.Parallelism(2),
        retry.Sleep(time.Second*3),
        retry.Jitter(time.Second/2),
        retry.Verbose(true),
    )

    var (
        dbh *sql.DB
        kaf *kafka.Conn
        red *redis.Conn
    )

    steps := []retry.Step{
        {"database", func() (err error) {
            dbh, err = sql.Open(...)

            return
        }},
        {"kafka", func() (err error) {
            kaf, err = kafka.Connect(...)

            return
        }},
        {"redis", func() (err error) {
            red, err = redis.Connect(...)

            return
        }},
    }

    // parallel execution
    if err := try.Parallel(steps...); err != nil {
        log.Fatal("retry:", err)
    }

    // at this point all tree resources will be avialaible

}

Documentation

Index

Constants

View Source
const (
	// Simple mode - time increases by sleep + jitter*attempt.
	Simple mode = 0
	// Linear mode - time increases by sleep*attempt + jitter.
	Linear mode = 1
	// Exponential mode - time increases by sleep*2^attempt + jitter.
	Exponential mode = 2
	// Fibonacci mode - time increases by sleep*fibonacci(attempt) + jitter.
	Fibonacci mode = 3
)

Variables

This section is empty.

Functions

func Count

func Count(n int) func(*Config)

Count sets number of retry attempts.

func Fatal added in v1.1.0

func Fatal(errs ...error) func(*Config)

Fatal sets errors, that will act as non-retriable.

func Jitter

func Jitter(d time.Duration) func(*Config)

Jitter sets sleep-jitter value - if set, every attempt will await for sleep_value + jitter_value * attempt_number.

func Mode added in v1.1.0

func Mode(m mode) func(*Config)

Mode sets sleep mode - linear, exponential or simple (by default).

func Parallelism

func Parallelism(n int) func(*Config)

Parallelism sets max parallelism count, zero (default) - indicates no limit.

func Sleep

func Sleep(d time.Duration) func(*Config)

Sleep sets sleep time between attempts.

func Verbose

func Verbose(v bool) func(*Config)

Verbose sets verbosity of retry process.

Types

type Config

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

Config holds configuration.

func New

func New(opts ...option) (c *Config)

New creates new `Config` with given options If no options given default configuration will be applied: 1 retry in 1 second.

func (*Config) Chain

func (c *Config) Chain(steps ...Step) (err error)

Chain executes several `steps` one by one, returning first error.

func (*Config) Parallel

func (c *Config) Parallel(steps ...Step) (err error)

Parallel executes several `steps` in parallel.

func (*Config) Single

func (c *Config) Single(name string, fn func() error) (err error)

Single executes 'fn', until no error returned, at most `Count` times (default is 1, so `fn` will be executed at most 2 times), each execution delayed on time given as `Sleep` option (default is 1 second).

type Step

type Step struct {
	Func func() error
	Name string
}

Step represents a single execution step to re-try.

Jump to

Keyboard shortcuts

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