2 Copyright 2016 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
19 // RateLimitingInterface is an interface that rate limits items being added to the queue.
20 type RateLimitingInterface interface {
23 // AddRateLimited adds an item to the workqueue after the rate limiter says it's ok
24 AddRateLimited(item interface{})
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{})
31 // NumRequeues returns back how many times the item was requeued
32 NumRequeues(item interface{}) int
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,
44 func NewNamedRateLimitingQueue(rateLimiter RateLimiter, name string) RateLimitingInterface {
45 return &rateLimitingType{
46 DelayingInterface: NewNamedDelayingQueue(name),
47 rateLimiter: rateLimiter,
51 // rateLimitingType wraps an Interface and provides rateLimited re-enquing
52 type rateLimitingType struct {
55 rateLimiter RateLimiter
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))
63 func (q *rateLimitingType) NumRequeues(item interface{}) int {
64 return q.rateLimiter.NumRequeues(item)
67 func (q *rateLimitingType) Forget(item interface{}) {
68 q.rateLimiter.Forget(item)