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

source

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(())
}
source

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(())
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.