newRetryTimer creates a timer with exponentially increasing delays until the maximum retry attempts are reached.
(ctx context.Context, maxRetry int, baseSleep, maxSleep time.Duration, jitter float64)
| 50 | // newRetryTimer creates a timer with exponentially increasing |
| 51 | // delays until the maximum retry attempts are reached. |
| 52 | func (c *Client) newRetryTimer(ctx context.Context, maxRetry int, baseSleep, maxSleep time.Duration, jitter float64) iter.Seq[int] { |
| 53 | // computes the exponential backoff duration according to |
| 54 | // https://www.awsarchitectureblog.com/2015/03/backoff.html |
| 55 | exponentialBackoffWait := func(attempt int) time.Duration { |
| 56 | // normalize jitter to the range [0, 1.0] |
| 57 | if jitter < NoJitter { |
| 58 | jitter = NoJitter |
| 59 | } |
| 60 | if jitter > MaxJitter { |
| 61 | jitter = MaxJitter |
| 62 | } |
| 63 | |
| 64 | // sleep = random_between(0, min(maxSleep, base * 2 ** attempt)) |
| 65 | sleep := baseSleep * time.Duration(1<<uint(attempt)) |
| 66 | if sleep > maxSleep { |
| 67 | sleep = maxSleep |
| 68 | } |
| 69 | if math.Abs(jitter-NoJitter) > 1e-9 { |
| 70 | sleep -= time.Duration(c.random.Float64() * float64(sleep) * jitter) |
| 71 | } |
| 72 | return sleep |
| 73 | } |
| 74 | |
| 75 | return func(yield func(int) bool) { |
| 76 | // if context is already canceled, skip yield |
| 77 | select { |
| 78 | case <-ctx.Done(): |
| 79 | return |
| 80 | default: |
| 81 | } |
| 82 | |
| 83 | for i := range maxRetry { |
| 84 | if !yield(i) { |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | select { |
| 89 | case <-time.After(exponentialBackoffWait(i)): |
| 90 | case <-ctx.Done(): |
| 91 | return |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // List of AWS S3 error codes which are retryable. |
| 98 | var retryableS3Codes = set.CreateStringSet( |
no outgoing calls