Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 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 | // RateLimitingInterface is an interface that rate limits items being added to the queue. |
| 20 | type RateLimitingInterface interface { |
| 21 | DelayingInterface |
| 22 | |
| 23 | // AddRateLimited adds an item to the workqueue after the rate limiter says it's ok |
| 24 | AddRateLimited(item interface{}) |
| 25 | |
| 26 | // Forget indicates that an item is finished being retried. Doesn't matter whether it's for perm failing |
| 27 | // or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you |
| 28 | // still have to call `Done` on the queue. |
| 29 | Forget(item interface{}) |
| 30 | |
| 31 | // NumRequeues returns back how many times the item was requeued |
| 32 | NumRequeues(item interface{}) int |
| 33 | } |
| 34 | |
| 35 | // NewRateLimitingQueue constructs a new workqueue with rateLimited queuing ability |
| 36 | // Remember to call Forget! If you don't, you may end up tracking failures forever. |
| 37 | func NewRateLimitingQueue(rateLimiter RateLimiter) RateLimitingInterface { |
| 38 | return &rateLimitingType{ |
| 39 | DelayingInterface: NewDelayingQueue(), |
| 40 | rateLimiter: rateLimiter, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func NewNamedRateLimitingQueue(rateLimiter RateLimiter, name string) RateLimitingInterface { |
| 45 | return &rateLimitingType{ |
| 46 | DelayingInterface: NewNamedDelayingQueue(name), |
| 47 | rateLimiter: rateLimiter, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // rateLimitingType wraps an Interface and provides rateLimited re-enquing |
| 52 | type rateLimitingType struct { |
| 53 | DelayingInterface |
| 54 | |
| 55 | rateLimiter RateLimiter |
| 56 | } |
| 57 | |
| 58 | // AddRateLimited AddAfter's the item based on the time when the rate limiter says it's ok |
| 59 | func (q *rateLimitingType) AddRateLimited(item interface{}) { |
| 60 | q.DelayingInterface.AddAfter(item, q.rateLimiter.When(item)) |
| 61 | } |
| 62 | |
| 63 | func (q *rateLimitingType) NumRequeues(item interface{}) int { |
| 64 | return q.rateLimiter.NumRequeues(item) |
| 65 | } |
| 66 | |
| 67 | func (q *rateLimitingType) Forget(item interface{}) { |
| 68 | q.rateLimiter.Forget(item) |
| 69 | } |