RpmTimer

Struct RpmTimer 

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

Use this struct to limit the speed of any items processing.

Adjust processing speed using struct’s methods.

Example usage:

extern crate rpm_timer;

use rpm_timer::RpmTimer;

fn main() {
    let items = &["Hello", "World!", "How", "are", "you?"];

    RpmTimer::default()
        .rps_limit(1.0)
        .max_threads(1)
        .run_slice(items, print);
}

fn print(items: &[&str]) {
    for item in items {
        println!("{}", item);
    }
}

Implementations§

Source§

impl RpmTimer

Source

pub fn tick(self, value: Duration) -> Self

Main thread will try to spawn working threads every tick.

Tip: yhe higher RPM requested, the lower tick duration should be.

Default: 100 ms

Source

pub fn rpm_limit(self, value: f64) -> Self

Target requests per minute number. It overrides the value previously set by rps_limit, if any.

Default: 60

Source

pub fn rps_limit(self, value: f64) -> Self

Target requests per second number. It overrides the value previously set by rpm_limit, if any.

Default: 1

Source

pub fn max_threads<T: Into<Option<usize>>>(self, value: T) -> Self

Maximum number of working threads in the pool.

Pass None to limit the number to the number of cpu cores (uses num_cpus under the hood).

Default: None

Source

pub fn run_slice<T, F>(self, items: &[T], action: F)
where F: Fn(&[T]) + Sync, T: Send + Sync,

Non-allocating method that spawns thread and pass sub-slices to the workers.

This is the preffered way unless you only have an iterator.

It waits for all spawned threads to finish.

Source

pub fn run_iter<T, I, F>(self, items: I, action: F)
where F: Fn(Vec<T>) + Sync, I: Iterator<Item = T>, T: Send,

Allocating method that spawns thread and pass vectors with collected items to the workers.

This is the most generic solution but you should only use it when run_slice is not possible..

It waits for all spawned threads to finish.

Trait Implementations§

Source§

impl Default for RpmTimer

Source§

fn default() -> Self

Returns the “default value” for a type. 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 = Infallible

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

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

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.