use ntex_util::time::Millis;
use crate::{Server, ServerConfiguration};
const DEFAULT_SHUTDOWN_TIMEOUT: Millis = Millis::from_secs(30);
#[derive(Debug, Clone)]
pub struct WorkerPool {
pub(crate) num: usize,
pub(crate) name: String,
pub(crate) no_signals: bool,
pub(crate) stop_runtime: bool,
pub(crate) shutdown_timeout: Millis,
pub(crate) affinity: bool,
}
impl Default for WorkerPool {
fn default() -> Self {
Self::new()
}
}
impl WorkerPool {
#[must_use]
pub fn new() -> Self {
let num = core_affinity::get_core_ids().map_or_else(
|| std::thread::available_parallelism().map_or(2, std::num::NonZeroUsize::get),
|v| v.len(),
);
WorkerPool {
num,
name: "ntex".to_string(),
no_signals: false,
stop_runtime: false,
shutdown_timeout: DEFAULT_SHUTDOWN_TIMEOUT,
affinity: false,
}
}
#[must_use]
pub fn name<T: AsRef<str>>(mut self, name: T) -> Self {
self.name = name.as_ref().to_string();
self
}
#[must_use]
pub fn workers(mut self, num: usize) -> Self {
self.num = num;
self
}
#[must_use]
pub fn stop_runtime(mut self) -> Self {
self.stop_runtime = true;
self
}
#[must_use]
pub fn disable_signals(mut self) -> Self {
self.no_signals = true;
self
}
#[must_use]
pub fn shutdown_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
self.shutdown_timeout = timeout.into();
self
}
#[must_use]
pub fn enable_affinity(mut self) -> Self {
self.affinity = true;
self
}
pub fn run<F: ServerConfiguration>(self, factory: F) -> Server<F::Item> {
crate::manager::ServerManager::start(self, factory)
}
}