Queue is running game downloads sequentially now

This commit is contained in:
quexeky
2024-10-28 20:57:42 +11:00
parent 1ab61c86b1
commit 5564d23536
4 changed files with 26 additions and 7 deletions

View File

@ -67,12 +67,15 @@ impl GameDownloadAgent {
self.ensure_manifest_exists().await
}
pub fn begin_download(&self, max_threads: usize) -> Result<(), GameDownloadError> {
pub async fn begin_download(&self, max_threads: usize) -> Result<(), GameDownloadError> {
self.change_state(GameDownloadState::Downloading);
// TODO we're coping the whole context thing
// It's not necessary, I just can't figure out to make the borrow checker happy
self.progress
.run_contexts_parallel(self.contexts.lock().unwrap().to_vec(), max_threads);
{
let lock = self.contexts.lock().unwrap().to_vec();
self.progress
.run_context_parallel(lock, max_threads).await;
}
Ok(())
}

View File

@ -46,8 +46,8 @@ pub async fn start_game_downloads(
}
};
info!("Downloading game");
start_game_download(max_threads, download_agent.unwrap()).await?;
{
start_game_download(max_threads, download_agent.unwrap()).await?;
let mut lock = state.lock().unwrap();
lock.game_downloads.remove_entry(&current_id);
}
@ -70,7 +70,7 @@ pub async fn start_game_download(
download_agent.generate_job_contexts(&local_manifest, download_agent.version.clone(), download_agent.id.clone()).unwrap();
download_agent.begin_download(max_threads)?;
download_agent.begin_download(max_threads).await?;
Ok(())
}

View File

@ -1,4 +1,6 @@
use rayon::ThreadPoolBuilder;
use uuid::timestamp::context;
use std::os::unix::thread;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
@ -35,7 +37,7 @@ where
self.counter.fetch_add(1, Ordering::Relaxed);
}
}
pub fn run_contexts_parallel(&self, contexts: Vec<T>, max_threads: usize) {
pub fn run_contexts_parallel_background(&self, contexts: Vec<T>, max_threads: usize) {
let threads = ThreadPoolBuilder::new()
// If max_threads == 0, then the limit will be determined
// by Rayon's internal RAYON_NUM_THREADS
@ -48,6 +50,20 @@ where
threads.spawn(move || f(context));
}
}
pub async fn run_context_parallel(&self, contexts: Vec<T>, max_threads: usize) {
let threads = ThreadPoolBuilder::new()
.num_threads(max_threads)
.build()
.unwrap();
threads.scope(|s| {
for context in contexts {
let f = self.f.clone();
s.spawn(move |_| f(context));
}
});
}
pub fn get_progress(&self) -> usize {
self.counter.load(Ordering::Relaxed)
}

View File

@ -13,7 +13,7 @@ fn test_progress_sequentially() {
fn test_progress_parallel() {
let counter = Arc::new(AtomicUsize::new(0));
let p = ProgressChecker::new(Box::new(test_fn), counter.clone());
p.run_contexts_parallel((1..100).collect(), 10);
p.run_contexts_parallel_background((1..100).collect(), 10);
}
fn test_fn(int: usize) {