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>

source

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

source

pub fn test(default_bucket_size: usize, default_fill_rate: usize) -> Self

source

pub fn open(label: &'static str) -> Self

Used for testing and to not have a rate limiter

source

pub fn bucket(&self, key: Key) -> SharedBucket

Retrieve bucket, or create a new one

source

pub fn try_garbage_collect_key(&self, key: &Key) -> bool

Garbage collects a single key, if we know what it is

Auto Trait Implementations§

§

impl<Key> !RefUnwindSafe for TokenBucketRateLimiter<Key>

§

impl<Key> Send for TokenBucketRateLimiter<Key>where Key: Send,

§

impl<Key> Sync for TokenBucketRateLimiter<Key>where Key: Send + Sync,

§

impl<Key> Unpin for TokenBucketRateLimiter<Key>where Key: Unpin,

§

impl<Key> !UnwindSafe for TokenBucketRateLimiter<Key>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more