| 1 |
package retrier |
| 2 |
|
| 3 |
import "time" |
| 4 |
|
| 5 |
// ConstantBackoff generates a simple back-off strategy of retrying 'n' times, and waiting 'amount' time after each one. |
| 6 |
func ConstantBackoff(n int, amount time.Duration) []time.Duration { |
| 7 |
ret := make([]time.Duration, n) |
| 8 |
for i := range ret { |
| 9 |
ret[i] = amount |
| 10 |
} |
| 11 |
return ret |
| 12 |
} |
| 13 |
|
| 14 |
// ExponentialBackoff generates a simple back-off strategy of retrying 'n' times, and doubling the amount of |
| 15 |
// time waited after each one. |
| 16 |
func ExponentialBackoff(n int, initialAmount time.Duration) []time.Duration { |
| 17 |
ret := make([]time.Duration, n) |
| 18 |
next := initialAmount |
| 19 |
for i := range ret { |
| 20 |
ret[i] = next |
| 21 |
next *= 2 |
| 22 |
} |
| 23 |
return ret |
| 24 |
} |
| 25 |
|
| 26 |
// LimitedExponentialBackoff generates a simple back-off strategy of retrying 'n' times, and doubling the amount of |
| 27 |
// time waited after each one. |
| 28 |
// If back-off reaches `limitAmount` , thereafter back-off will be filled with `limitAmount` . |
| 29 |
func LimitedExponentialBackoff(n int, initialAmount time.Duration, limitAmount time.Duration) []time.Duration { |
| 30 |
ret := make([]time.Duration, n) |
| 31 |
next := initialAmount |
| 32 |
for i := range ret { |
| 33 |
if next < limitAmount { |
| 34 |
ret[i] = next |
| 35 |
next *= 2 |
| 36 |
} else { |
| 37 |
ret[i] = limitAmount |
| 38 |
} |
| 39 |
} |
| 40 |
return ret |
| 41 |
} |