Skip to content

Commit 9e6a76a

Browse files
authored
feat: add env vars to configure the runtime threadpool size and name
1 parent 2b6c7fe commit 9e6a76a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@
230230
//! default-features = false
231231
//! features = ["alloc"]
232232
//! ```
233+
//!
234+
//! # Runtime configuration
235+
//!
236+
//! Several environment variables are available to tune the async-std
237+
//! runtime:
238+
//!
239+
//! * `ASYNC_STD_THREAD_COUNT`: The number of threads that the
240+
//! async-std runtime will start. By default, this is one per logical
241+
//! cpu as reported by the [num_cpus](num_cpus) crate, which may be
242+
//! different than the number of physical cpus. Async-std _will panic_
243+
//! if this is set to any value other than a positive integer.
244+
//! * `ASYNC_STD_THREAD_NAME`: The name that async-std's runtime
245+
//! threads report to the operating system. The default value is
246+
//! `"async-std/runtime"`.
247+
//!
233248
234249
#![cfg_attr(not(feature = "std"), no_std)]
235250
#![cfg_attr(feature = "docs", feature(doc_cfg))]

src/rt/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The runtime.
22
3+
use std::env;
34
use std::thread;
45

56
use once_cell::sync::Lazy;
@@ -12,10 +13,20 @@ pub struct Runtime {}
1213
/// The global runtime.
1314
pub static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
1415
// Create an executor thread pool.
15-
let num_threads = num_cpus::get().max(1);
16-
for _ in 0..num_threads {
16+
17+
let thread_count = env::var("ASYNC_STD_THREAD_COUNT")
18+
.map(|env| {
19+
env.parse()
20+
.expect("ASYNC_STD_THREAD_COUNT must be a number")
21+
})
22+
.unwrap_or_else(|_| num_cpus::get())
23+
.max(1);
24+
25+
let thread_name = env::var("ASYNC_STD_THREAD_NAME").unwrap_or("async-std/runtime".to_string());
26+
27+
for _ in 0..thread_count {
1728
thread::Builder::new()
18-
.name("async-std/runtime".to_string())
29+
.name(thread_name.clone())
1930
.spawn(|| smol::run(future::pending::<()>()))
2031
.expect("cannot start a runtime thread");
2132
}

0 commit comments

Comments
 (0)