From 44e06299269a989876c155d9943d8b8241a4e3ed Mon Sep 17 00:00:00 2001 From: ddidderr Date: Wed, 20 May 2026 08:40:03 +0200 Subject: [PATCH] refactor(peer-cli): split download measurement event handlers Extract the download-begin and chunk-finished measurement bookkeeping out of the main peer-cli event reducer. This keeps the S37 throughput reporting behavior unchanged while bringing the reducer back under the pedantic clippy line-count threshold. Test Plan: - just fmt - just clippy - just test Refs: S37 download throughput measurement harness. --- crates/lanspread-peer-cli/src/main.rs | 65 ++++++++++++++++----------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/crates/lanspread-peer-cli/src/main.rs b/crates/lanspread-peer-cli/src/main.rs index 99c626d..32753d7 100644 --- a/crates/lanspread-peer-cli/src/main.rs +++ b/crates/lanspread-peer-cli/src/main.rs @@ -450,17 +450,7 @@ async fn update_state_from_event(shared: &SharedState, event: PeerEvent) -> (&'s json!({"game_id": id, "file_descriptions": file_descriptions}), ) } - PeerEvent::DownloadGameFilesBegin { id } => { - shared.state.write().await.downloads.insert( - id.clone(), - DownloadMeasurement { - started_at: Instant::now(), - bytes: 0, - chunks: 0, - }, - ); - ("download-begin", json!({"game_id": id})) - } + PeerEvent::DownloadGameFilesBegin { id } => download_begin_event(shared, id).await, PeerEvent::DownloadGameFileChunkFinished { id, peer_addr, @@ -468,20 +458,8 @@ async fn update_state_from_event(shared: &SharedState, event: PeerEvent) -> (&'s offset, length, } => { - if let Some(measurement) = shared.state.write().await.downloads.get_mut(&id) { - measurement.bytes = measurement.bytes.saturating_add(length); - measurement.chunks = measurement.chunks.saturating_add(1); - } - ( - "download-chunk-finished", - json!({ - "game_id": id, - "peer_addr": peer_addr.to_string(), - "relative_path": relative_path, - "offset": offset, - "length": length, - }), - ) + download_chunk_finished_event(shared, id, peer_addr, relative_path, offset, length) + .await } PeerEvent::DownloadGameFilesFinished { id } => { download_terminal_event(shared, "download-finished", id).await @@ -521,6 +499,43 @@ fn game_id_event(kind: &'static str, id: String) -> (&'static str, Value) { (kind, json!({"game_id": id})) } +async fn download_begin_event(shared: &SharedState, id: String) -> (&'static str, Value) { + shared.state.write().await.downloads.insert( + id.clone(), + DownloadMeasurement { + started_at: Instant::now(), + bytes: 0, + chunks: 0, + }, + ); + game_id_event("download-begin", id) +} + +async fn download_chunk_finished_event( + shared: &SharedState, + id: String, + peer_addr: SocketAddr, + relative_path: String, + offset: u64, + length: u64, +) -> (&'static str, Value) { + if let Some(measurement) = shared.state.write().await.downloads.get_mut(&id) { + measurement.bytes = measurement.bytes.saturating_add(length); + measurement.chunks = measurement.chunks.saturating_add(1); + } + + ( + "download-chunk-finished", + json!({ + "game_id": id, + "peer_addr": peer_addr.to_string(), + "relative_path": relative_path, + "offset": offset, + "length": length, + }), + ) +} + async fn download_terminal_event( shared: &SharedState, kind: &'static str,