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.
This commit is contained in:
2026-05-20 08:40:03 +02:00
parent d7f7dc737e
commit 44e0629926
+40 -25
View File
@@ -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,