pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter using token bucket algorithm
This structure is thread-safe and can be shared across multiple tasks.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimiterConfig) -> RateLimiter
pub fn new(config: RateLimiterConfig) -> RateLimiter
Create a new rate limiter with the given configuration
§Example
use ccxt_core::rate_limiter::{RateLimiter, RateLimiterConfig};
use std::time::Duration;
let config = RateLimiterConfig::new(50, Duration::from_secs(1));
let limiter = RateLimiter::new(config);Sourcepub async fn wait(&self)
pub async fn wait(&self)
Wait until a request can be made (async)
This method will block until enough tokens are available.
§Example
use ccxt_core::rate_limiter::RateLimiter;
let limiter = RateLimiter::default();
limiter.wait().await;
// Make API request hereSourcepub async fn wait_with_cost(&self, cost: u32)
pub async fn wait_with_cost(&self, cost: u32)
Wait until a request with custom cost can be made
§Arguments
cost- Number of tokens to consume for this request
Sourcepub async fn acquire(&self, cost: u32)
pub async fn acquire(&self, cost: u32)
Acquire permission to make a request (wait if necessary)
This is an alias for wait() that matches the naming convention
used in other rate limiting libraries.
Sourcepub async fn try_acquire(&self) -> bool
pub async fn try_acquire(&self) -> bool
Try to make a request without waiting
Returns true if the request can proceed, false if rate limited.
§Example
use ccxt_core::rate_limiter::RateLimiter;
let limiter = RateLimiter::default();
if limiter.try_acquire().await {
// Make API request
} else {
// Rate limited, handle accordingly
}Sourcepub async fn try_acquire_with_cost(&self, cost: u32) -> bool
pub async fn try_acquire_with_cost(&self, cost: u32) -> bool
Try to make a request with custom cost without waiting
Sourcepub async fn available_tokens(&self) -> u32
pub async fn available_tokens(&self) -> u32
Get current number of available tokens
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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RateLimiter
impl Debug for RateLimiter
Source§impl Default for RateLimiter
impl Default for RateLimiter
Source§fn default() -> RateLimiter
fn default() -> RateLimiter
Returns the “default value” for a type. Read more
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