default_rate_limiters.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Copyright 2016 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 workqueue
  14. import (
  15. "math"
  16. "sync"
  17. "time"
  18. "golang.org/x/time/rate"
  19. )
  20. // Deprecated: RateLimiter is deprecated, use TypedRateLimiter instead.
  21. type RateLimiter TypedRateLimiter[any]
  22. type TypedRateLimiter[T comparable] interface {
  23. // When gets an item and gets to decide how long that item should wait
  24. When(item T) time.Duration
  25. // Forget indicates that an item is finished being retried. Doesn't matter whether it's for failing
  26. // or for success, we'll stop tracking it
  27. Forget(item T)
  28. // NumRequeues returns back how many failures the item has had
  29. NumRequeues(item T) int
  30. }
  31. // DefaultControllerRateLimiter is a no-arg constructor for a default rate limiter for a workqueue. It has
  32. // both overall and per-item rate limiting. The overall is a token bucket and the per-item is exponential
  33. //
  34. // Deprecated: Use DefaultTypedControllerRateLimiter instead.
  35. func DefaultControllerRateLimiter() RateLimiter {
  36. return DefaultTypedControllerRateLimiter[any]()
  37. }
  38. // DefaultTypedControllerRateLimiter is a no-arg constructor for a default rate limiter for a workqueue. It has
  39. // both overall and per-item rate limiting. The overall is a token bucket and the per-item is exponential
  40. func DefaultTypedControllerRateLimiter[T comparable]() TypedRateLimiter[T] {
  41. return NewTypedMaxOfRateLimiter(
  42. NewTypedItemExponentialFailureRateLimiter[T](5*time.Millisecond, 1000*time.Second),
  43. // 10 qps, 100 bucket size. This is only for retry speed and its only the overall factor (not per item)
  44. &TypedBucketRateLimiter[T]{Limiter: rate.NewLimiter(rate.Limit(10), 100)},
  45. )
  46. }
  47. // Deprecated: BucketRateLimiter is deprecated, use TypedBucketRateLimiter instead.
  48. type BucketRateLimiter = TypedBucketRateLimiter[any]
  49. // TypedBucketRateLimiter adapts a standard bucket to the workqueue ratelimiter API
  50. type TypedBucketRateLimiter[T comparable] struct {
  51. *rate.Limiter
  52. }
  53. var _ RateLimiter = &BucketRateLimiter{}
  54. func (r *TypedBucketRateLimiter[T]) When(item T) time.Duration {
  55. return r.Limiter.Reserve().Delay()
  56. }
  57. func (r *TypedBucketRateLimiter[T]) NumRequeues(item T) int {
  58. return 0
  59. }
  60. func (r *TypedBucketRateLimiter[T]) Forget(item T) {
  61. }
  62. // Deprecated: ItemExponentialFailureRateLimiter is deprecated, use TypedItemExponentialFailureRateLimiter instead.
  63. type ItemExponentialFailureRateLimiter = TypedItemExponentialFailureRateLimiter[any]
  64. // TypedItemExponentialFailureRateLimiter does a simple baseDelay*2^<num-failures> limit
  65. // dealing with max failures and expiration are up to the caller
  66. type TypedItemExponentialFailureRateLimiter[T comparable] struct {
  67. failuresLock sync.Mutex
  68. failures map[T]int
  69. baseDelay time.Duration
  70. maxDelay time.Duration
  71. }
  72. var _ RateLimiter = &ItemExponentialFailureRateLimiter{}
  73. // Deprecated: NewItemExponentialFailureRateLimiter is deprecated, use NewTypedItemExponentialFailureRateLimiter instead.
  74. func NewItemExponentialFailureRateLimiter(baseDelay time.Duration, maxDelay time.Duration) RateLimiter {
  75. return NewTypedItemExponentialFailureRateLimiter[any](baseDelay, maxDelay)
  76. }
  77. func NewTypedItemExponentialFailureRateLimiter[T comparable](baseDelay time.Duration, maxDelay time.Duration) TypedRateLimiter[T] {
  78. return &TypedItemExponentialFailureRateLimiter[T]{
  79. failures: map[T]int{},
  80. baseDelay: baseDelay,
  81. maxDelay: maxDelay,
  82. }
  83. }
  84. // Deprecated: DefaultItemBasedRateLimiter is deprecated, use DefaultTypedItemBasedRateLimiter instead.
  85. func DefaultItemBasedRateLimiter() RateLimiter {
  86. return DefaultTypedItemBasedRateLimiter[any]()
  87. }
  88. func DefaultTypedItemBasedRateLimiter[T comparable]() TypedRateLimiter[T] {
  89. return NewTypedItemExponentialFailureRateLimiter[T](time.Millisecond, 1000*time.Second)
  90. }
  91. func (r *TypedItemExponentialFailureRateLimiter[T]) When(item T) time.Duration {
  92. r.failuresLock.Lock()
  93. defer r.failuresLock.Unlock()
  94. exp := r.failures[item]
  95. r.failures[item] = r.failures[item] + 1
  96. // The backoff is capped such that 'calculated' value never overflows.
  97. backoff := float64(r.baseDelay.Nanoseconds()) * math.Pow(2, float64(exp))
  98. if backoff > math.MaxInt64 {
  99. return r.maxDelay
  100. }
  101. calculated := time.Duration(backoff)
  102. if calculated > r.maxDelay {
  103. return r.maxDelay
  104. }
  105. return calculated
  106. }
  107. func (r *TypedItemExponentialFailureRateLimiter[T]) NumRequeues(item T) int {
  108. r.failuresLock.Lock()
  109. defer r.failuresLock.Unlock()
  110. return r.failures[item]
  111. }
  112. func (r *TypedItemExponentialFailureRateLimiter[T]) Forget(item T) {
  113. r.failuresLock.Lock()
  114. defer r.failuresLock.Unlock()
  115. delete(r.failures, item)
  116. }
  117. // ItemFastSlowRateLimiter does a quick retry for a certain number of attempts, then a slow retry after that
  118. // Deprecated: Use TypedItemFastSlowRateLimiter instead.
  119. type ItemFastSlowRateLimiter = TypedItemFastSlowRateLimiter[any]
  120. // TypedItemFastSlowRateLimiter does a quick retry for a certain number of attempts, then a slow retry after that
  121. type TypedItemFastSlowRateLimiter[T comparable] struct {
  122. failuresLock sync.Mutex
  123. failures map[T]int
  124. maxFastAttempts int
  125. fastDelay time.Duration
  126. slowDelay time.Duration
  127. }
  128. var _ RateLimiter = &ItemFastSlowRateLimiter{}
  129. // Deprecated: NewItemFastSlowRateLimiter is deprecated, use NewTypedItemFastSlowRateLimiter instead.
  130. func NewItemFastSlowRateLimiter(fastDelay, slowDelay time.Duration, maxFastAttempts int) RateLimiter {
  131. return NewTypedItemFastSlowRateLimiter[any](fastDelay, slowDelay, maxFastAttempts)
  132. }
  133. func NewTypedItemFastSlowRateLimiter[T comparable](fastDelay, slowDelay time.Duration, maxFastAttempts int) TypedRateLimiter[T] {
  134. return &TypedItemFastSlowRateLimiter[T]{
  135. failures: map[T]int{},
  136. fastDelay: fastDelay,
  137. slowDelay: slowDelay,
  138. maxFastAttempts: maxFastAttempts,
  139. }
  140. }
  141. func (r *TypedItemFastSlowRateLimiter[T]) When(item T) time.Duration {
  142. r.failuresLock.Lock()
  143. defer r.failuresLock.Unlock()
  144. r.failures[item] = r.failures[item] + 1
  145. if r.failures[item] <= r.maxFastAttempts {
  146. return r.fastDelay
  147. }
  148. return r.slowDelay
  149. }
  150. func (r *TypedItemFastSlowRateLimiter[T]) NumRequeues(item T) int {
  151. r.failuresLock.Lock()
  152. defer r.failuresLock.Unlock()
  153. return r.failures[item]
  154. }
  155. func (r *TypedItemFastSlowRateLimiter[T]) Forget(item T) {
  156. r.failuresLock.Lock()
  157. defer r.failuresLock.Unlock()
  158. delete(r.failures, item)
  159. }
  160. // MaxOfRateLimiter calls every RateLimiter and returns the worst case response
  161. // When used with a token bucket limiter, the burst could be apparently exceeded in cases where particular items
  162. // were separately delayed a longer time.
  163. //
  164. // Deprecated: Use TypedMaxOfRateLimiter instead.
  165. type MaxOfRateLimiter = TypedMaxOfRateLimiter[any]
  166. // TypedMaxOfRateLimiter calls every RateLimiter and returns the worst case response
  167. // When used with a token bucket limiter, the burst could be apparently exceeded in cases where particular items
  168. // were separately delayed a longer time.
  169. type TypedMaxOfRateLimiter[T comparable] struct {
  170. limiters []TypedRateLimiter[T]
  171. }
  172. func (r *TypedMaxOfRateLimiter[T]) When(item T) time.Duration {
  173. ret := time.Duration(0)
  174. for _, limiter := range r.limiters {
  175. curr := limiter.When(item)
  176. if curr > ret {
  177. ret = curr
  178. }
  179. }
  180. return ret
  181. }
  182. // Deprecated: NewMaxOfRateLimiter is deprecated, use NewTypedMaxOfRateLimiter instead.
  183. func NewMaxOfRateLimiter(limiters ...TypedRateLimiter[any]) RateLimiter {
  184. return NewTypedMaxOfRateLimiter(limiters...)
  185. }
  186. func NewTypedMaxOfRateLimiter[T comparable](limiters ...TypedRateLimiter[T]) TypedRateLimiter[T] {
  187. return &TypedMaxOfRateLimiter[T]{limiters: limiters}
  188. }
  189. func (r *TypedMaxOfRateLimiter[T]) NumRequeues(item T) int {
  190. ret := 0
  191. for _, limiter := range r.limiters {
  192. curr := limiter.NumRequeues(item)
  193. if curr > ret {
  194. ret = curr
  195. }
  196. }
  197. return ret
  198. }
  199. func (r *TypedMaxOfRateLimiter[T]) Forget(item T) {
  200. for _, limiter := range r.limiters {
  201. limiter.Forget(item)
  202. }
  203. }
  204. // WithMaxWaitRateLimiter have maxDelay which avoids waiting too long
  205. // Deprecated: Use TypedWithMaxWaitRateLimiter instead.
  206. type WithMaxWaitRateLimiter = TypedWithMaxWaitRateLimiter[any]
  207. // TypedWithMaxWaitRateLimiter have maxDelay which avoids waiting too long
  208. type TypedWithMaxWaitRateLimiter[T comparable] struct {
  209. limiter TypedRateLimiter[T]
  210. maxDelay time.Duration
  211. }
  212. // Deprecated: NewWithMaxWaitRateLimiter is deprecated, use NewTypedWithMaxWaitRateLimiter instead.
  213. func NewWithMaxWaitRateLimiter(limiter RateLimiter, maxDelay time.Duration) RateLimiter {
  214. return NewTypedWithMaxWaitRateLimiter[any](limiter, maxDelay)
  215. }
  216. func NewTypedWithMaxWaitRateLimiter[T comparable](limiter TypedRateLimiter[T], maxDelay time.Duration) TypedRateLimiter[T] {
  217. return &TypedWithMaxWaitRateLimiter[T]{limiter: limiter, maxDelay: maxDelay}
  218. }
  219. func (w TypedWithMaxWaitRateLimiter[T]) When(item T) time.Duration {
  220. delay := w.limiter.When(item)
  221. if delay > w.maxDelay {
  222. return w.maxDelay
  223. }
  224. return delay
  225. }
  226. func (w TypedWithMaxWaitRateLimiter[T]) Forget(item T) {
  227. w.limiter.Forget(item)
  228. }
  229. func (w TypedWithMaxWaitRateLimiter[T]) NumRequeues(item T) int {
  230. return w.limiter.NumRequeues(item)
  231. }