k8s.io/client-go@v0.31.1/util/workqueue/rate_limiting_queue.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package workqueue 18 19 import "k8s.io/utils/clock" 20 21 // RateLimitingInterface is an interface that rate limits items being added to the queue. 22 // 23 // Deprecated: Use TypedRateLimitingInterface instead. 24 type RateLimitingInterface TypedRateLimitingInterface[any] 25 26 // TypedRateLimitingInterface is an interface that rate limits items being added to the queue. 27 type TypedRateLimitingInterface[T comparable] interface { 28 TypedDelayingInterface[T] 29 30 // AddRateLimited adds an item to the workqueue after the rate limiter says it's ok 31 AddRateLimited(item T) 32 33 // Forget indicates that an item is finished being retried. Doesn't matter whether it's for perm failing 34 // or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you 35 // still have to call `Done` on the queue. 36 Forget(item T) 37 38 // NumRequeues returns back how many times the item was requeued 39 NumRequeues(item T) int 40 } 41 42 // RateLimitingQueueConfig specifies optional configurations to customize a RateLimitingInterface. 43 // 44 // Deprecated: Use TypedRateLimitingQueueConfig instead. 45 type RateLimitingQueueConfig = TypedRateLimitingQueueConfig[any] 46 47 // TypedRateLimitingQueueConfig specifies optional configurations to customize a TypedRateLimitingInterface. 48 type TypedRateLimitingQueueConfig[T comparable] struct { 49 // Name for the queue. If unnamed, the metrics will not be registered. 50 Name string 51 52 // MetricsProvider optionally allows specifying a metrics provider to use for the queue 53 // instead of the global provider. 54 MetricsProvider MetricsProvider 55 56 // Clock optionally allows injecting a real or fake clock for testing purposes. 57 Clock clock.WithTicker 58 59 // DelayingQueue optionally allows injecting custom delaying queue DelayingInterface instead of the default one. 60 DelayingQueue TypedDelayingInterface[T] 61 } 62 63 // NewRateLimitingQueue constructs a new workqueue with rateLimited queuing ability 64 // Remember to call Forget! If you don't, you may end up tracking failures forever. 65 // NewRateLimitingQueue does not emit metrics. For use with a MetricsProvider, please use 66 // NewRateLimitingQueueWithConfig instead and specify a name. 67 // 68 // Deprecated: Use NewTypedRateLimitingQueue instead. 69 func NewRateLimitingQueue(rateLimiter RateLimiter) RateLimitingInterface { 70 return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{}) 71 } 72 73 // NewTypedRateLimitingQueue constructs a new workqueue with rateLimited queuing ability 74 // Remember to call Forget! If you don't, you may end up tracking failures forever. 75 // NewTypedRateLimitingQueue does not emit metrics. For use with a MetricsProvider, please use 76 // NewTypedRateLimitingQueueWithConfig instead and specify a name. 77 func NewTypedRateLimitingQueue[T comparable](rateLimiter TypedRateLimiter[T]) TypedRateLimitingInterface[T] { 78 return NewTypedRateLimitingQueueWithConfig(rateLimiter, TypedRateLimitingQueueConfig[T]{}) 79 } 80 81 // NewRateLimitingQueueWithConfig constructs a new workqueue with rateLimited queuing ability 82 // with options to customize different properties. 83 // Remember to call Forget! If you don't, you may end up tracking failures forever. 84 // 85 // Deprecated: Use NewTypedRateLimitingQueueWithConfig instead. 86 func NewRateLimitingQueueWithConfig(rateLimiter RateLimiter, config RateLimitingQueueConfig) RateLimitingInterface { 87 return NewTypedRateLimitingQueueWithConfig(rateLimiter, config) 88 } 89 90 // NewTypedRateLimitingQueueWithConfig constructs a new workqueue with rateLimited queuing ability 91 // with options to customize different properties. 92 // Remember to call Forget! If you don't, you may end up tracking failures forever. 93 func NewTypedRateLimitingQueueWithConfig[T comparable](rateLimiter TypedRateLimiter[T], config TypedRateLimitingQueueConfig[T]) TypedRateLimitingInterface[T] { 94 if config.Clock == nil { 95 config.Clock = clock.RealClock{} 96 } 97 98 if config.DelayingQueue == nil { 99 config.DelayingQueue = NewTypedDelayingQueueWithConfig(TypedDelayingQueueConfig[T]{ 100 Name: config.Name, 101 MetricsProvider: config.MetricsProvider, 102 Clock: config.Clock, 103 }) 104 } 105 106 return &rateLimitingType[T]{ 107 TypedDelayingInterface: config.DelayingQueue, 108 rateLimiter: rateLimiter, 109 } 110 } 111 112 // NewNamedRateLimitingQueue constructs a new named workqueue with rateLimited queuing ability. 113 // Deprecated: Use NewRateLimitingQueueWithConfig instead. 114 func NewNamedRateLimitingQueue(rateLimiter RateLimiter, name string) RateLimitingInterface { 115 return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{ 116 Name: name, 117 }) 118 } 119 120 // NewRateLimitingQueueWithDelayingInterface constructs a new named workqueue with rateLimited queuing ability 121 // with the option to inject a custom delaying queue instead of the default one. 122 // Deprecated: Use NewRateLimitingQueueWithConfig instead. 123 func NewRateLimitingQueueWithDelayingInterface(di DelayingInterface, rateLimiter RateLimiter) RateLimitingInterface { 124 return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{ 125 DelayingQueue: di, 126 }) 127 } 128 129 // rateLimitingType wraps an Interface and provides rateLimited re-enquing 130 type rateLimitingType[T comparable] struct { 131 TypedDelayingInterface[T] 132 133 rateLimiter TypedRateLimiter[T] 134 } 135 136 // AddRateLimited AddAfter's the item based on the time when the rate limiter says it's ok 137 func (q *rateLimitingType[T]) AddRateLimited(item T) { 138 q.TypedDelayingInterface.AddAfter(item, q.rateLimiter.When(item)) 139 } 140 141 func (q *rateLimitingType[T]) NumRequeues(item T) int { 142 return q.rateLimiter.NumRequeues(item) 143 } 144 145 func (q *rateLimitingType[T]) Forget(item T) { 146 q.rateLimiter.Forget(item) 147 }