pub struct RateLimiter { /* private fields */ }Expand description
A rate limiter that enforces rate limits for exchange API calls
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests_per_window: usize, window_milliseconds: u64) -> Self
pub fn new(max_requests_per_window: usize, window_milliseconds: u64) -> Self
Creates a new rate limiter
§Parameters
max_requests_per_window- Maximum number of requests allowed per time windowwindow_milliseconds- Time window in milliseconds
Sourcepub async fn acquire(&self) -> Result<()>
pub async fn acquire(&self) -> Result<()>
Acquires permission to make a request, waiting if necessary
§Returns
Ok(()) when permission is granted, or an error if the wait was interrupted
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Tries to acquire permission to make a request without waiting
§Returns
true if permission was granted, false if the rate limit would be exceeded
Sourcepub fn mark_request(&self)
pub fn mark_request(&self)
Marks a request as completed, useful for tracking successful API calls
Sourcepub fn remaining_requests(&self) -> usize
pub fn remaining_requests(&self) -> usize
Returns the number of remaining requests that can be made immediately
Sourcepub fn time_until_next_available_ms(&self) -> u64
pub fn time_until_next_available_ms(&self) -> u64
Returns the time in milliseconds until the next slot becomes available
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 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