Struct lib_wc::sync::RateLimiter
source · pub struct RateLimiter { /* private fields */ }
Expand description
A client-side rate limiter. This is useful for limiting the number of queries sent to a server from a single client. For example, it is useful inside of a web crawler to limit the number of requests sent by the crawler.
The rate limit is a “best effort” rate limit. It is not guaranteed that the rate limit will be exactly the specified number of queries per second. It is possible that the rate limit will be exceeded by a small amount.
Implementations§
source§impl RateLimiter
impl RateLimiter
sourcepub fn new(max_qps: f64) -> Result<Self>
pub fn new(max_qps: f64) -> Result<Self>
Creates a new rate limiter.
Returns an error if the max QPS is 0.
Examples
use tokio::sync::Mutex;
use anyhow::Result;
use lib_wc::sync::RateLimiter;
#[tokio::main]
async fn main() -> Result<()> {
let max_qps = 100.0;
RateLimiter::new(max_qps)?;
Ok(())
}
sourcepub async fn acquire(&self) -> Result<()>
pub async fn acquire(&self) -> Result<()>
Waits for the rate limiter to allow the client to send another query.
Examples
use tokio::sync::Mutex;
use anyhow::Result;
use lib_wc::sync::RateLimiter;
#[tokio::main]
async fn main() -> Result<()> {
let max_qps = 1.0;
let rate_limiter = RateLimiter::new(max_qps)?;
for _ in 0..1 {
rate_limiter.acquire().await?;
// Send a query to a server
}
Ok(())
}