KeyedRateLimiter

Struct KeyedRateLimiter 

Source
pub struct KeyedRateLimiter<K>
where K: Hash + Eq,
{ /* private fields */ }
👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.
Expand description

Provides rate limiting for multiple contexts at the same time

This can use e.g. IP address as a Key. Internally this is a DashMap of TokenBucket instances that are created on demand using a prototype TokenBucket to copy initial state from. Uses LazyLru logic under the hood to keep the amount of items under control.

Implementations§

Source§

impl<K> KeyedRateLimiter<K>
where K: Hash + Eq,

Source

pub fn new( target_capacity: usize, prototype_bucket: TokenBucket, shard_amount: usize, ) -> Self

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Creates a new KeyedRateLimiter with a specified taget capacity and shard amount for the underlying DashMap. This uses a LazyLRU style eviction policy, so actual memory consumption will be 2 * target_capacity.

shard_amount should be greater than 0 and be a power of two. If a shard_amount which is not a power of two is provided, the function will panic.

Source

pub fn current_tokens(&self, key: impl Borrow<K>) -> Option<u64>

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Fetches amount of tokens available for key.

Returns None if no bucket exists for the key provided

Source

pub fn consume_tokens(&self, key: K, request_size: u64) -> Result<u64, u64>

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Consumes request_size tokens from a bucket at given key.

On success, returns Ok(amount of tokens left in the bucket) On failure, returns Err(amount of tokens missing to fill request) If no bucket exists at key, a new bucket will be allocated, and normal policy will be applied to it Outdated buckets may be evicted on an LRU basis.

Source

pub fn len_approx(&self) -> usize

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Returns approximate amount of entries in the datastructure. Should be within ~10% of the true amount.

Source

pub fn set_shrink_interval(&mut self, interval: usize)

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Set the auto-shrink interval. Set to 0 to disable shrinking. During writes we want to check for length, but not too often to reduce probability of lock contention, so keeping this large is good for perf (at cost of memory use)

Source

pub fn shrink_interval(&self) -> usize

👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.

Get the auto-shrink interval.

Auto Trait Implementations§

§

impl<K> !Freeze for KeyedRateLimiter<K>

§

impl<K> !RefUnwindSafe for KeyedRateLimiter<K>

§

impl<K> Send for KeyedRateLimiter<K>
where K: Send,

§

impl<K> Sync for KeyedRateLimiter<K>
where K: Send + Sync,

§

impl<K> Unpin for KeyedRateLimiter<K>

§

impl<K> UnwindSafe for KeyedRateLimiter<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T