pub struct MultiKeyRateLimiter { /* private fields */ }Expand description
Rate limiter that tracks limits per key (topic, node, etc.)
§Example
use mecha10::rate_limit::{MultiKeyRateLimiter, RateLimit};
let limiter = MultiKeyRateLimiter::new();
// Set limits per topic
limiter.set_limit("/camera/rgb", RateLimit::per_second(30.0)).await;
limiter.set_limit("/camera/depth", RateLimit::per_second(30.0)).await;
// Check limits
if limiter.check_and_consume("/camera/rgb").await {
println!("Allowed");
}Implementations§
Source§impl MultiKeyRateLimiter
impl MultiKeyRateLimiter
Sourcepub fn with_default(default: RateLimit) -> Self
pub fn with_default(default: RateLimit) -> Self
Create with a default limit for all keys
Sourcepub async fn set_limit(&self, key: impl Into<String>, limit: RateLimit)
pub async fn set_limit(&self, key: impl Into<String>, limit: RateLimit)
Set rate limit for a specific key
Sourcepub async fn remove_limit(&self, key: &str)
pub async fn remove_limit(&self, key: &str)
Remove rate limit for a key
Sourcepub async fn check_and_consume(&self, key: &str) -> bool
pub async fn check_and_consume(&self, key: &str) -> bool
Check and consume token for key
Sourcepub async fn available_tokens(&self, key: &str) -> Option<f64>
pub async fn available_tokens(&self, key: &str) -> Option<f64>
Get available tokens for a key
Sourcepub async fn get_limits(&self) -> HashMap<String, RateLimit>
pub async fn get_limits(&self) -> HashMap<String, RateLimit>
Get all configured limits
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MultiKeyRateLimiter
impl !RefUnwindSafe for MultiKeyRateLimiter
impl Send for MultiKeyRateLimiter
impl Sync for MultiKeyRateLimiter
impl Unpin for MultiKeyRateLimiter
impl !UnwindSafe for MultiKeyRateLimiter
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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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>
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 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>
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