backoffhandler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package origin
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. )
  7. // Redeclare time functions so they can be overridden in tests.
  8. var (
  9. timeNow = time.Now
  10. timeAfter = time.After
  11. )
  12. // BackoffHandler manages exponential backoff and limits the maximum number of retries.
  13. // The base time period is 1 second, doubling with each retry.
  14. // After initial success, a grace period can be set to reset the backoff timer if
  15. // a connection is maintained successfully for a long enough period. The base grace period
  16. // is 2 seconds, doubling with each retry.
  17. type BackoffHandler struct {
  18. // MaxRetries sets the maximum number of retries to perform. The default value
  19. // of 0 disables retry completely.
  20. MaxRetries uint
  21. // RetryForever caps the exponential backoff period according to MaxRetries
  22. // but allows you to retry indefinitely.
  23. RetryForever bool
  24. // BaseTime sets the initial backoff period.
  25. BaseTime time.Duration
  26. retries uint
  27. resetDeadline time.Time
  28. }
  29. func (b BackoffHandler) GetMaxBackoffDuration(ctx context.Context) (time.Duration, bool) {
  30. // Follows the same logic as Backoff, but without mutating the receiver.
  31. // This select has to happen first to reflect the actual behaviour of the Backoff function.
  32. select {
  33. case <-ctx.Done():
  34. return time.Duration(0), false
  35. default:
  36. }
  37. if !b.resetDeadline.IsZero() && timeNow().After(b.resetDeadline) {
  38. // b.retries would be set to 0 at this point
  39. return time.Second, true
  40. }
  41. if b.retries >= b.MaxRetries && !b.RetryForever {
  42. return time.Duration(0), false
  43. }
  44. maxTimeToWait := b.GetBaseTime() * 1 << (b.retries + 1)
  45. return maxTimeToWait, true
  46. }
  47. // BackoffTimer returns a channel that sends the current time when the exponential backoff timeout expires.
  48. // Returns nil if the maximum number of retries have been used.
  49. func (b *BackoffHandler) BackoffTimer() <-chan time.Time {
  50. if !b.resetDeadline.IsZero() && timeNow().After(b.resetDeadline) {
  51. b.retries = 0
  52. b.resetDeadline = time.Time{}
  53. }
  54. if b.retries >= b.MaxRetries {
  55. if !b.RetryForever {
  56. return nil
  57. }
  58. } else {
  59. b.retries++
  60. }
  61. maxTimeToWait := time.Duration(b.GetBaseTime() * 1 << (b.retries))
  62. timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds()))
  63. return timeAfter(timeToWait)
  64. }
  65. // Backoff is used to wait according to exponential backoff. Returns false if the
  66. // maximum number of retries have been used or if the underlying context has been cancelled.
  67. func (b *BackoffHandler) Backoff(ctx context.Context) bool {
  68. c := b.BackoffTimer()
  69. if c == nil {
  70. return false
  71. }
  72. select {
  73. case <-c:
  74. return true
  75. case <-ctx.Done():
  76. return false
  77. }
  78. }
  79. // Sets a grace period within which the the backoff timer is maintained. After the grace
  80. // period expires, the number of retries & backoff duration is reset.
  81. func (b *BackoffHandler) SetGracePeriod() {
  82. maxTimeToWait := b.GetBaseTime() * 2 << (b.retries + 1)
  83. timeToWait := time.Duration(rand.Int63n(maxTimeToWait.Nanoseconds()))
  84. b.resetDeadline = timeNow().Add(timeToWait)
  85. }
  86. func (b BackoffHandler) GetBaseTime() time.Duration {
  87. if b.BaseTime == 0 {
  88. return time.Second
  89. }
  90. return b.BaseTime
  91. }
  92. // Retries returns the number of retries consumed so far.
  93. func (b *BackoffHandler) Retries() int {
  94. return int(b.retries)
  95. }
  96. func (b *BackoffHandler) ReachedMaxRetries() bool {
  97. return b.retries == b.MaxRetries
  98. }
  99. func (b *BackoffHandler) resetNow() {
  100. b.resetDeadline = time.Now()
  101. }