rate_limiting_queue.go 2.3 KB

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