pub struct RateLimiter { /* private fields */ }Expand description
Token bucket rate limiter.
Prevents thundering herd by limiting the rate of operations. Thread-safe and async-aware.
§Example
use replication_engine::resilience::{RateLimiter, RateLimitConfig};
let limiter = RateLimiter::new(RateLimitConfig::default());
// In hot path loop:
let events = vec![1, 2, 3];
for event in events {
limiter.acquire().await; // Blocks if over limit
// process(event);
}Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimitConfig) -> Self
pub fn new(config: RateLimitConfig) -> Self
Create a new rate limiter with the given configuration.
Sourcepub async fn acquire(&self)
pub async fn acquire(&self)
Acquire a permit, blocking until one is available.
This method is cancel-safe.
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Try to acquire a permit without blocking.
Returns true if acquired, false if rate limit exceeded.
Sourcepub async fn acquire_many(&self, n: u32)
pub async fn acquire_many(&self, n: u32)
Acquire multiple permits at once.
Useful for batched operations where you want to rate limit the batch as a whole rather than each item.
Sourcepub fn config(&self) -> &RateLimitConfig
pub fn config(&self) -> &RateLimitConfig
Get the current configuration.
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnwindSafe for RateLimiter
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