feat(engine): set keep alive for Tokio threads (#16162)

This commit is contained in:
Alexey Shekhirin
2025-05-12 12:21:48 +01:00
committed by GitHub
parent eb6e7f03c9
commit bc9722d9e2

View File

@@ -1,9 +1,12 @@
//! Executor for mixed I/O and CPU workloads.
use rayon::ThreadPool as RayonPool;
use std::sync::{Arc, OnceLock};
use std::{
sync::{Arc, OnceLock},
time::Duration,
};
use tokio::{
runtime::{Handle, Runtime},
runtime::{Builder, Handle, Runtime},
task::JoinHandle,
};
@@ -66,10 +69,22 @@ impl WorkloadExecutorInner {
fn new(rayon_pool: rayon::ThreadPool) -> Self {
fn get_runtime_handle() -> Handle {
Handle::try_current().unwrap_or_else(|_| {
// Create a new runtime if now runtime is available
// Create a new runtime if no runtime is available
static RT: OnceLock<Runtime> = OnceLock::new();
let rt = RT.get_or_init(|| Runtime::new().unwrap());
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()
})