hexz_core/store/runtime.rs
1//! Shared Tokio runtime for async storage backends.
2//!
3//! Provides a lazily-initialized, process-global Tokio runtime so that
4//! `HttpBackend` and `S3Backend` share a single thread pool and reactor
5//! instead of each creating their own `Runtime`.
6
7use std::sync::OnceLock;
8use tokio::runtime::{Handle, Runtime};
9
10/// Process-global Tokio runtime instance.
11static GLOBAL_RUNTIME: OnceLock<Runtime> = OnceLock::new();
12
13/// Returns a handle to the shared Tokio runtime.
14///
15/// The runtime is created lazily on first call with a multi-threaded
16/// scheduler. Subsequent calls return a handle to the same runtime.
17pub fn global_handle() -> Handle {
18 GLOBAL_RUNTIME
19 .get_or_init(|| Runtime::new().expect("Failed to create shared Tokio runtime"))
20 .handle()
21 .clone()
22}