mirror of
https://git.alemi.dev/fedicharter.git
synced 2024-11-22 16:34:49 +01:00
fix: only lock cache store, use rwlock
a mutex on the whole cache made it so incredibly slow
This commit is contained in:
parent
316c03af08
commit
eb32729b15
2 changed files with 7 additions and 7 deletions
12
src/cache.rs
12
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<Mutex<InstanceCache>> = Arc::new(Mutex::new(InstanceCache::default()));
|
||||
pub static ref CACHE : Arc<InstanceCache> = Arc::new(InstanceCache::default());
|
||||
}
|
||||
|
||||
const MAX_CACHE_AGE : i64 = 86400;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct InstanceCache {
|
||||
store: HashMap<String, (i64, serde_json::Value)>,
|
||||
store: RwLock<HashMap<String, (i64, serde_json::Value)>>,
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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::<serde_json::Value>()
|
||||
.await?;
|
||||
|
||||
self.store.insert(domain.to_string(), (now, value.clone()));
|
||||
self.store.write().await.insert(domain.to_string(), (now, value.clone()));
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ async fn scan_instance(domain: &str, map: Arc<Mutex<Map>>) -> 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);
|
||||
|
|
Loading…
Reference in a new issue