rate_limiting_queue.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "k8s.io/utils/clock"
  15. // RateLimitingInterface is an interface that rate limits items being added to the queue.
  16. //
  17. // Deprecated: Use TypedRateLimitingInterface instead.
  18. type RateLimitingInterface TypedRateLimitingInterface[any]
  19. // TypedRateLimitingInterface is an interface that rate limits items being added to the queue.
  20. type TypedRateLimitingInterface[T comparable] interface {
  21. TypedDelayingInterface[T]
  22. // AddRateLimited adds an item to the workqueue after the rate limiter says it's ok
  23. AddRateLimited(item T)
  24. // Forget indicates that an item is finished being retried. Doesn't matter whether it's for perm failing
  25. // or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you
  26. // still have to call `Done` on the queue.
  27. Forget(item T)
  28. // NumRequeues returns back how many times the item was requeued
  29. NumRequeues(item T) int
  30. }
  31. // RateLimitingQueueConfig specifies optional configurations to customize a RateLimitingInterface.
  32. //
  33. // Deprecated: Use TypedRateLimitingQueueConfig instead.
  34. type RateLimitingQueueConfig = TypedRateLimitingQueueConfig[any]
  35. // TypedRateLimitingQueueConfig specifies optional configurations to customize a TypedRateLimitingInterface.
  36. type TypedRateLimitingQueueConfig[T comparable] struct {
  37. // Name for the queue. If unnamed, the metrics will not be registered.
  38. Name string
  39. // MetricsProvider optionally allows specifying a metrics provider to use for the queue
  40. // instead of the global provider.
  41. MetricsProvider MetricsProvider
  42. // Clock optionally allows injecting a real or fake clock for testing purposes.
  43. Clock clock.WithTicker
  44. // DelayingQueue optionally allows injecting custom delaying queue DelayingInterface instead of the default one.
  45. DelayingQueue TypedDelayingInterface[T]
  46. }
  47. // NewRateLimitingQueue constructs a new workqueue with rateLimited queuing ability
  48. // Remember to call Forget! If you don't, you may end up tracking failures forever.
  49. // NewRateLimitingQueue does not emit metrics. For use with a MetricsProvider, please use
  50. // NewRateLimitingQueueWithConfig instead and specify a name.
  51. //
  52. // Deprecated: Use NewTypedRateLimitingQueue instead.
  53. func NewRateLimitingQueue(rateLimiter RateLimiter) RateLimitingInterface {
  54. return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{})
  55. }
  56. // NewTypedRateLimitingQueue constructs a new workqueue with rateLimited queuing ability
  57. // Remember to call Forget! If you don't, you may end up tracking failures forever.
  58. // NewTypedRateLimitingQueue does not emit metrics. For use with a MetricsProvider, please use
  59. // NewTypedRateLimitingQueueWithConfig instead and specify a name.
  60. func NewTypedRateLimitingQueue[T comparable](rateLimiter TypedRateLimiter[T]) TypedRateLimitingInterface[T] {
  61. return NewTypedRateLimitingQueueWithConfig(rateLimiter, TypedRateLimitingQueueConfig[T]{})
  62. }
  63. // NewRateLimitingQueueWithConfig constructs a new workqueue with rateLimited queuing ability
  64. // with options to customize different properties.
  65. // Remember to call Forget! If you don't, you may end up tracking failures forever.
  66. //
  67. // Deprecated: Use NewTypedRateLimitingQueueWithConfig instead.
  68. func NewRateLimitingQueueWithConfig(rateLimiter RateLimiter, config RateLimitingQueueConfig) RateLimitingInterface {
  69. return NewTypedRateLimitingQueueWithConfig(rateLimiter, config)
  70. }
  71. // NewTypedRateLimitingQueueWithConfig constructs a new workqueue with rateLimited queuing ability
  72. // with options to customize different properties.
  73. // Remember to call Forget! If you don't, you may end up tracking failures forever.
  74. func NewTypedRateLimitingQueueWithConfig[T comparable](rateLimiter TypedRateLimiter[T], config TypedRateLimitingQueueConfig[T]) TypedRateLimitingInterface[T] {
  75. if config.Clock == nil {
  76. config.Clock = clock.RealClock{}
  77. }
  78. if config.DelayingQueue == nil {
  79. config.DelayingQueue = NewTypedDelayingQueueWithConfig(TypedDelayingQueueConfig[T]{
  80. Name: config.Name,
  81. MetricsProvider: config.MetricsProvider,
  82. Clock: config.Clock,
  83. })
  84. }
  85. return &rateLimitingType[T]{
  86. TypedDelayingInterface: config.DelayingQueue,
  87. rateLimiter: rateLimiter,
  88. }
  89. }
  90. // NewNamedRateLimitingQueue constructs a new named workqueue with rateLimited queuing ability.
  91. // Deprecated: Use NewRateLimitingQueueWithConfig instead.
  92. func NewNamedRateLimitingQueue(rateLimiter RateLimiter, name string) RateLimitingInterface {
  93. return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{
  94. Name: name,
  95. })
  96. }
  97. // NewRateLimitingQueueWithDelayingInterface constructs a new named workqueue with rateLimited queuing ability
  98. // with the option to inject a custom delaying queue instead of the default one.
  99. // Deprecated: Use NewRateLimitingQueueWithConfig instead.
  100. func NewRateLimitingQueueWithDelayingInterface(di DelayingInterface, rateLimiter RateLimiter) RateLimitingInterface {
  101. return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{
  102. DelayingQueue: di,
  103. })
  104. }
  105. // rateLimitingType wraps an Interface and provides rateLimited re-enquing
  106. type rateLimitingType[T comparable] struct {
  107. TypedDelayingInterface[T]
  108. rateLimiter TypedRateLimiter[T]
  109. }
  110. // AddRateLimited AddAfter's the item based on the time when the rate limiter says it's ok
  111. func (q *rateLimitingType[T]) AddRateLimited(item T) {
  112. q.TypedDelayingInterface.AddAfter(item, q.rateLimiter.When(item))
  113. }
  114. func (q *rateLimitingType[T]) NumRequeues(item T) int {
  115. return q.rateLimiter.NumRequeues(item)
  116. }
  117. func (q *rateLimitingType[T]) Forget(item T) {
  118. q.rateLimiter.Forget(item)
  119. }