backoffhandler.go 3.1 KB

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