parallel downloads

This commit is contained in:
JMARyA 2025-03-08 23:30:02 +01:00
parent ac6dd1479d
commit 71fa998861
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 136 additions and 18 deletions

View file

@ -1,5 +1,6 @@
use std::path::PathBuf;
use comrade::rally;
use rand::seq::SliceRandom;
use super::{Package, Repository, arch::Architecture};
@ -31,33 +32,46 @@ impl MirrorRepository {
arch.to_string()
};
let mut mirrorlist = mirrorlist.to_vec();
mirrorlist.shuffle(&mut rand::rng());
let parent = file_path.parent().unwrap();
std::fs::create_dir_all(parent).unwrap();
for mirror in mirrorlist {
let mirror = mirror
.replace("$repo", &self.inner.name)
.replace("$arch", &arch);
let mirrorlist_links = mirrorlist
.into_iter()
.take(5)
.map(|x| {
let mirror = x.replace("$repo", &self.inner.name).replace("$arch", &arch);
let url = format!("{mirror}/{url}");
log::info!("Trying mirror {url}");
(
x.clone(),
format!("{mirror}/{url}"),
file_path.to_str().unwrap().to_string(),
)
})
.collect();
let client = reqwest::Client::new();
if let Ok(resp) = client.get(&url).send().await {
let data = rally(mirrorlist_links, |input| {
let (mirror, url, file_path) = input;
let client = reqwest::blocking::Client::new();
if let Ok(resp) = client.get(url).send() {
if resp.status().is_success() {
let data = resp.bytes().await.unwrap().to_vec();
let parent = file_path.parent().unwrap();
std::fs::create_dir_all(parent).unwrap();
let data = resp.bytes().unwrap().to_vec();
log::info!("Downloaded {url}");
std::fs::write(file_path, data).unwrap();
return;
return Some(data);
} else {
log::warn!("Mirror {mirror} failed [{}]", resp.status().as_u16());
return None;
}
}
None
});
if let Some(data) = data.1 {
std::fs::write(file_path, data).unwrap();
} else {
log::error!("Downloading {url} failed. No mirror could provide a valid response.");
}
log::error!("Downloading {url} failed. No mirror could provide a valid response.")
}
/// Get the `.db.tar.gz` content for the repository of `arch`