#tokio #async #run-time #variables #env-var #thread-pool #environmental

tokio_env

An utility library to configure the tokio runtime via environmental variables

3 unstable releases

0.1.0 Dec 2, 2021
0.0.2 Oct 4, 2021
0.0.1 Oct 4, 2021

#618 in Configuration

Download history 780/week @ 2023-11-29 396/week @ 2023-12-06 167/week @ 2023-12-13 1621/week @ 2023-12-20 1010/week @ 2023-12-27 921/week @ 2024-01-03 1143/week @ 2024-01-10 1112/week @ 2024-01-17 1030/week @ 2024-01-24 956/week @ 2024-01-31 1264/week @ 2024-02-07 2308/week @ 2024-02-14 1135/week @ 2024-02-21 1227/week @ 2024-02-28 1198/week @ 2024-03-06 857/week @ 2024-03-13

4,567 downloads per month

Apache-2.0

7KB

tokio-env

A configuration library for convenient setup of the tokio runtime.

Configuration

All configuration is made vie environmental variables. The following variables are supported:

  • TOKIO_ENABLE_ALL Whether to enable all types of thread pools. Defaults to true.
  • TOKIO_BLOCKING_THREADS The amount of blocking threads to use.
  • TOKIO_WORKER_THREADS The amount of worker threads to use.
  • TOKIO_THREAD_STACK_SIZE The size of the stack for the created threads.
  • TOKIO_THREAD_NAME The name for the created thread pool(s).

If the environment variable is not provided, it will fall back to the tokio defaults, except for the TOKIO_ENABLE_ALL which defaults to true.

So an empty configuration unfolds like this:

tokio::runtime::Builder::new_multi_thread()
    .enable_all()
    .map(|runtime| runtime.block_on(fun));

Usage

Usage of this library could look like this:

fn main() {
    println!("Initializing tokio runtime...");
    let exit_code = tokio_env::start_with(run)
        .expect("Failed to start tokio runtime!");
    println!("Tokio runtime exited with code: {}", exit_code)
}

async fn run() -> i32 {
    println!("Program started!");
    // Your async logic here
    0
}

But... why?

I'm tired of writing the same boilerplate code over and over again, so I made it a one-liner!

Dependencies

~3–4.5MB
~81K SLoC