#thread-pool #actix #env-var

actix-dynamic-threadpool

a dynamic scale threadpool for actix crates

1 unstable release

0.1.0 Sep 7, 2020

#26 in #threadpool

41 downloads per month

MIT license

22KB
468 lines

A dynamic thread pool for actix-xxx crates.


How to use:

  • Add to your Cargo.toml and replace actix-threadpool.

lib.rs:

Dynamic thread pool for blocking operations

The pool would lazily generate thread according to the workload and spawn up to a total amount of you machine's logical CPU cores * 5 threads. Any spawned threads kept idle for 30 seconds would be recycled and de spawned.

*. Settings are configuable through env variables.

Example:

use std::env::set_var;

#[actix_rt::main]
async fn main() {
    // Optional: Set the max thread count for the blocking pool.
    set_var("ACTIX_THREADPOOL", "30");
    // Optional: Set the min thread count for the blocking pool.
    set_var("ACTIX_THREADPOOL_MIN", "1");
    // Optional: Set the timeout duration IN SECONDS for the blocking pool's idle threads.
    set_var("ACTIX_THREADPOOL_TIMEOUT", "30");

    let future = actix_dynamic_threadpool::run(|| {
        /* Some blocking code with a Result<T, E> as return type */
        Ok::<usize, ()>(1usize)
    });

    /*
        We can await on this blocking code and NOT block our runtime.
        When we waiting our actix runtime can switch to other async tasks.
    */

    let result: Result<usize, actix_dynamic_threadpool::BlockingError<()>> = future.await;

    assert_eq!(1usize, result.unwrap())
}

Dependencies

~1–1.8MB
~33K SLoC