Idiomatic Go retry package.
package main
import (
"context"
"errors"
"log"
"time"
"github.com/lewisay/try"
)
func main() {
// before
var err error
for i := 0; i < 3; i++ {
err = doSomeThing()
if err != nil {
break
}
}
if err != nil {
log.Println("error:", err)
}
// after
err = try.Do(context.TODO(), func(attempt int) (retry bool, err error) {
retry = attempt < 3 // try 3 times
return retry, doSomeThing()
})
if err != nil {
log.Println("error:", err)
}
}
func doSomeThing() error {
time.Sleep(1 * time.Second)
return errors.New("something error")
}