From f83cfc33b41a7c86c656af931258d9d3de09f940 Mon Sep 17 00:00:00 2001 From: harryob <55142896+harryob@users.noreply.github.com> Date: Mon, 4 Nov 2024 07:41:57 +0000 Subject: [PATCH] handle poisoning properly --- src/byond.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/byond.rs b/src/byond.rs index ce83b32..3d8d139 100644 --- a/src/byond.rs +++ b/src/byond.rs @@ -1,6 +1,6 @@ use sqlx::{prelude::FromRow, Error}; -use std::net::ToSocketAddrs; use std::sync::Mutex; +use std::{net::ToSocketAddrs, sync::MutexGuard}; use chrono::{DateTime, Duration, Utc}; use http2byond::ByondTopicValue; @@ -59,19 +59,19 @@ pub async fn round( config: &State, ) -> Option> { { - 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 mutexed: MutexGuard<'_, Option>> = match cache.cache_time.lock() { + Ok(real) => real, + Err(poisoned) => poisoned.into_inner(), }; + + if mutexed.is_some() { + let cache_time = mutexed.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())); + } + } } let topic_config_option = config.topic.clone();