Skip to main content

ntex_server/
pool.rs

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)]
8/// Server builder
9pub 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    /// Create new Server builder instance
27    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    /// Set workers name.
45    ///
46    /// Name is used for worker thread name
47    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    /// Set number of workers to start.
54    ///
55    /// By default server uses number of available logical cpu as workers
56    /// count.
57    pub fn workers(mut self, num: usize) -> Self {
58        self.num = num;
59        self
60    }
61
62    #[must_use]
63    /// Stop current ntex runtime when manager get dropped.
64    ///
65    /// By default "stop runtime" is disabled.
66    pub fn stop_runtime(mut self) -> Self {
67        self.stop_runtime = true;
68        self
69    }
70
71    #[must_use]
72    /// Disable signal handling.
73    ///
74    /// By default signal handling is enabled.
75    pub fn disable_signals(mut self) -> Self {
76        self.no_signals = true;
77        self
78    }
79
80    #[must_use]
81    /// Timeout for graceful workers shutdown.
82    ///
83    /// After receiving a stop signal, workers have this much time to finish
84    /// serving requests. Workers still alive after the timeout are force
85    /// dropped.
86    ///
87    /// By default shutdown timeout sets to 30 seconds.
88    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    /// Enable core affinity
95    ///
96    /// By default affinity is disabled.
97    pub fn enable_affinity(mut self) -> Self {
98        self.affinity = true;
99        self
100    }
101
102    /// Starts processing incoming items and return server controller.
103    pub fn run<F: ServerConfiguration>(self, factory: F) -> Server<F::Item> {
104        crate::manager::ServerManager::start(self, factory)
105    }
106}