Skip to main content

vector_core/transport/
runtime.rs

1//! The transport runtime: the bridge, its connections and every long-lived transport socket live
2//! here, so a short-lived caller runtime (Android background sync) can't take them down with it.
3
4use std::sync::OnceLock;
5
6static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
7
8/// Lazily built and never shut down. A Clearnet-only process never builds it.
9pub fn runtime() -> &'static tokio::runtime::Runtime {
10    RT.get_or_init(|| {
11        tokio::runtime::Builder::new_multi_thread()
12            .worker_threads(2)
13            .thread_name("vector-net")
14            .enable_all()
15            .build()
16            .expect("the transport runtime must start")
17    })
18}
19
20/// The one way `transport/` and `i2p/` start tasks. Every call site says why it owns no account
21/// state with a `spawn-detached:` marker.
22pub fn spawn_on<F>(fut: F) -> tokio::task::JoinHandle<F::Output>
23where
24    F: std::future::Future + Send + 'static,
25    F::Output: Send + 'static,
26{
27    // spawn-detached: the primitive itself; every caller carries its own marker.
28    runtime().spawn(fut)
29}
30
31/// The runtime, only if something already needed it.
32pub(crate) fn built() -> Option<&'static tokio::runtime::Runtime> {
33    RT.get()
34}