Skip to main content

ntex_server/
lib.rs

1//! Worker-based server infrastructure for ntex.
2//!
3//! [`WorkerPool`] runs services across one or more worker threads. The [`net`]
4//! module builds TCP and Unix domain socket servers on top of that pool.
5//! [`Server`] is the controller used to pause, resume, stop, or await a running
6//! server.
7
8#![deny(clippy::pedantic)]
9#![allow(
10    async_fn_in_trait,
11    clippy::clone_on_copy,
12    clippy::must_use_candidate,
13    clippy::missing_fields_in_debug,
14    clippy::missing_errors_doc,
15    clippy::missing_panics_doc,
16    clippy::unused_async
17)]
18
19use ntex_service::Service;
20
21mod manager;
22pub mod net;
23mod pool;
24mod server;
25mod state;
26mod wrk;
27
28pub use self::pool::WorkerPool;
29pub use self::server::Server;
30pub use self::state::{NoConfig, ServerAppConfig};
31pub use self::wrk::{Worker, WorkerStatus, WorkerStop};
32
33/// Identifier assigned to a server worker.
34#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
35pub struct WorkerId(pub(crate) usize);
36
37impl WorkerId {
38    pub(self) fn next(&mut self) -> WorkerId {
39        let id = WorkerId(self.0);
40        self.0 += 1;
41        id
42    }
43}
44
45/// Worker service configuration.
46pub trait ServerConfiguration: Send + Clone + 'static {
47    /// Item dispatched to a worker service.
48    type Item: Send + 'static;
49    /// Service created independently for each worker.
50    type Service: Service<(), Self::Item, Res = (), Error = ()> + 'static;
51
52    /// Creates the service used by one worker.
53    async fn create(&self) -> std::io::Result<Self::Service>;
54
55    /// Pause the server.
56    fn pause(&self) {}
57
58    /// Resume the server.
59    fn resume(&self) {}
60
61    /// Called when the server is terminated immediately.
62    fn terminate(&self) {}
63
64    /// Performs asynchronous cleanup when the server stops.
65    async fn stop(&self) {}
66}