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#[allow(clippy::struct_excessive_bools)]
8#[derive(Debug, Clone)]
9/// Server builder
10pub 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    /// Create new Server builder instance
30    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    /// Set workers name.
50    ///
51    /// Name is used for worker thread name
52    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    /// Set number of workers to start.
59    ///
60    /// By default server uses number of available logical cpu as workers
61    /// count.
62    pub fn workers(mut self, num: usize) -> Self {
63        self.num = num;
64        self
65    }
66
67    #[must_use]
68    /// Stop current ntex runtime when manager get dropped.
69    ///
70    /// By default "stop runtime" is disabled.
71    pub fn stop_runtime(mut self) -> Self {
72        self.stop_runtime = true;
73        self
74    }
75
76    #[must_use]
77    /// Stops the server when one of the workers panics.
78    ///
79    /// By default, "stop on panic" is disabled.
80    pub fn stop_on_panic(mut self) -> Self {
81        self.stop_on_panic = true;
82        self
83    }
84
85    #[must_use]
86    /// Disable signal handling.
87    ///
88    /// By default, signal handling is enabled.
89    pub fn disable_signals(mut self) -> Self {
90        self.no_signals = true;
91        self
92    }
93
94    #[must_use]
95    /// Graceful shutdown.
96    ///
97    /// Gracefully shuts down on SIGTERM, SIGSEGV, or SIGQUIT.
98    /// By default, graceful shutdown is disabled.
99    pub fn graceful_shutdown(mut self) -> Self {
100        self.graceful_shutdown = true;
101        self
102    }
103
104    #[must_use]
105    /// Timeout for graceful worker shutdown.
106    ///
107    /// After receiving a stop signal, workers have this much time to finish
108    /// serving requests. Workers that are still alive after the timeout are
109    /// forcefully dropped.
110    ///
111    /// By default, the shutdown timeout is set to 30 seconds.
112    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    /// Enable cpu affinity.
119    ///
120    /// By default, affinity is disabled.
121    pub fn enable_affinity(mut self) -> Self {
122        self.affinity = true;
123        self
124    }
125
126    /// Starts processing incoming items and return server controller.
127    pub fn run<F: ServerConfiguration>(self, factory: F) -> Server<F::Item> {
128        crate::manager::ServerManager::start(self, factory)
129    }
130}