fix: only lock cache store, use rwlock

a mutex on the whole cache made it so incredibly slow
This commit is contained in:
əlemi 2023-10-02 14:31:08 +02:00
parent 316c03af08
commit eb32729b15
2 changed files with 7 additions and 7 deletions

View file

@ -1,24 +1,24 @@
use std::{sync::Arc, collections::HashMap, time::Duration}; use std::{sync::Arc, collections::HashMap, time::Duration};
use chrono::Utc; use chrono::Utc;
use tokio::sync::Mutex; use tokio::sync::RwLock;
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref CACHE : Arc<Mutex<InstanceCache>> = Arc::new(Mutex::new(InstanceCache::default())); pub static ref CACHE : Arc<InstanceCache> = Arc::new(InstanceCache::default());
} }
const MAX_CACHE_AGE : i64 = 86400; const MAX_CACHE_AGE : i64 = 86400;
#[derive(Default)] #[derive(Default)]
pub struct InstanceCache { pub struct InstanceCache {
store: HashMap<String, (i64, serde_json::Value)>, store: RwLock<HashMap<String, (i64, serde_json::Value)>>,
} }
impl InstanceCache { impl InstanceCache {
pub async fn instance_metadata(&mut self, domain: &str) -> reqwest::Result<serde_json::Value> { pub async fn instance_metadata(&self, domain: &str) -> reqwest::Result<serde_json::Value> {
let now = Utc::now().timestamp(); 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 { if now - age < MAX_CACHE_AGE {
return Ok(value.clone()); return Ok(value.clone());
} }
@ -33,7 +33,7 @@ impl InstanceCache {
.json::<serde_json::Value>() .json::<serde_json::Value>()
.await?; .await?;
self.store.insert(domain.to_string(), (now, value.clone())); self.store.write().await.insert(domain.to_string(), (now, value.clone()));
Ok(value) Ok(value)
} }

View file

@ -67,7 +67,7 @@ async fn scan_instance(domain: &str, map: Arc<Mutex<Map>>) -> Option<()> {
if map.lock().await.already_scanned(domain) { return None }; if map.lock().await.already_scanned(domain) { return None };
tracing::debug!("scanning instance {}", domain); 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, Ok(r) => r,
Err(e) => { Err(e) => {
tracing::warn!("could not fetch metadata for {}: {}", domain, e); tracing::warn!("could not fetch metadata for {}: {}", domain, e);