From a8c323d01f13304bf15fdeb88c49a37180795e94 Mon Sep 17 00:00:00 2001 From: harryob <55142896+harryob@users.noreply.github.com> Date: Mon, 4 Nov 2024 07:29:31 +0000 Subject: [PATCH] tidyup cache for byond status --- src/byond.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/byond.rs b/src/byond.rs index 16c851e..ce83b32 100644 --- a/src/byond.rs +++ b/src/byond.rs @@ -58,15 +58,20 @@ pub async fn round( cache: &State, config: &State, ) -> Option> { - let mut locked = cache.cache_time.lock().unwrap(); - - if locked.is_some() { - let cache_time = locked.unwrap(); - let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60); - - if cache_time > five_minutes_ago { - return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap())); - } + { + match cache.cache_time.lock() { + Ok(real) => { + if real.is_some() { + let cache_time = real.unwrap(); + let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60); + + if cache_time > five_minutes_ago { + return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap())); + } + } + } + Err(_) => {} + }; } let topic_config_option = config.topic.clone(); @@ -113,7 +118,7 @@ pub async fn round( }; *cache.cached_status.lock().unwrap() = Some(byond_json.clone()); - *locked = Some(chrono::Utc::now()); + *cache.cache_time.lock().unwrap() = Some(chrono::Utc::now()); Some(Json(byond_json)) }