refactor(peer): make startup directory-driven
Peer startup used to bootstrap itself by spawning the runtime and immediately sending a SetGameDir command back through its own control channel. The Tauri integration then polled shared state until a directory appeared and waited two seconds before asking peers for games. That made startup ordering implicit and left a race-prone sleep in the UI bridge. Install the initial game directory directly into the peer context instead. The runtime now attempts the initial local-library scan before starting discovery, then launches the server, discovery, liveness, and local monitor services from that initialized context. Later directory changes still use SetGameDir, so the existing UI command surface stays intact. Use PathBuf and Path references across peer filesystem boundaries so directory state is represented as a path rather than an optional string. The Tauri layer now validates a selected game directory before storing it, loads the bundled catalog on first use, and starts or updates the peer runtime from one helper. Peer event fan-out is split into named handlers so the Tauri setup closure only wires state and starts the event loop. Shutdown goodbye notifications are still best-effort, but they are now awaited with a short timeout instead of being spawned and forgotten. The tradeoff is a small bounded wait during peer runtime shutdown in exchange for clearer task ownership. Test Plan: - cargo test -p lanspread-peer - cargo clippy - cargo clippy --benches - cargo clippy --tests - cargo +nightly fmt - git diff --check Refs: none
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! Peer runtime task startup and shutdown orchestration.
|
||||
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
|
||||
|
||||
use tokio::sync::{
|
||||
RwLock,
|
||||
@@ -22,44 +22,42 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
const EPHEMERAL_SERVER_ADDR: &str = "0.0.0.0:0";
|
||||
|
||||
pub(crate) fn spawn_peer_runtime(
|
||||
rx_control: UnboundedReceiver<PeerCommand>,
|
||||
tx_notify_ui: UnboundedSender<PeerEvent>,
|
||||
peer_game_db: Arc<RwLock<PeerGameDB>>,
|
||||
peer_id: String,
|
||||
game_dir: PathBuf,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = run_peer(rx_control, tx_notify_ui, peer_game_db, peer_id).await {
|
||||
if let Err(err) = run_peer(rx_control, tx_notify_ui, peer_game_db, peer_id, game_dir).await
|
||||
{
|
||||
log::error!("Peer system failed: {err}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_startup_services(
|
||||
ctx: &Ctx,
|
||||
tx_notify_ui: &UnboundedSender<PeerEvent>,
|
||||
) -> eyre::Result<()> {
|
||||
spawn_quic_server(ctx, tx_notify_ui)?;
|
||||
pub(crate) fn spawn_startup_services(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
|
||||
spawn_quic_server(ctx, tx_notify_ui);
|
||||
spawn_peer_discovery_service(ctx, tx_notify_ui);
|
||||
spawn_peer_liveness_service(ctx, tx_notify_ui);
|
||||
spawn_local_library_monitor(ctx, tx_notify_ui);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn spawn_goodbye_notifications(ctx: &Ctx) {
|
||||
pub(crate) async fn send_goodbye_notifications(ctx: &Ctx) {
|
||||
let peer_id = ctx.peer_id.as_ref().clone();
|
||||
let peer_addresses = { ctx.peer_game_db.read().await.get_peer_addresses() };
|
||||
|
||||
for peer_addr in peer_addresses {
|
||||
spawn_goodbye_notification(peer_addr, peer_id.clone());
|
||||
}
|
||||
futures::future::join_all(
|
||||
peer_addresses
|
||||
.into_iter()
|
||||
.map(|peer_addr| send_goodbye_notification(peer_addr, peer_id.clone())),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
fn spawn_quic_server(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) -> eyre::Result<()> {
|
||||
let server_addr = EPHEMERAL_SERVER_ADDR.parse::<SocketAddr>()?;
|
||||
fn spawn_quic_server(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
|
||||
let server_addr = SocketAddr::from(([0, 0, 0, 0], 0));
|
||||
let peer_ctx = ctx.to_peer_ctx(tx_notify_ui.clone());
|
||||
let tx_notify_ui = tx_notify_ui.clone();
|
||||
|
||||
@@ -68,8 +66,6 @@ fn spawn_quic_server(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) -> ey
|
||||
log::error!("Server component error: {err}");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn spawn_peer_discovery_service(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
|
||||
@@ -107,10 +103,10 @@ fn spawn_local_library_monitor(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEve
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_goodbye_notification(peer_addr: SocketAddr, peer_id: String) {
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = send_goodbye(peer_addr, peer_id).await {
|
||||
log::warn!("Failed to send Goodbye to {peer_addr}: {err}");
|
||||
}
|
||||
});
|
||||
async fn send_goodbye_notification(peer_addr: SocketAddr, peer_id: String) {
|
||||
match tokio::time::timeout(Duration::from_secs(1), send_goodbye(peer_addr, peer_id)).await {
|
||||
Ok(Ok(())) => {}
|
||||
Ok(Err(err)) => log::warn!("Failed to send Goodbye to {peer_addr}: {err}"),
|
||||
Err(_) => log::warn!("Timed out sending Goodbye to {peer_addr}"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user