Skip to content

Commit

Permalink
use a mutex on the refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed May 11, 2024
1 parent 3225900 commit 0a7d106
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 15 additions & 7 deletions src/content/sitemap.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -15,7 +16,7 @@ use super::{get_content, mime_type::Mime, Content};
pub struct ContentTree
{
uri_stem: String,
contents: Vec<Content>,
contents: Vec<(Content, Arc<Mutex<bool>>)>,
children: BTreeMap<String, ContentTree>
}

Expand All @@ -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()
})
Expand All @@ -57,7 +65,7 @@ impl ContentTree
{
if uri_stem == "/"
{
self.contents.push(content);
self.contents.push((content, Arc::new(Mutex::new(false))));
return;
}

Expand Down Expand Up @@ -91,7 +99,7 @@ impl ContentTree
}
None =>
{
self.contents.push(content)
self.contents.push((content, Arc::new(Mutex::new(false))))
}
}
}
Expand All @@ -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")
{
Expand Down
15 changes: 9 additions & 6 deletions tests/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 0a7d106

Please sign in to comment.