#thread-pool #actix #blocking #dynamic #env-variables #idle #cpu

actix-dynamic-threadpool

a dynamic scale threadpool for actix crates

1 unstable release

0.1.0 Sep 7, 2020

#40 in #blocking

Download history 4/week @ 2024-01-23 50/week @ 2024-01-30 33/week @ 2024-02-06 14/week @ 2024-02-13 12/week @ 2024-02-20 4/week @ 2024-02-27

63 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.8–2.4MB
~50K SLoC