Skip to main content

RateLimiter

Struct RateLimiter 

Source
pub struct RateLimiter { /* private fields */ }
Expand description

Token bucket: rate ops/second, starting EMPTY (strict pacing — a full-bucket start lets a small job burn its whole burst unthrottled, defeating the point of --rate for short sweeps). rate == 0 = unlimited.

use kevy_cli::bulk::RateLimiter;
use std::time::{Duration, Instant};

let mut limiter = RateLimiter::new(100); // 100 ops/s
let start = Instant::now();
for _ in 0..5 {
    limiter.take();
}
// the bucket starts empty, so even a short burst is paced
assert!(start.elapsed() >= Duration::from_millis(40));

Implementations§

Source§

impl RateLimiter

Source

pub fn new(rate: u64) -> Self

New limiter at rate ops/s (0 = off).

use kevy_cli::bulk::RateLimiter;
let mut unlimited = RateLimiter::new(0);
let start = std::time::Instant::now();
for _ in 0..10_000 {
    unlimited.take(); // never blocks
}
assert!(start.elapsed() < std::time::Duration::from_secs(1));
Source

pub fn take(&mut self)

Block until one op is admitted.

use kevy_cli::bulk::RateLimiter;
use std::time::{Duration, Instant};

let mut limiter = RateLimiter::new(50);
let start = Instant::now();
limiter.take(); // the first op already waits for its token (~20 ms)
assert!(start.elapsed() >= Duration::from_millis(15));

Trait Implementations§

Source§

impl Debug for RateLimiter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.