pub struct RateLimiter { /* private fields */ }Expand description
Token bucket rate limiter.
Tokens refill at a constant rate up to a maximum capacity. When tokens are exhausted, callers wait asynchronously.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(rate: u32, burst: Option<u32>) -> Self
pub fn new(rate: u32, burst: Option<u32>) -> Self
Create a new rate limiter.
§Arguments
rate- Sustained requests per secondburst- Maximum burst size (default: same as rate)
Sourcepub async fn acquire(&self)
pub async fn acquire(&self)
Acquire a single token, waiting asynchronously if necessary.
Returns immediately if a token is available, otherwise sleeps until enough tokens have refilled.
Sourcepub async fn try_acquire(&self) -> bool
pub async fn try_acquire(&self) -> bool
Try to acquire a token without waiting.
Returns true if a token was available and consumed.
Sourcepub async fn available(&self) -> f64
pub async fn available(&self) -> f64
Get the current number of available tokens (for diagnostics).
Sourcepub fn public_rest() -> Self
pub fn public_rest() -> Self
Create a rate limiter for public REST endpoints (50 req/s).
Sourcepub fn private_rest() -> Self
pub fn private_rest() -> Self
Create a rate limiter for private REST endpoints (50 req/s).
Sourcepub fn ws_order_entry() -> Self
pub fn ws_order_entry() -> Self
Create a rate limiter for WebSocket order entry (20 req/s).
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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