...

Source file src/k8s.io/client-go/util/workqueue/rate_limiting_queue.go

Documentation: k8s.io/client-go/util/workqueue

     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  type RateLimitingInterface interface {
    23  	DelayingInterface
    24  
    25  	// AddRateLimited adds an item to the workqueue after the rate limiter says it's ok
    26  	AddRateLimited(item interface{})
    27  
    28  	// Forget indicates that an item is finished being retried.  Doesn't matter whether it's for perm failing
    29  	// or for success, we'll stop the rate limiter from tracking it.  This only clears the `rateLimiter`, you
    30  	// still have to call `Done` on the queue.
    31  	Forget(item interface{})
    32  
    33  	// NumRequeues returns back how many times the item was requeued
    34  	NumRequeues(item interface{}) int
    35  }
    36  
    37  // RateLimitingQueueConfig specifies optional configurations to customize a RateLimitingInterface.
    38  
    39  type RateLimitingQueueConfig struct {
    40  	// Name for the queue. If unnamed, the metrics will not be registered.
    41  	Name string
    42  
    43  	// MetricsProvider optionally allows specifying a metrics provider to use for the queue
    44  	// instead of the global provider.
    45  	MetricsProvider MetricsProvider
    46  
    47  	// Clock optionally allows injecting a real or fake clock for testing purposes.
    48  	Clock clock.WithTicker
    49  
    50  	// DelayingQueue optionally allows injecting custom delaying queue DelayingInterface instead of the default one.
    51  	DelayingQueue DelayingInterface
    52  }
    53  
    54  // NewRateLimitingQueue constructs a new workqueue with rateLimited queuing ability
    55  // Remember to call Forget!  If you don't, you may end up tracking failures forever.
    56  // NewRateLimitingQueue does not emit metrics. For use with a MetricsProvider, please use
    57  // NewRateLimitingQueueWithConfig instead and specify a name.
    58  func NewRateLimitingQueue(rateLimiter RateLimiter) RateLimitingInterface {
    59  	return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{})
    60  }
    61  
    62  // NewRateLimitingQueueWithConfig constructs a new workqueue with rateLimited queuing ability
    63  // with options to customize different properties.
    64  // Remember to call Forget!  If you don't, you may end up tracking failures forever.
    65  func NewRateLimitingQueueWithConfig(rateLimiter RateLimiter, config RateLimitingQueueConfig) RateLimitingInterface {
    66  	if config.Clock == nil {
    67  		config.Clock = clock.RealClock{}
    68  	}
    69  
    70  	if config.DelayingQueue == nil {
    71  		config.DelayingQueue = NewDelayingQueueWithConfig(DelayingQueueConfig{
    72  			Name:            config.Name,
    73  			MetricsProvider: config.MetricsProvider,
    74  			Clock:           config.Clock,
    75  		})
    76  	}
    77  
    78  	return &rateLimitingType{
    79  		DelayingInterface: config.DelayingQueue,
    80  		rateLimiter:       rateLimiter,
    81  	}
    82  }
    83  
    84  // NewNamedRateLimitingQueue constructs a new named workqueue with rateLimited queuing ability.
    85  // Deprecated: Use NewRateLimitingQueueWithConfig instead.
    86  func NewNamedRateLimitingQueue(rateLimiter RateLimiter, name string) RateLimitingInterface {
    87  	return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{
    88  		Name: name,
    89  	})
    90  }
    91  
    92  // NewRateLimitingQueueWithDelayingInterface constructs a new named workqueue with rateLimited queuing ability
    93  // with the option to inject a custom delaying queue instead of the default one.
    94  // Deprecated: Use NewRateLimitingQueueWithConfig instead.
    95  func NewRateLimitingQueueWithDelayingInterface(di DelayingInterface, rateLimiter RateLimiter) RateLimitingInterface {
    96  	return NewRateLimitingQueueWithConfig(rateLimiter, RateLimitingQueueConfig{
    97  		DelayingQueue: di,
    98  	})
    99  }
   100  
   101  // rateLimitingType wraps an Interface and provides rateLimited re-enquing
   102  type rateLimitingType struct {
   103  	DelayingInterface
   104  
   105  	rateLimiter RateLimiter
   106  }
   107  
   108  // AddRateLimited AddAfter's the item based on the time when the rate limiter says it's ok
   109  func (q *rateLimitingType) AddRateLimited(item interface{}) {
   110  	q.DelayingInterface.AddAfter(item, q.rateLimiter.When(item))
   111  }
   112  
   113  func (q *rateLimitingType) NumRequeues(item interface{}) int {
   114  	return q.rateLimiter.NumRequeues(item)
   115  }
   116  
   117  func (q *rateLimitingType) Forget(item interface{}) {
   118  	q.rateLimiter.Forget(item)
   119  }
   120  

View as plain text