pub struct TokenBucketRateLimiter<Key: Eq + Hash + Clone + Debug> { /* private fields */ }
Expand description
A generic token bucket filter
Terms
Key
A key
is an identifier of the item being rate limited
Token
A token
is the smallest discrete value that we want to rate limit by. In a situation involving
network requests, this may represent a request or a byte. Tokens
are the counters for the
rate limiting, and when there are no tokens
left in a bucket
, the key
is throttled.
Bucket
A bucket
is the tracker of the number of tokens
. It has a bucket size
, and any additional
tokens added to it will “spill” out of the bucket
. The buckets
are filled at an interval
with a given fill rate
.
Interval
The interval
at which we refill all of the buckets
in the token bucket filter. Configured
across the whole token bucket filter.
Fill Rate
The rate at which we fill a bucket
with tokens. Configured per bucket.
Bucket Size
Maximum size of a bucket. A bucket saturates at this size. Configured per bucket.
Features
Keys
The token bucket takes any key as long as it’s hashable. This should allow it to apply to many applications that need rate limiters.
Bucket sizes and Rates
Defaults
There are defaults for bucket size and fill rate, which will apply to unknown keys.
Refill Interval
Buckets are refilled automatically at an interval. To do this synchronously, it calculates the number of intervals that have passed. This is done synchronously and in the future may be done asynchronously.
Implementations§
source§impl<Key: Eq + Hash + Clone + Debug> TokenBucketRateLimiter<Key>
impl<Key: Eq + Hash + Clone + Debug> TokenBucketRateLimiter<Key>
pub fn new( label: &'static str, log_info: String, new_bucket_start_percentage: u8, default_bucket_size: usize, default_fill_rate: usize, metrics: Option<HistogramVec> ) -> Self
pub fn test(default_bucket_size: usize, default_fill_rate: usize) -> Self
sourcepub fn bucket(&self, key: Key) -> SharedBucket
pub fn bucket(&self, key: Key) -> SharedBucket
Retrieve bucket, or create a new one
sourcepub fn try_garbage_collect_key(&self, key: &Key) -> bool
pub fn try_garbage_collect_key(&self, key: &Key) -> bool
Garbage collects a single key, if we know what it is