2022-08-17 18:22:31 +00:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
2022-08-18 19:57:59 +00:00
|
|
|
"errors"
|
2022-08-18 21:55:56 +00:00
|
|
|
"reflect"
|
2022-08-17 18:22:31 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("retry")
|
|
|
|
|
2022-08-22 21:32:43 +00:00
|
|
|
func ErrorIsIn(err error, errorTypes []error) bool {
|
2022-08-18 19:57:59 +00:00
|
|
|
for _, etype := range errorTypes {
|
2022-08-18 21:55:56 +00:00
|
|
|
tmp := reflect.New(reflect.PointerTo(reflect.ValueOf(etype).Elem().Type())).Interface()
|
|
|
|
if errors.As(err, tmp) {
|
2022-08-18 19:57:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-22 21:32:43 +00:00
|
|
|
func Retry[T any](attempts int, initialBackoff time.Duration, errorTypes []error, f func() (T, error)) (result T, err error) {
|
2022-08-17 18:22:31 +00:00
|
|
|
for i := 0; i < attempts; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
log.Info("Retrying after error:", err)
|
2022-08-22 21:32:43 +00:00
|
|
|
time.Sleep(initialBackoff)
|
|
|
|
initialBackoff *= 2
|
2022-08-17 18:22:31 +00:00
|
|
|
}
|
|
|
|
result, err = f()
|
2022-08-22 21:32:43 +00:00
|
|
|
if err == nil || !ErrorIsIn(err, errorTypes) {
|
2022-08-19 15:33:37 +00:00
|
|
|
return result, err
|
2022-08-17 18:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Errorf("Failed after %d attempts, last error: %s", attempts, err)
|
|
|
|
return result, err
|
|
|
|
}
|