Skip to content

Commit

Permalink
Saving download state before create temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Nov 9, 2023
1 parent 95960c2 commit 16e3bfe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct Args {
proxy: Option<String>,
}

type DownloadState = RwLock<HashMap<[u8; 20], (Arc<TempPath>, watch::Receiver<u64>)>>;
type DownloadState = RwLock<HashMap<[u8; 20], (watch::Receiver<Option<Arc<TempPath>>>, watch::Receiver<u64>)>>;

struct AppState {
runtime: Handle,
Expand Down
18 changes: 13 additions & 5 deletions src/route/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ async fn hath(

// Check if the file is already downloading
let download_state = data.download_state.read().get(&info.hash()).cloned();
let (temp_path, mut rx) = if let Some(state) = download_state {
state
let (temp_path, mut rx) = if let Some((mut tempfile, progress)) = download_state {
let tempfile = tempfile.wait_for(Option::is_some).await;
if let Err(err) = tempfile {
error!("Waiting tempfile create error: {}", err);
data.download_state.write().remove(&info.hash());
return HttpResponse::NotFound().body("An error has occurred. (404)");
}
(tempfile.unwrap().as_ref().unwrap().clone(), progress)
} else {
let temp_path = Arc::new(data.cache_manager.create_temp_file().await);
// Tracking download progress
let (temp_tx, temp_rx) = watch::channel(None); // Tempfile
let (tx, rx) = watch::channel(0); // Download progress
data.download_state.write().insert(info.hash(), (temp_rx.clone(), rx.clone()));

// Tracking download progress
data.download_state.write().insert(info.hash(), (temp_path.clone(), rx.clone()));
let temp_path = Arc::new(data.cache_manager.create_temp_file().await);
temp_tx.send_replace(Some(temp_path.clone()));

let sources = match data.rpc.sr_fetch(file_index, xres, &file_id).await {
Some(v) => v,
Expand Down

0 comments on commit 16e3bfe

Please sign in to comment.