Retry executes the function again using backoff until maxRetries or success
(ctx context.Context, f func() (T, error))
| 16 | |
| 17 | // Retry executes the function again using backoff until maxRetries or success |
| 18 | func Retry[T any](ctx context.Context, f func() (T, error)) (T, error) { |
| 19 | operation := func() (T, error) { |
| 20 | res, err := f() |
| 21 | if err != nil { |
| 22 | var zero T |
| 23 | twerr, ok := err.(twirp.Error) |
| 24 | if !ok { |
| 25 | return zero, backoff.Permanent(err) |
| 26 | } |
| 27 | if twerr.Code() == twirp.Unavailable { |
| 28 | return zero, err |
| 29 | } |
| 30 | return zero, backoff.Permanent(err) |
| 31 | } |
| 32 | return res, nil |
| 33 | } |
| 34 | |
| 35 | return backoff.Retry( |
| 36 | ctx, |
| 37 | operation, |
| 38 | backoff.WithBackOff(backoff.NewExponentialBackOff()), |
| 39 | backoff.WithMaxTries(maxRetries), |
| 40 | backoff.WithNotify(func(err error, _ time.Duration) { |
| 41 | log.Warn("HTTP error", log.Err(err)) |
| 42 | log.Info("Retrying HTTP request...") |
| 43 | }), |
| 44 | ) |
| 45 | } |
no test coverage detected
searching dependent graphs…