Simple idiomatic retry package for Go
Idiomatic Go retry package. Thanks to @rowland for code review.
go get gopkg.in/matryer/try.v1
or
drop gopkg.in/matryer/try.v1Just call try.Do with the function you want to retry in the event of an error:
try.Do that returns a bool indicating whether to retry or not, and an error attempt argument will start at 1 and count uptry.Do blocks until you return false, or a nil errortry.Do returns the last error or nil if it was successfulvar value string
err := try.Do(func(attempt int) (bool, error) {
var err error
value, err = SomeFunction()
return attempt < 5, err // try 5 times
})
if err != nil {
log.Fatalln("error:", err)
}In the above example the function will be called repeatedly until error is nil, while attempt < 5 (i.e. try 5 times)
Try supports retrying in the event of a panic.
retry firsterr manually in the case of a panicreturn statement at the endvar value string
err := try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
if err != nil {
log.Fatalln("error:", err)
}To introduce a delay between retries, just make a time.Sleep call before you return from the function if you are returning an error.
var value string
err := try.Do(func(attempt int) (bool, error) {
var err error
value, err = SomeFunction()
if err != nil {
time.Sleep(1 * time.Minute) // wait a minute
}
return attempt < 5, err
})
if err != nil {
log.Fatalln("error:", err)
}To avoid infinite loops, Try will ensure it only makes try.MaxRetries attempts. By default, this value is 10, but you can change it:
try.MaxRetries = 20To see if a Do operation failed due to reaching the limit, you can check the error with try.IsMaxRetries(err).
No open issues yet, or sync has not completed.