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 package retry
import ( import (
"errors"
"time" "time"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
@ -8,7 +9,17 @@ import (
var log = logging.Logger("retry") 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++ { for i := 0; i < attempts; i++ {
if i > 0 { if i > 0 {
log.Info("Retrying after error:", err) 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 sleep *= 2
} }
result, err = f() result, err = f()
if err == nil { if err == nil || !errorIsIn(err, errorTypes) {
return result, nil return result, nil
} }
} }

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/filecoin-project/go-jsonrpc"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -90,7 +91,8 @@ func (a *UuidWrapper) MpoolPushMessage(ctx context.Context, msg *types.Message,
spec = new(api.MessageSendSpec) spec = new(api.MessageSendSpec)
} }
spec.MsgUuid = uuid.New() 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 { func MakeUuidWrapper(a v1api.RawFullNodeAPI) v1api.FullNode {