random mirror selection

This commit is contained in:
JMARyA 2024-04-12 08:33:17 +02:00
parent d89760702a
commit aeb41e5d44
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 14 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1002,6 +1002,7 @@ dependencies = [
"actix-web", "actix-web",
"env_logger", "env_logger",
"log", "log",
"rand",
"regex", "regex",
"reqwest", "reqwest",
"serde", "serde",

View file

@ -8,6 +8,7 @@ actix-files = "0.6.5"
actix-web = "4.5.1" actix-web = "4.5.1"
env_logger = "0.11.3" env_logger = "0.11.3"
log = "0.4.21" log = "0.4.21"
rand = "0.8.5"
regex = "1.10.4" regex = "1.10.4"
reqwest = "0.12.3" reqwest = "0.12.3"
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }

View file

@ -1,9 +1,12 @@
use std::path::{Path, PathBuf};
use actix_web::{HttpRequest, HttpResponse}; use actix_web::{HttpRequest, HttpResponse};
use rand::prelude::*;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
pub struct ProxyMirror { pub struct ProxyMirror {
mirrors: Vec<String>, mirrors: Vec<Arc<String>>,
data_dir: String, data_dir: String,
no_cache: regex::Regex, no_cache: regex::Regex,
} }
@ -11,7 +14,7 @@ pub struct ProxyMirror {
impl ProxyMirror { impl ProxyMirror {
pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str) -> Self { pub fn new(mirrors: Vec<String>, data_dir: &str, no_cache: &str) -> Self {
Self { Self {
mirrors, mirrors: mirrors.into_iter().map(|x| Arc::new(x)).collect(),
data_dir: data_dir.to_string(), data_dir: data_dir.to_string(),
no_cache: regex::Regex::new(no_cache).unwrap(), no_cache: regex::Regex::new(no_cache).unwrap(),
} }
@ -59,8 +62,11 @@ impl ProxyMirror {
} }
} }
for mirror in &self.mirrors { let mut mirrors = self.mirrors.clone();
log::info!("Fetching {path} from mirrors"); mirrors.shuffle(&mut rand::thread_rng());
log::info!("Fetching {path} from mirrors");
for mirror in mirrors {
let url = format!("{mirror}{path}"); let url = format!("{mirror}{path}");
let res = self.get_url(&url, &p).await; let res = self.get_url(&url, &p).await;
if let Some(res) = res { if let Some(res) = res {