[−][src]Struct rate_limit::UnsyncLimiter
UnsyncLimiter is a simple token bucket-style rate limiter (cheaper to clone, but not thread safe)
When its tokens are exhausted, it will block the current thread until its refilled
The limiter is cheaply-clonable
// limits to 3 `.take()` per 1 second let limiter = UnsyncLimiter::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 = UnsyncLimiter::empty(3, Duration::from_secs(1)); for _ in 0..10 { limiter.take(); }
Methods
impl UnsyncLimiter
[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 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 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 UnsyncLimiter
[src]
fn clone(&self) -> UnsyncLimiter
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl !Sync for UnsyncLimiter
impl !Send for UnsyncLimiter
impl Unpin for UnsyncLimiter
impl !RefUnwindSafe for UnsyncLimiter
impl !UnwindSafe for UnsyncLimiter
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,