pub struct RateLimiter { /* private fields */ }Expand description
Token-bucket rate limiter.
Refills at rate_pps tokens per second. Each consume() call
blocks until a token is available, providing smooth rate control.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(rate_pps: u64, burst: u64) -> Self
pub fn new(rate_pps: u64, burst: u64) -> Self
Create a new rate limiter.
rate_pps: target packets per second (0 = unlimited)burst: maximum burst size in packets
Sourcepub fn try_consume(&mut self) -> bool
pub fn try_consume(&mut self) -> bool
Try to consume one token. Returns true if the token was available.
Does NOT block.
Sourcepub fn try_consume_batch(&mut self, n: u64) -> u64
pub fn try_consume_batch(&mut self, n: u64) -> u64
Try to consume n tokens. Returns the number actually consumed.
Sourcepub fn consume_blocking(&mut self)
pub fn consume_blocking(&mut self)
Block until a token is available, then consume it.
Uses spin-wait for sub-microsecond precision when the wait is short,
and thread::yield_now for longer waits.
Sourcepub fn is_unlimited(&self) -> bool
pub fn is_unlimited(&self) -> bool
Whether this limiter is unlimited.
Sourcepub fn set_rate_pps(&mut self, rate_pps: u64)
pub fn set_rate_pps(&mut self, rate_pps: u64)
Re-target the rate at runtime. Used by AdaptiveLoop to react
to TX drops / ICMP-unreachable bursts without rebuilding the
limiter (which would lose the bucket fill state and stutter).
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> 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