diff --git a/src/content/mod.rs b/src/content/mod.rs index 13d1aca..3adde64 100644 --- a/src/content/mod.rs +++ b/src/content/mod.rs @@ -136,7 +136,7 @@ impl Content { match self.last_refreshed.elapsed() { - Ok(duration) => {duration.as_secs() > self.browser_cache_period_seconds as u64}, + Ok(duration) => {duration.as_secs() > self.server_cache_period_seconds as u64}, Err(e) => { crate::debug(format!("Time error checking cache is expired {}", e), None); diff --git a/src/content/sitemap.rs b/src/content/sitemap.rs index bcc37f0..6e163fd 100644 --- a/src/content/sitemap.rs +++ b/src/content/sitemap.rs @@ -1,5 +1,6 @@ -use std::{collections::BTreeMap, time::{Duration, Instant, SystemTime}, vec}; +use std::{collections::BTreeMap, sync::Arc, time::{Duration, Instant, SystemTime}, vec}; +use tokio::sync::Mutex; use axum::{response::IntoResponse, routing::get, Router}; use chrono::{DateTime, Datelike, Utc}; @@ -15,7 +16,7 @@ use super::{get_content, mime_type::Mime, Content}; pub struct ContentTree { uri_stem: String, - contents: Vec, + contents: Vec<(Content, Arc>)>, children: BTreeMap } @@ -29,16 +30,23 @@ impl ContentTree fn route(&self, static_router: bool) -> Router { let mut router: Router<(), axum::body::Body> = Router::new(); - for mut content in self.contents.clone() + for (mut content, mutex) in self.contents.clone() { router = router.route ( &content.get_uri(), get(move || async move { + // check if we should attempt a lock if !static_router && content.server_cache_expired() && content.is_stale() { - content.refresh() + let _ = mutex.lock().await; + if content.server_cache_expired() && content.is_stale() + { + // got the lock, and still stale + content.refresh(); + crate::debug(format!("Refresh called on Content {}", content.get_uri()), None); + } } content.into_response() }) @@ -57,7 +65,7 @@ impl ContentTree { if uri_stem == "/" { - self.contents.push(content); + self.contents.push((content, Arc::new(Mutex::new(false)))); return; } @@ -91,7 +99,7 @@ impl ContentTree } None => { - self.contents.push(content) + self.contents.push((content, Arc::new(Mutex::new(false)))) } } } @@ -113,7 +121,7 @@ impl ContentTree .write_inner_content::<_, Error> (|writer| { - for content in &self.contents + for (content, _) in &self.contents { if content.get_uri().contains("sitemap.xml") { diff --git a/tests/config.json b/tests/config.json index 64cdd1c..1b1256a 100644 --- a/tests/config.json +++ b/tests/config.json @@ -13,18 +13,21 @@ "path": "stats", "hit_cooloff_seconds": 60, "digest_period_seconds": 86400, - "log_files_clear_period_seconds": 2419200 + "log_files_clear_period_seconds": 2419200, + "ignore_regexes": ["/favicon.ico"] }, "content": { - "path": "tests/pages", + "path": "/home/jerboa/Website/", "home": "/home/jerboa/Website/jerboa.html", "allow_without_extension": true, - "cache_period_seconds": 3600 + "browser_cache_period_seconds": 3600, + "server_cache_period_seconds": 1, + "ignore_regexes": ["/.git", "workspace"] }, - "domain": "jerboa.app", - "api_token": "a_secret_token", - "notification_endpoint": { "addr": "https://discord.com/api/webhooks/xxx/yyy" }, + "domain": "127.0.0.1", + "api_token": "some_secure_secret_token", + "notification_endpoint": { "addr": "https://discord.com/api/webhooks/abc/xyz" }, "cert_path": "certs/cert.pem", "key_path": "certs/key.pem" }