pub struct KeyedRateLimiter<K>{ /* private fields */ }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>
impl<K> KeyedRateLimiter<K>
Sourcepub 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.
pub fn new( target_capacity: usize, prototype_bucket: TokenBucket, shard_amount: usize, ) -> Self
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.
Sourcepub 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.
pub fn current_tokens(&self, key: impl Borrow<K>) -> Option<u64>
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
Sourcepub 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.
pub fn consume_tokens(&self, key: K, request_size: u64) -> Result<u64, u64>
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.
Sourcepub 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.
pub fn len_approx(&self) -> usize
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.
Sourcepub 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.
pub fn set_shrink_interval(&mut self, interval: usize)
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)
Sourcepub 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.
pub fn shrink_interval(&self) -> usize
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>
impl<K> Unpin for KeyedRateLimiter<K>
impl<K> UnwindSafe for KeyedRateLimiter<K>where
K: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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