add generic retry supervisor

This commit is contained in:
Dmitry Sergeev
2026-08-27 08:35:46 +03:00
parent 1eaab14793
commit a5cfe6dffc
2 changed files with 284 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
package playback
import (
"context"
"time"
)
type attemptFunc func(context.Context) error
type retryDecider func(error) bool
type waitFunc func(context.Context, time.Duration) error
func waitForRetry(ctx context.Context, delay time.Duration) error {
timer := time.NewTimer(delay)
defer timer.Stop()
select {
case <-timer.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func runWithRetry(
ctx context.Context,
policy RetryPolicy,
attempt attemptFunc,
shouldRetry retryDecider,
wait waitFunc,
) error {
failedAttempts := 0
for {
err := attempt(ctx)
if err == nil {
return nil
}
if ctx.Err() != nil {
return ctx.Err()
}
failedAttempts++
if !shouldRetry(err) || !policy.canRetry(failedAttempts) {
return err
}
if err := wait(ctx, policy.retryDelay(failedAttempts)); err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
return err
}
}
}