Small, full-featured, 100% test-covered retry package for golang
- 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
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)
}
}
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
}