[][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 ratelimit::RateLimit;

// limits to 3 `.take()` per 1 second
let mut 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 mut limiter = RateLimit::empty(3, Duration::from_secs(1));
for _ in 0..10 {
    limiter.take();
}

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

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(&mut 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(&mut 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(&mut self) -> u64[src]

Take a token, blocking if unavailable

Returns how many tokens are available

Auto Trait Implementations

impl Send for RateLimit

impl Sync for RateLimit

Blanket Implementations

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

impl<T> From<T> for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

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

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

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