diff --git a/src/servers/udp/server/mod.rs b/src/servers/udp/server/mod.rs index 1bb9831e..034f71be 100644 --- a/src/servers/udp/server/mod.rs +++ b/src/servers/udp/server/mod.rs @@ -1,22 +1,4 @@ //! Module to handle the UDP server instances. -//! -//! There are two main types in this module: -//! -//! - [`UdpServer`]: a controller to start and stop the server. -//! - [`Udp`]: the server launcher. -//! -//! The `UdpServer` is an state machine for a given configuration. This struct -//! represents concrete configuration and state. It allows to start and -//! stop the server but always keeping the same configuration. -//! -//! The `Udp` is the server launcher. It's responsible for launching the UDP -//! but without keeping any state. -//! -//! For the time being, the `UdpServer` is only used for testing purposes, -//! because we want to be able to start and stop the server multiple times, and -//! we want to know the bound address and the current state of the server. -//! In production, the `Udp` launcher is used directly. - use std::fmt::Debug; use super::RawRequest; @@ -37,7 +19,7 @@ pub mod states; /// /// Some errors triggered while stopping the server are: /// -/// - The [`UdpServer`] cannot send the shutdown signal to the spawned UDP service thread. +/// - The [`Server`] cannot send the shutdown signal to the spawned UDP service thread. #[derive(Debug)] pub enum UdpError { /// Any kind of error starting or stopping the server. @@ -92,7 +74,7 @@ mod tests { tokio::time::sleep(Duration::from_secs(1)).await; - assert_eq!(stopped.state.launcher.bind_to, bind_to); + assert_eq!(stopped.state.spawner.bind_to, bind_to); } #[tokio::test] @@ -116,7 +98,7 @@ mod tests { tokio::time::sleep(Duration::from_secs(1)).await; - assert_eq!(stopped.state.launcher.bind_to, bind_to); + assert_eq!(stopped.state.spawner.bind_to, bind_to); } } diff --git a/src/servers/udp/server/spawner.rs b/src/servers/udp/server/spawner.rs index a36404fc..e4612fbe 100644 --- a/src/servers/udp/server/spawner.rs +++ b/src/servers/udp/server/spawner.rs @@ -1,3 +1,4 @@ +//! A thin wrapper for tokio spawn to launch the UDP server launcher as a new task. use std::net::SocketAddr; use std::sync::Arc; @@ -16,21 +17,22 @@ pub struct Spawner { } impl Spawner { - /// It spawns a new tasks to run the UDP server instance. + /// It spawns a new task to run the UDP server instance. /// /// # Panics /// /// It would panic if unable to resolve the `local_addr` from the supplied ´socket´. - pub fn start( + pub fn spawn_launcher( &self, tracker: Arc, tx_start: oneshot::Sender, rx_halt: oneshot::Receiver, ) -> JoinHandle { - let launcher = Spawner::new(self.bind_to); + let spawner = Self::new(self.bind_to); + tokio::spawn(async move { - Launcher::run_with_graceful_shutdown(tracker, launcher.bind_to, tx_start, rx_halt).await; - launcher + Launcher::run_with_graceful_shutdown(tracker, spawner.bind_to, tx_start, rx_halt).await; + spawner }) } } diff --git a/src/servers/udp/server/states.rs b/src/servers/udp/server/states.rs index 919646d7..d0a2e4e8 100644 --- a/src/servers/udp/server/states.rs +++ b/src/servers/udp/server/states.rs @@ -25,7 +25,7 @@ pub type RunningUdpServer = Server; /// A stopped UDP server state. pub struct Stopped { - pub launcher: Spawner, + pub spawner: Spawner, } /// A running UDP server state. @@ -40,9 +40,9 @@ pub struct Running { impl Server { /// Creates a new `UdpServer` instance in `stopped`state. #[must_use] - pub fn new(launcher: Spawner) -> Self { + pub fn new(spawner: Spawner) -> Self { Self { - state: Stopped { launcher }, + state: Stopped { spawner }, } } @@ -64,7 +64,7 @@ impl Server { assert!(!tx_halt.is_closed(), "Halt channel for UDP tracker should be open"); // May need to wrap in a task to about a tokio bug. - let task = self.state.launcher.start(tracker, tx_start, rx_halt); + let task = self.state.spawner.spawn_launcher(tracker, tx_start, rx_halt); let binding = rx_start.await.expect("it should be able to start the service").address; let local_addr = format!("udp://{binding}"); @@ -107,7 +107,7 @@ impl Server { let launcher = self.state.task.await.expect("it should shutdown service"); let stopped_api_server: Server = Server { - state: Stopped { launcher }, + state: Stopped { spawner: launcher }, }; Ok(stopped_api_server)