From 726804e73df3782679acf38611d105903bb1ea63 Mon Sep 17 00:00:00 2001 From: Lowder Date: Sat, 21 Feb 2026 01:55:52 +0500 Subject: [PATCH] fix: download bases without locking --- Cargo.lock | 2 +- querying/src/lib.rs | 44 +++++++++++++++----------------------------- website/Cargo.toml | 2 +- website/src/main.rs | 10 ++++++++-- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6122cff..e3e84ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4225,7 +4225,7 @@ dependencies = [ [[package]] name = "website" -version = "0.2.2" +version = "0.2.4" dependencies = [ "dotenvy", "env_logger", diff --git a/querying/src/lib.rs b/querying/src/lib.rs index 6f61ffa..74ecba1 100644 --- a/querying/src/lib.rs +++ b/querying/src/lib.rs @@ -7,6 +7,7 @@ use chrono::{DateTime, Utc}; use ipnet::IpNet; use log::error; use std::collections::{HashMap, HashSet}; +use std::io; use std::net::IpAddr; use std::sync::Arc; use maxminddb::MaxMindDbError; @@ -53,6 +54,8 @@ pub enum CheckError { NotFound, } +pub type Bases = (::Base, ::Base, ::Base); + impl Checker { pub async fn new() -> Checker { let (tx, rx) = watch::channel(None); @@ -132,38 +135,21 @@ impl Checker { self.rx.borrow().clone() } - pub async fn update_all(&self) { - match GeoIp::download().await { - Ok(base) => { - if let Err(e) = self.geo_ip.write().await.install(base).await { - error!("Failed to update GeoIP: {}", e); - } - } - Err(e) => { - error!("Failed to download GeoIP: {}", e); - } + pub async fn download_all() -> Result { + Ok((GeoIp::download().await?, RuBlacklist::download().await?, CdnList::download().await?)) + } + + pub async fn update_all(&self, (geo_ip, ru_blacklist, cdn_list): Bases) { + if let Err(e) = self.geo_ip.write().await.install(geo_ip).await { + error!("Failed to update GeoIP: {}", e); } - match RuBlacklist::download().await { - Ok(base) => { - if let Err(e) = self.ru_blacklist.write().await.install(base).await { - error!("Failed to update RKN: {}", e); - } - } - Err(e) => { - error!("Failed to download RKN: {}", e); - } + if let Err(e) = self.ru_blacklist.write().await.install(ru_blacklist).await { + error!("Failed to update RKN: {}", e); + } + if let Err(e) = self.cdn_list.write().await.install(cdn_list).await { + error!("Failed to update CDN: {}", e); } - match CdnList::download().await { - Ok(base) => { - if let Err(e) = self.cdn_list.write().await.install(base).await { - error!("Failed to update CDN: {}", e); - } - } - Err(e) => { - error!("Failed to download CDN: {}", e); - } - } self.tx.send(Some(Utc::now())).unwrap(); } diff --git a/website/Cargo.toml b/website/Cargo.toml index 6d2701c..74b3c15 100644 --- a/website/Cargo.toml +++ b/website/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "website" -version = "0.2.3" +version = "0.2.4" edition = "2024" [dependencies] diff --git a/website/src/main.rs b/website/src/main.rs index 8d6449a..c6ddb53 100644 --- a/website/src/main.rs +++ b/website/src/main.rs @@ -281,8 +281,14 @@ async fn rocket() -> _ { loop { interval.tick().await; log::info!("Updating all DBs"); - checker_clone.read().await.update_all().await; - log::info!("Updated databases"); + match Checker::download_all().await { + Ok(bases) => { + log::info!("Downloaded, updating..."); + checker_clone.read().await.update_all(bases).await; + log::info!("Updated databases"); + }, + Err(e) => log::error!("Failed to download all DBs"), + } } });