chore: name tokio rt threads (#21777)

This commit is contained in:
DaniPopes
2026-02-04 16:23:22 +01:00
committed by GitHub
parent 1fc3d2c4ae
commit a3f7431d28
3 changed files with 16 additions and 33 deletions

View File

@@ -219,7 +219,14 @@ impl CliRunnerConfig {
/// Creates a new default tokio multi-thread [Runtime](tokio::runtime::Runtime) with all features
/// enabled
pub fn tokio_runtime() -> Result<tokio::runtime::Runtime, std::io::Error> {
tokio::runtime::Builder::new_multi_thread().enable_all().build()
tokio::runtime::Builder::new_multi_thread()
.enable_all()
// Keep the threads alive for at least the block time (12 seconds) plus buffer.
// This prevents the costly process of spawning new threads on every
// new block, and instead reuses the existing threads.
.thread_keep_alive(Duration::from_secs(15))
.thread_name("tokio-rt")
.build()
}
/// Runs the given future to completion or until a critical task panicked.

View File

@@ -1,10 +1,7 @@
//! Executor for mixed I/O and CPU workloads.
use std::{sync::OnceLock, time::Duration};
use tokio::{
runtime::{Builder, Handle, Runtime},
task::JoinHandle,
};
use reth_trie_parallel::root::get_tokio_runtime_handle;
use tokio::{runtime::Handle, task::JoinHandle};
/// An executor for mixed I/O and CPU workloads.
///
@@ -27,7 +24,7 @@ impl WorkloadExecutor {
&self.inner.handle
}
/// Shorthand for [`Runtime::spawn_blocking`]
/// Runs the provided function on an executor dedicated to blocking operations.
#[track_caller]
pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
where
@@ -45,29 +42,6 @@ struct WorkloadExecutorInner {
impl WorkloadExecutorInner {
fn new() -> Self {
fn get_runtime_handle() -> Handle {
Handle::try_current().unwrap_or_else(|_| {
// Create a new runtime if no runtime is available
static RT: OnceLock<Runtime> = OnceLock::new();
let rt = RT.get_or_init(|| {
Builder::new_multi_thread()
.enable_all()
// Keep the threads alive for at least the block time, which is 12 seconds
// at the time of writing, plus a little extra.
//
// This is to prevent the costly process of spawning new threads on every
// new block, and instead reuse the existing
// threads.
.thread_keep_alive(Duration::from_secs(15))
.build()
.unwrap()
});
rt.handle().clone()
})
}
Self { handle: get_runtime_handle() }
Self { handle: get_tokio_runtime_handle() }
}
}

View File

@@ -98,7 +98,7 @@ where
let mut storage_roots = HashMap::with_capacity(storage_root_targets.len());
// Get runtime handle once outside the loop
let handle = get_runtime_handle();
let handle = get_tokio_runtime_handle();
for (hashed_address, prefix_set) in
storage_root_targets.into_iter().sorted_unstable_by_key(|(address, _)| *address)
@@ -273,17 +273,19 @@ impl From<StateProofError> for ParallelStateRootError {
/// Gets or creates a tokio runtime handle for spawning blocking tasks.
/// This ensures we always have a runtime available for I/O operations.
fn get_runtime_handle() -> Handle {
pub fn get_tokio_runtime_handle() -> Handle {
Handle::try_current().unwrap_or_else(|_| {
// Create a new runtime if no runtime is available
static RT: OnceLock<Runtime> = OnceLock::new();
let rt = RT.get_or_init(|| {
Builder::new_multi_thread()
.enable_all()
// Keep the threads alive for at least the block time (12 seconds) plus buffer.
// This prevents the costly process of spawning new threads on every
// new block, and instead reuses the existing threads.
.thread_keep_alive(Duration::from_secs(15))
.thread_name("trie-tokio-rt")
.build()
.expect("Failed to create tokio runtime")
});