soph_runtime/support/
runtime.rs

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