backoffs.go 653 B

123456789101112131415161718192021222324
  1. package retrier
  2. import "time"
  3. // ConstantBackoff generates a simple back-off strategy of retrying 'n' times, and waiting 'amount' time after each one.
  4. func ConstantBackoff(n int, amount time.Duration) []time.Duration {
  5. ret := make([]time.Duration, n)
  6. for i := range ret {
  7. ret[i] = amount
  8. }
  9. return ret
  10. }
  11. // ExponentialBackoff generates a simple back-off strategy of retrying 'n' times, and doubling the amount of
  12. // time waited after each one.
  13. func ExponentialBackoff(n int, initialAmount time.Duration) []time.Duration {
  14. ret := make([]time.Duration, n)
  15. next := initialAmount
  16. for i := range ret {
  17. ret[i] = next
  18. next *= 2
  19. }
  20. return ret
  21. }