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:
@@ -450,17 +450,7 @@ async fn update_state_from_event(shared: &SharedState, event: PeerEvent) -> (&'s
|
|||||||
json!({"game_id": id, "file_descriptions": file_descriptions}),
|
json!({"game_id": id, "file_descriptions": file_descriptions}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
PeerEvent::DownloadGameFilesBegin { id } => {
|
PeerEvent::DownloadGameFilesBegin { id } => download_begin_event(shared, id).await,
|
||||||
shared.state.write().await.downloads.insert(
|
|
||||||
id.clone(),
|
|
||||||
DownloadMeasurement {
|
|
||||||
started_at: Instant::now(),
|
|
||||||
bytes: 0,
|
|
||||||
chunks: 0,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
("download-begin", json!({"game_id": id}))
|
|
||||||
}
|
|
||||||
PeerEvent::DownloadGameFileChunkFinished {
|
PeerEvent::DownloadGameFileChunkFinished {
|
||||||
id,
|
id,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
@@ -468,20 +458,8 @@ async fn update_state_from_event(shared: &SharedState, event: PeerEvent) -> (&'s
|
|||||||
offset,
|
offset,
|
||||||
length,
|
length,
|
||||||
} => {
|
} => {
|
||||||
if let Some(measurement) = shared.state.write().await.downloads.get_mut(&id) {
|
download_chunk_finished_event(shared, id, peer_addr, relative_path, offset, length)
|
||||||
measurement.bytes = measurement.bytes.saturating_add(length);
|
.await
|
||||||
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,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
PeerEvent::DownloadGameFilesFinished { id } => {
|
PeerEvent::DownloadGameFilesFinished { id } => {
|
||||||
download_terminal_event(shared, "download-finished", id).await
|
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}))
|
(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(
|
async fn download_terminal_event(
|
||||||
shared: &SharedState,
|
shared: &SharedState,
|
||||||
kind: &'static str,
|
kind: &'static str,
|
||||||
|
|||||||
Reference in New Issue
Block a user