iota_core/
runtime.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{env, str::FromStr};
6
7use iota_config::NodeConfig;
8use tap::TapFallible;
9use tokio::runtime::Runtime;
10use tracing::warn;
11
12pub struct IotaRuntimes {
13    // Order in this struct is the order in which runtimes are stopped
14    pub json_rpc: Runtime,
15    pub iota_node: Runtime,
16    pub metrics: Runtime,
17}
18
19impl IotaRuntimes {
20    pub fn new(_confg: &NodeConfig) -> Self {
21        let iota_node = tokio::runtime::Builder::new_multi_thread()
22            .thread_name("iota-node-runtime")
23            .enable_all()
24            .build()
25            .unwrap();
26        let metrics = tokio::runtime::Builder::new_multi_thread()
27            .thread_name("metrics-runtime")
28            .worker_threads(2)
29            .enable_all()
30            .build()
31            .unwrap();
32
33        let worker_thread = env::var("RPC_WORKER_THREAD")
34            .ok()
35            .and_then(|o| {
36                usize::from_str(&o)
37                    .tap_err(|e| warn!("Cannot parse RPC_WORKER_THREAD to usize: {e}"))
38                    .ok()
39            })
40            .unwrap_or(num_cpus::get() / 2);
41
42        let json_rpc = tokio::runtime::Builder::new_multi_thread()
43            .thread_name("jsonrpc-runtime")
44            .worker_threads(worker_thread)
45            .enable_all()
46            .build()
47            .unwrap();
48        Self {
49            iota_node,
50            metrics,
51            json_rpc,
52        }
53    }
54}