Skip to content
/ retry Public

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

License

Notifications You must be signed in to change notification settings

s0rg/retry

Repository files navigation

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

}

About

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

Topics

Resources

License

Stars

Watchers

Forks