1use ntex_util::time::Millis;
2
3use crate::{Server, ServerConfiguration};
4
5const DEFAULT_SHUTDOWN_TIMEOUT: Millis = Millis::from_secs(30);
6
7#[allow(clippy::struct_excessive_bools)]
8#[derive(Debug, Clone)]
9pub struct WorkerPool {
11 pub(crate) num: usize,
12 pub(crate) name: String,
13 pub(crate) no_signals: bool,
14 pub(crate) stop_runtime: bool,
15 pub(crate) stop_on_panic: bool,
16 pub(crate) graceful_shutdown: bool,
17 pub(crate) shutdown_timeout: Millis,
18 pub(crate) affinity: bool,
19}
20
21impl Default for WorkerPool {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl WorkerPool {
28 #[must_use]
29 pub fn new() -> Self {
31 let num = core_affinity::get_core_ids().map_or_else(
32 || std::thread::available_parallelism().map_or(2, std::num::NonZeroUsize::get),
33 |v| v.len(),
34 );
35
36 WorkerPool {
37 num,
38 name: "ntex".to_string(),
39 no_signals: false,
40 stop_runtime: false,
41 stop_on_panic: false,
42 graceful_shutdown: false,
43 shutdown_timeout: DEFAULT_SHUTDOWN_TIMEOUT,
44 affinity: false,
45 }
46 }
47
48 #[must_use]
49 pub fn name<T: AsRef<str>>(mut self, name: T) -> Self {
53 self.name = name.as_ref().to_string();
54 self
55 }
56
57 #[must_use]
58 pub fn workers(mut self, num: usize) -> Self {
63 self.num = num;
64 self
65 }
66
67 #[must_use]
68 pub fn stop_runtime(mut self) -> Self {
72 self.stop_runtime = true;
73 self
74 }
75
76 #[must_use]
77 pub fn stop_on_panic(mut self) -> Self {
81 self.stop_on_panic = true;
82 self
83 }
84
85 #[must_use]
86 pub fn disable_signals(mut self) -> Self {
90 self.no_signals = true;
91 self
92 }
93
94 #[must_use]
95 pub fn graceful_shutdown(mut self) -> Self {
100 self.graceful_shutdown = true;
101 self
102 }
103
104 #[must_use]
105 pub fn shutdown_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
113 self.shutdown_timeout = timeout.into();
114 self
115 }
116
117 #[must_use]
118 pub fn enable_affinity(mut self) -> Self {
122 self.affinity = true;
123 self
124 }
125
126 pub fn run<F: ServerConfiguration>(self, factory: F) -> Server<F::Item> {
128 crate::manager::ServerManager::start(self, factory)
129 }
130}