From 00975237b1312eed7a203708e1d938162719eac6 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Thu, 18 Aug 2022 15:57:59 -0400 Subject: [PATCH] Add retries for specific types of errors --- lib/retry/retry.go | 15 +++++++++++++-- node/modules/storageminer.go | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/retry/retry.go b/lib/retry/retry.go index c7d269a22..0f53fe91e 100644 --- a/lib/retry/retry.go +++ b/lib/retry/retry.go @@ -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 } } diff --git a/node/modules/storageminer.go b/node/modules/storageminer.go index 0875d3935..36ddeffab 100644 --- a/node/modules/storageminer.go +++ b/node/modules/storageminer.go @@ -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 {