[][src]Struct twitchchat::helpers::RateLimit

pub struct RateLimit { /* fields omitted */ }

RateLimit is a simple token bucket-style rate limiter

When its tokens are exhausted, it will block the current thread until its refilled

The limiter is cheaply-clonable

use std::time::Duration;
use twitchchat::helpers::RateLimit;

// limits to 3 `.take()` per 1 second
let limiter = RateLimit::full(3, Duration::from_secs(1));
for _ in 0..10 {
    // every 3 calls within 1 second will cause the next .take() to block
    // so this will take ~3 seconds to run (10 / 3 = ~3)
    limiter.take();
}

// initially empty, it will block for 1 second then block for every 3 calls per 1 second
let limiter = RateLimit::empty(3, Duration::from_secs(1));
for _ in 0..10 {
    limiter.take();
}

The _unsync() variants create a cheaper, single-threaded form

Methods

impl RateLimit[src]

pub fn new(cap: u64, initial: u64, period: Duration) -> Self[src]

Create a new, thread-safe limiter

cap is the number of total tokens available

initial is how many are initially available

period is how long it'll take to refill all of the tokens

pub fn new_unsync(cap: u64, initial: u64, period: Duration) -> Self[src]

Create a new, single-threaded limiter

cap is the number of total tokens available

initial is how many are initially available

period is how long it'll take to refill all of the tokens

pub fn full(cap: u64, period: Duration) -> Self[src]

Create a thread-safe limiter thats pre-filled

cap is the number of total tokens available

period is how long it'll take to refill all of the tokens

pub fn empty(cap: u64, period: Duration) -> Self[src]

Create an empty thread-safe limiter

cap is the number of total tokens available

period is how long it'll take to refill all of the tokens

pub fn cap(&self) -> u64[src]

Returns the capacity of this RateLimit

pub fn full_unsync(cap: u64, period: Duration) -> Self[src]

Create a single-threaded limiter thats pre-filled

cap is the number of total tokens available

period is how long it'll take to refill all of the tokens

pub fn empty_unsync(cap: u64, period: Duration) -> Self[src]

Create an empty single-threaded limiter

cap is the number of total tokens available

period is how long it'll take to refill all of the tokens

pub fn consume(&self, tokens: u64) -> Result<u64, Duration>[src]

Tries to consume tokens

If it will consume more than available then an Error is returned. Otherwise it returns how many tokens are left

This error is the Duration of the next available time

pub fn throttle(&self, tokens: u64) -> u64[src]

Consumes tokens blocking if its trying to consume more than available

Returns how many tokens are available

pub fn take(&self) -> u64[src]

Take a token, blocking if unavailable

Returns how many tokens are available

Trait Implementations

impl Clone for RateLimit[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl !Send for RateLimit

impl !Sync for RateLimit

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.