Skip to content

Commit 48531ac

Browse files
authored
Fix simultaneous downloads (#433)
Acquire a semaphore permit while executing each download task, instead of while _creating_ each download task. - Fixes #370 - Prevents deadlocking when downloading more than 75 mods - Aet reasonable number of concurrent downloads - Don't wrap `request::Client` in an `Arc` `request::Client` is just an `Arc` wrapper around its own internal type anyway.
1 parent e034941 commit 48531ac

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/download.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::{
1919
};
2020
use tokio::sync::Semaphore;
2121

22+
const CONCURRENT_DOWNLOADS: usize = 6;
23+
2224
/// Check the given `directory`
2325
///
2426
/// - If there are files there that are not in `to_download` or `to_install`, they will be moved to `directory`/.old
@@ -113,17 +115,17 @@ pub async fn download(
113115
.expect("Mutex poisoned")
114116
.enable_steady_tick(Duration::from_millis(100));
115117
let mut tasks = FuturesUnordered::new();
116-
let semaphore = Arc::new(Semaphore::new(75));
117-
let client = Arc::new(reqwest::Client::new());
118+
let semaphore = Arc::new(Semaphore::new(CONCURRENT_DOWNLOADS));
119+
let client = reqwest::Client::new();
118120

119121
for downloadable in to_download {
120-
let permit = Arc::clone(&semaphore).acquire_owned().await?;
122+
let semaphore = Arc::clone(&semaphore);
121123
let progress_bar = Arc::clone(&progress_bar);
122-
let client = Arc::clone(&client);
124+
let client = client.clone();
123125
let output_dir = output_dir.clone();
124126

125127
tasks.push(async move {
126-
let _permit = permit;
128+
let _permit = semaphore.acquire_owned().await?;
127129
let (length, filename) = downloadable
128130
.download(&client, &output_dir, |additional| {
129131
progress_bar

0 commit comments

Comments
 (0)