use crate::net::DriverKind;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Workers {
pub(crate) kind: DriverKind,
pub(crate) count: Option<u16>,
}
pub(crate) const BLOCKING_DEFAULT: u16 = 256;
pub(crate) const REACTOR_CONNECTIONS: u32 = 10_000;
impl Workers {
#[must_use]
pub fn auto() -> Self {
Workers {
kind: DriverKind::Auto,
count: None,
}
}
#[must_use]
pub fn reactor(workers: u16) -> Self {
Workers {
kind: DriverKind::Reactor,
count: (workers > 0).then_some(workers),
}
}
#[must_use]
pub fn blocking(threads: u16) -> Self {
Workers {
kind: DriverKind::Blocking,
count: Some(threads),
}
}
#[must_use]
pub fn kind(&self) -> DriverKind {
self.kind
}
#[must_use]
pub fn count(&self) -> Option<u16> {
self.count
}
pub(crate) fn resolved(&self) -> DriverKind {
match self.kind {
DriverKind::Auto if cfg!(serve_net_blocking_tests) => DriverKind::Blocking,
DriverKind::Auto => DriverKind::Reactor,
kind => kind,
}
}
pub(crate) fn reactor_workers(&self) -> u16 {
self.count.unwrap_or_else(|| {
std::thread::available_parallelism()
.map_or(4, |n| u16::try_from(n.get()).unwrap_or(4096).min(4096))
})
}
pub(crate) fn blocking_threads(&self) -> u16 {
self.count.unwrap_or(BLOCKING_DEFAULT)
}
}
impl Default for Workers {
fn default() -> Self {
Workers::auto()
}
}