pub struct RateLimiter { /* private fields */ }
Implementations§
Source§impl TokenBucketRateLimiter
impl TokenBucketRateLimiter
Sourcepub fn new(rate: usize) -> TokenBucketRateLimiter
pub fn new(rate: usize) -> TokenBucketRateLimiter
Creates a new TokenBucketRateLimiter
.
rate
specifies the average number of operations allowed per second.
Note: rate
MUST be greater than zero.
Sourcepub fn burst(&self, burst: usize) -> &TokenBucketRateLimiter
pub fn burst(&self, burst: usize) -> &TokenBucketRateLimiter
burst
specifies the maximum burst number of operations allowed.
The default value of burst
is same as rate
.
Note: burst
MUST be greater than zero.
Sourcepub fn try_acquire(&self) -> Result<(), Duration>
pub fn try_acquire(&self) -> Result<(), Duration>
Try to acquire a token. Return Ok
if the token is successfully
acquired, it means that you can safely perform frequency-controlled
operations. Otherwise Err(duration)
is returned, duration
is the
minimum time to wait.
Sourcepub async fn acquire(&self)
pub async fn acquire(&self)
Acquire a token. When the token is successfully acquired, it means that you can safely perform frequency-controlled operations.
Sourcepub async fn acquire_with_timeout(&self, timeout: Duration) -> bool
pub async fn acquire_with_timeout(&self, timeout: Duration) -> bool
Acquire a token. When the token is successfully acquired, it means that you can safely perform frequency-controlled operations.
If the method fails to obtain a token after exceeding the timeout
,
false will be returned, otherwise true will be returned.