Skip to content

Commit

Permalink
docs: update some UDP server comments
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 25, 2024
1 parent 89bb735 commit f06976e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
24 changes: 3 additions & 21 deletions src/servers/udp/server/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/servers/udp/server/spawner.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Tracker>,
tx_start: oneshot::Sender<Started>,
rx_halt: oneshot::Receiver<Halted>,
) -> JoinHandle<Spawner> {
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
})
}
}
10 changes: 5 additions & 5 deletions src/servers/udp/server/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub type RunningUdpServer = Server<Running>;
/// A stopped UDP server state.

pub struct Stopped {
pub launcher: Spawner,
pub spawner: Spawner,
}

/// A running UDP server state.
Expand All @@ -40,9 +40,9 @@ pub struct Running {
impl Server<Stopped> {
/// 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 },
}
}

Expand All @@ -64,7 +64,7 @@ impl Server<Stopped> {
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}");
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Server<Running> {
let launcher = self.state.task.await.expect("it should shutdown service");

let stopped_api_server: Server<Stopped> = Server {
state: Stopped { launcher },
state: Stopped { spawner: launcher },
};

Ok(stopped_api_server)
Expand Down

0 comments on commit f06976e

Please sign in to comment.