Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 2.54 KB

README.md

File metadata and controls

94 lines (73 loc) · 2.54 KB

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

}