backoff.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flowcontrol
  14. import (
  15. "sync"
  16. "time"
  17. "k8s.io/kubernetes/pkg/util/clock"
  18. "k8s.io/kubernetes/pkg/util/integer"
  19. )
  20. type backoffEntry struct {
  21. backoff time.Duration
  22. lastUpdate time.Time
  23. }
  24. type Backoff struct {
  25. sync.Mutex
  26. Clock clock.Clock
  27. defaultDuration time.Duration
  28. maxDuration time.Duration
  29. perItemBackoff map[string]*backoffEntry
  30. }
  31. func NewFakeBackOff(initial, max time.Duration, tc *clock.FakeClock) *Backoff {
  32. return &Backoff{
  33. perItemBackoff: map[string]*backoffEntry{},
  34. Clock: tc,
  35. defaultDuration: initial,
  36. maxDuration: max,
  37. }
  38. }
  39. func NewBackOff(initial, max time.Duration) *Backoff {
  40. return &Backoff{
  41. perItemBackoff: map[string]*backoffEntry{},
  42. Clock: clock.RealClock{},
  43. defaultDuration: initial,
  44. maxDuration: max,
  45. }
  46. }
  47. // Get the current backoff Duration
  48. func (p *Backoff) Get(id string) time.Duration {
  49. p.Lock()
  50. defer p.Unlock()
  51. var delay time.Duration
  52. entry, ok := p.perItemBackoff[id]
  53. if ok {
  54. delay = entry.backoff
  55. }
  56. return delay
  57. }
  58. // move backoff to the next mark, capping at maxDuration
  59. func (p *Backoff) Next(id string, eventTime time.Time) {
  60. p.Lock()
  61. defer p.Unlock()
  62. entry, ok := p.perItemBackoff[id]
  63. if !ok || hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
  64. entry = p.initEntryUnsafe(id)
  65. } else {
  66. delay := entry.backoff * 2 // exponential
  67. entry.backoff = time.Duration(integer.Int64Min(int64(delay), int64(p.maxDuration)))
  68. }
  69. entry.lastUpdate = p.Clock.Now()
  70. }
  71. // Reset forces clearing of all backoff data for a given key.
  72. func (p *Backoff) Reset(id string) {
  73. p.Lock()
  74. defer p.Unlock()
  75. delete(p.perItemBackoff, id)
  76. }
  77. // Returns True if the elapsed time since eventTime is smaller than the current backoff window
  78. func (p *Backoff) IsInBackOffSince(id string, eventTime time.Time) bool {
  79. p.Lock()
  80. defer p.Unlock()
  81. entry, ok := p.perItemBackoff[id]
  82. if !ok {
  83. return false
  84. }
  85. if hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
  86. return false
  87. }
  88. return p.Clock.Now().Sub(eventTime) < entry.backoff
  89. }
  90. // Returns True if time since lastupdate is less than the current backoff window.
  91. func (p *Backoff) IsInBackOffSinceUpdate(id string, eventTime time.Time) bool {
  92. p.Lock()
  93. defer p.Unlock()
  94. entry, ok := p.perItemBackoff[id]
  95. if !ok {
  96. return false
  97. }
  98. if hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
  99. return false
  100. }
  101. return eventTime.Sub(entry.lastUpdate) < entry.backoff
  102. }
  103. // Garbage collect records that have aged past maxDuration. Backoff users are expected
  104. // to invoke this periodically.
  105. func (p *Backoff) GC() {
  106. p.Lock()
  107. defer p.Unlock()
  108. now := p.Clock.Now()
  109. for id, entry := range p.perItemBackoff {
  110. if now.Sub(entry.lastUpdate) > p.maxDuration*2 {
  111. // GC when entry has not been updated for 2*maxDuration
  112. delete(p.perItemBackoff, id)
  113. }
  114. }
  115. }
  116. func (p *Backoff) DeleteEntry(id string) {
  117. p.Lock()
  118. defer p.Unlock()
  119. delete(p.perItemBackoff, id)
  120. }
  121. // Take a lock on *Backoff, before calling initEntryUnsafe
  122. func (p *Backoff) initEntryUnsafe(id string) *backoffEntry {
  123. entry := &backoffEntry{backoff: p.defaultDuration}
  124. p.perItemBackoff[id] = entry
  125. return entry
  126. }
  127. // After 2*maxDuration we restart the backoff factor to the beginning
  128. func hasExpired(eventTime time.Time, lastUpdate time.Time, maxDuration time.Duration) bool {
  129. return eventTime.Sub(lastUpdate) > maxDuration*2 // consider stable if it's ok for twice the maxDuration
  130. }