From eb32729b15e957ef13a85863fb992bf505d1d80a Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 2 Oct 2023 14:31:08 +0200 Subject: [PATCH] fix: only lock cache store, use rwlock a mutex on the whole cache made it so incredibly slow --- src/cache.rs | 12 ++++++------ src/main.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 31960d5..70fc586 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,24 +1,24 @@ use std::{sync::Arc, collections::HashMap, time::Duration}; use chrono::Utc; -use tokio::sync::Mutex; +use tokio::sync::RwLock; lazy_static::lazy_static! { - pub static ref CACHE : Arc> = Arc::new(Mutex::new(InstanceCache::default())); + pub static ref CACHE : Arc = Arc::new(InstanceCache::default()); } const MAX_CACHE_AGE : i64 = 86400; #[derive(Default)] pub struct InstanceCache { - store: HashMap, + store: RwLock>, } impl InstanceCache { - pub async fn instance_metadata(&mut self, domain: &str) -> reqwest::Result { + pub async fn instance_metadata(&self, domain: &str) -> reqwest::Result { let now = Utc::now().timestamp(); - if let Some((age, value)) = self.store.get(domain) { + if let Some((age, value)) = self.store.read().await.get(domain) { if now - age < MAX_CACHE_AGE { return Ok(value.clone()); } @@ -33,7 +33,7 @@ impl InstanceCache { .json::() .await?; - self.store.insert(domain.to_string(), (now, value.clone())); + self.store.write().await.insert(domain.to_string(), (now, value.clone())); Ok(value) } diff --git a/src/main.rs b/src/main.rs index 82b09f2..c65dd83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,7 +67,7 @@ async fn scan_instance(domain: &str, map: Arc>) -> Option<()> { if map.lock().await.already_scanned(domain) { return None }; tracing::debug!("scanning instance {}", domain); - let response = match CACHE.lock().await.instance_metadata(domain).await { + let response = match CACHE.instance_metadata(domain).await { Ok(r) => r, Err(e) => { tracing::warn!("could not fetch metadata for {}: {}", domain, e);