1use ntex_util::time::Millis;
2
3use crate::{Server, ServerConfiguration};
4
5const DEFAULT_SHUTDOWN_TIMEOUT: Millis = Millis::from_secs(30);
6
7#[derive(Debug, Clone)]
8pub struct WorkerPool {
10 pub(crate) num: usize,
11 pub(crate) name: String,
12 pub(crate) no_signals: bool,
13 pub(crate) stop_runtime: bool,
14 pub(crate) shutdown_timeout: Millis,
15 pub(crate) affinity: bool,
16}
17
18impl Default for WorkerPool {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl WorkerPool {
25 #[must_use]
26 pub fn new() -> Self {
28 let num = core_affinity::get_core_ids().map_or_else(
29 || std::thread::available_parallelism().map_or(2, std::num::NonZeroUsize::get),
30 |v| v.len(),
31 );
32
33 WorkerPool {
34 num,
35 name: "ntex".to_string(),
36 no_signals: false,
37 stop_runtime: false,
38 shutdown_timeout: DEFAULT_SHUTDOWN_TIMEOUT,
39 affinity: false,
40 }
41 }
42
43 #[must_use]
44 pub fn name<T: AsRef<str>>(mut self, name: T) -> Self {
48 self.name = name.as_ref().to_string();
49 self
50 }
51
52 #[must_use]
53 pub fn workers(mut self, num: usize) -> Self {
58 self.num = num;
59 self
60 }
61
62 #[must_use]
63 pub fn stop_runtime(mut self) -> Self {
67 self.stop_runtime = true;
68 self
69 }
70
71 #[must_use]
72 pub fn disable_signals(mut self) -> Self {
76 self.no_signals = true;
77 self
78 }
79
80 #[must_use]
81 pub fn shutdown_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
89 self.shutdown_timeout = timeout.into();
90 self
91 }
92
93 #[must_use]
94 pub fn enable_affinity(mut self) -> Self {
98 self.affinity = true;
99 self
100 }
101
102 pub fn run<F: ServerConfiguration>(self, factory: F) -> Server<F::Item> {
104 crate::manager::ServerManager::start(self, factory)
105 }
106}