erigon-pulse/cmd/observer/utils/retry.go
2022-08-10 19:04:13 +07:00

34 lines
633 B
Go

package utils
import (
"context"
"time"
"github.com/ledgerwatch/log/v3"
)
func Retry(
ctx context.Context,
retryCount int,
delayForAttempt func(attempt int) time.Duration,
isRecoverableError func(error) bool,
logger log.Logger,
opName string,
op func(context.Context) (interface{}, error),
) (interface{}, error) {
var result interface{}
var err error
for i := 0; i <= retryCount; i++ {
if i > 0 {
logger.Trace("retrying", "op", opName, "attempt", i, "err", err)
Sleep(ctx, delayForAttempt(i))
}
result, err = op(ctx)
if (err == nil) || !isRecoverableError(err) {
break
}
}
return result, err
}