diff --git a/Cargo.lock b/Cargo.lock index 85069f8..88e96ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1002,6 +1002,7 @@ dependencies = [ "actix-web", "env_logger", "log", + "rand", "regex", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 2f59be9..4606b6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ actix-files = "0.6.5" actix-web = "4.5.1" env_logger = "0.11.3" log = "0.4.21" +rand = "0.8.5" regex = "1.10.4" reqwest = "0.12.3" serde = { version = "1.0.197", features = ["derive"] } diff --git a/src/proxy.rs b/src/proxy.rs index f402948..c294464 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -1,9 +1,12 @@ -use std::path::{Path, PathBuf}; - use actix_web::{HttpRequest, HttpResponse}; +use rand::prelude::*; +use std::{ + path::{Path, PathBuf}, + sync::Arc, +}; pub struct ProxyMirror { - mirrors: Vec, + mirrors: Vec>, data_dir: String, no_cache: regex::Regex, } @@ -11,7 +14,7 @@ pub struct ProxyMirror { impl ProxyMirror { pub fn new(mirrors: Vec, data_dir: &str, no_cache: &str) -> Self { Self { - mirrors, + mirrors: mirrors.into_iter().map(|x| Arc::new(x)).collect(), data_dir: data_dir.to_string(), no_cache: regex::Regex::new(no_cache).unwrap(), } @@ -59,8 +62,11 @@ impl ProxyMirror { } } - for mirror in &self.mirrors { - log::info!("Fetching {path} from mirrors"); + let mut mirrors = self.mirrors.clone(); + mirrors.shuffle(&mut rand::thread_rng()); + + log::info!("Fetching {path} from mirrors"); + for mirror in mirrors { let url = format!("{mirror}{path}"); let res = self.get_url(&url, &p).await; if let Some(res) = res {