chore: refactor downloads

This commit is contained in:
Michel de Graaf
2025-11-18 14:11:25 +01:00
parent 467de9cb91
commit 79da19ed0f
5 changed files with 114 additions and 59 deletions
+9 -9
View File
@@ -242,8 +242,7 @@ impl DownloadPeer {
let client_guard = client_context
.read()
.map_err(|_| DownloadError::LockPoisoned)?;
let download_info =
client_guard.download_tokens.get(&token_u32).cloned();
let download_info = client_guard.get_download_by_token(token_u32).cloned();
drop(client_guard);
download_info.ok_or(DownloadError::TokenNotFound(token_u32))
@@ -318,13 +317,13 @@ impl DownloadPeer {
0.0
};
let _ =
dl.sender.send(DownloadStatus::InProgress {
bytes_downloaded: processor.total_bytes
as u64,
total_bytes: dl.size,
speed_bytes_per_sec: speed,
});
let status = DownloadStatus::InProgress {
bytes_downloaded: processor.total_bytes as u64,
total_bytes: dl.size,
speed_bytes_per_sec: speed,
};
let _ = dl.sender.send(status.clone());
client_context.write().unwrap().update_download_with_status(dl.token, status);
last_update_time = Instant::now();
}
@@ -422,6 +421,7 @@ impl DownloadPeer {
if let Some(ref dl) = download {
let _ = dl.sender.send(DownloadStatus::Queued);
client_context.write().unwrap().update_download_with_status(dl.token, DownloadStatus::Queued);
}
let mut stream = match stream {
+3 -3
View File
@@ -84,11 +84,10 @@ fn extract_download_from_buffer(
);
let context = client_context.read().unwrap();
let download = context.download_tokens.get(&token).cloned();
let download = context.get_download_by_token(token).cloned();
if download.is_none() {
let download_tokens: Vec<u32> =
context.download_tokens.keys().cloned().collect();
let download_tokens = context.get_download_tokens();
trace!("[listener:{peer_ip}:{peer_port}] download token not found: {:?}, download tokens: {:?}", token, download_tokens);
}
@@ -158,6 +157,7 @@ fn handle_file_connection(
) {
Ok((download, filename)) => {
let _ = download.sender.send(DownloadStatus::Completed);
context.client_context.write().unwrap().update_download_with_status(download.token, DownloadStatus::Completed);
info!(
"Successfully downloaded {} bytes to {}",
download.size, filename