pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter supporting per-client and global limits
Thread-safe: uses parking_lot::Mutex for the global bucket and
DashMap with per-entry Mutex for client buckets.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimiterConfig) -> Self
pub fn new(config: RateLimiterConfig) -> Self
Create a new rate limiter with the given configuration
Sourcepub fn check_rate_limit(&self, client_id: &str) -> Result<(), RateLimitError>
pub fn check_rate_limit(&self, client_id: &str) -> Result<(), RateLimitError>
Check rate limit for a given client, returning an error if exceeded
Checks the per-client limit first, then the global limit (if enabled). Tokens are only consumed from the global bucket if the per-client check passes, ensuring that a per-client rejection does not waste global capacity.
Sourcepub fn try_acquire(&self, client_id: &str) -> bool
pub fn try_acquire(&self, client_id: &str) -> bool
Try to acquire a token for the given client, returning true if allowed
This is a convenience wrapper around check_rate_limit.
Sourcepub fn remaining_tokens(&self, client_id: &str) -> u32
pub fn remaining_tokens(&self, client_id: &str) -> u32
Get the number of remaining tokens for a client
Returns the per-client remaining tokens, or if per-client tracking is disabled, returns the global remaining tokens.
Sourcepub fn cleanup_expired_buckets(&self) -> usize
pub fn cleanup_expired_buckets(&self) -> usize
Remove client buckets that have been idle longer than the configured timeout
Returns the number of buckets removed.
Sourcepub fn tracked_client_count(&self) -> usize
pub fn tracked_client_count(&self) -> usize
Get the current number of tracked clients
Sourcepub fn config(&self) -> &RateLimiterConfig
pub fn config(&self) -> &RateLimiterConfig
Get a reference to the configuration
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> 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>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.