Add retries for specific types of errors

This commit is contained in:
Shrenuj Bansal 2022-08-18 15:57:59 -04:00
parent 736b492fb0
commit 00975237b1
2 changed files with 16 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package retry
import (
"errors"
"time"
logging "github.com/ipfs/go-log/v2"
@ -8,7 +9,17 @@ import (
var log = logging.Logger("retry")
func Retry[T any](attempts int, sleep int, f func() (T, error)) (result T, err error) {
func errorIsIn(err error, errorTypes []error) bool {
for _, etype := range errorTypes {
tmp := etype
if errors.As(err, &tmp) {
return true
}
}
return false
}
func Retry[T any](attempts int, sleep int, errorTypes []error, f func() (T, error)) (result T, err error) {
for i := 0; i < attempts; i++ {
if i > 0 {
log.Info("Retrying after error:", err)
@ -16,7 +27,7 @@ func Retry[T any](attempts int, sleep int, f func() (T, error)) (result T, err e
sleep *= 2
}
result, err = f()
if err == nil {
if err == nil || !errorIsIn(err, errorTypes) {
return result, nil
}
}

View File

@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"github.com/filecoin-project/go-jsonrpc"
"net/http"
"os"
"path/filepath"
@ -90,7 +91,8 @@ func (a *UuidWrapper) MpoolPushMessage(ctx context.Context, msg *types.Message,
spec = new(api.MessageSendSpec)
}
spec.MsgUuid = uuid.New()
return retry.Retry(5, 1, func() (*types.SignedMessage, error) { return a.FullNode.MpoolPushMessage(ctx, msg, spec) })
errorsToRetry := []error{&jsonrpc.RPCConnectionError{}}
return retry.Retry(5, 1, errorsToRetry, func() (*types.SignedMessage, error) { return a.FullNode.MpoolPushMessage(ctx, msg, spec) })
}
func MakeUuidWrapper(a v1api.RawFullNodeAPI) v1api.FullNode {