blob: 8321876acf40978114bf76526bd4c708172ea66a [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package workqueue
18
19// RateLimitingInterface is an interface that rate limits items being added to the queue.
20type 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.
37func NewRateLimitingQueue(rateLimiter RateLimiter) RateLimitingInterface {
38 return &rateLimitingType{
39 DelayingInterface: NewDelayingQueue(),
40 rateLimiter: rateLimiter,
41 }
42}
43
44func 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
52type 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
59func (q *rateLimitingType) AddRateLimited(item interface{}) {
60 q.DelayingInterface.AddAfter(item, q.rateLimiter.When(item))
61}
62
63func (q *rateLimitingType) NumRequeues(item interface{}) int {
64 return q.rateLimiter.NumRequeues(item)
65}
66
67func (q *rateLimitingType) Forget(item interface{}) {
68 q.rateLimiter.Forget(item)
69}