mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-23 12:07:17 +00:00
34 lines
633 B
Go
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
|
|
}
|