ayun_runtime/support/
runtime.rs

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