mirror of
https://github.com/project-zot/zot.git
synced 2024-12-16 21:56:37 -05:00
25 lines
425 B
Go
25 lines
425 B
Go
|
package common
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func RetryWithContext(ctx context.Context, operation func(attempt int, retryIn time.Duration) error, maxRetries int,
|
||
|
delay time.Duration,
|
||
|
) error {
|
||
|
err := operation(1, delay)
|
||
|
|
||
|
for attempt := 1; err != nil && attempt < maxRetries; attempt++ {
|
||
|
select {
|
||
|
case <-time.After(delay):
|
||
|
case <-ctx.Done():
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = operation(attempt+1, delay)
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
}
|