soph_runtime/support/
runtime.rs1use crate::{config, Runtime, RuntimeResult};
2use soph_config::support::config;
3
4impl Runtime {
5 pub fn new() -> RuntimeResult<Self> {
6 let config = config().parse::<config::Runtime>()?;
7
8 let mut builder = match config.kind {
9 config::Kind::CurrentThread => tokio::runtime::Builder::new_current_thread(),
10 config::Kind::MultiThread => tokio::runtime::Builder::new_multi_thread(),
11 config::Kind::MultiThreadAlt => tokio::runtime::Builder::new_multi_thread_alt(),
12 };
13
14 if let Some(num) = config.worker_threads {
15 builder.worker_threads(num);
16 }
17
18 if let Some(stack_size) = config.thread_stack_size {
19 builder.thread_stack_size(stack_size);
20 }
21
22 if let Some(time) = config.keep_alive {
23 builder.thread_keep_alive(std::time::Duration::from_millis(time));
24 }
25
26 if let Some(queue_interval) = config.global_queue_interval {
27 builder.global_queue_interval(queue_interval);
28 }
29
30 let behavior = match config.unhandled_panic {
31 config::UnhandledPanic::Ignore => tokio::runtime::UnhandledPanic::Ignore,
32 config::UnhandledPanic::ShutdownRuntime => tokio::runtime::UnhandledPanic::Ignore,
33 };
34
35 if config.enable_metrics_poll_time_histogram {
36 builder.enable_metrics_poll_time_histogram();
37 }
38
39 if config.disable_lifo_slot {
40 builder.disable_lifo_slot();
41 }
42
43 let runtime = builder
44 .enable_all()
45 .max_io_events_per_tick(config.nevents)
46 .max_blocking_threads(config.max_blocking_threads)
47 .thread_name(config.thread_name.to_string())
48 .event_interval(config.event_interval)
49 .unhandled_panic(behavior)
50 .build()?;
51
52 Ok(Self { inner: runtime })
53 }
54}
55
56impl std::ops::Deref for Runtime {
57 type Target = tokio::runtime::Runtime;
58
59 fn deref(&self) -> &Self::Target {
60 &self.inner
61 }
62}
63
64impl Default for Runtime {
65 fn default() -> Self {
66 Self {
67 inner: tokio::runtime::Builder::new_multi_thread()
68 .enable_all()
69 .build()
70 .expect("Failed to initialize runtime"),
71 }
72 }
73}