Use sync RwLock for lru access

This commit is contained in:
asonix 2022-12-09 17:47:45 -06:00
parent d834537300
commit f4db90b699
3 changed files with 7 additions and 8 deletions

View file

@ -10,8 +10,7 @@ use actix_web::web;
use lru::LruCache; use lru::LruCache;
use rand::thread_rng; use rand::thread_rng;
use rsa::{RsaPrivateKey, RsaPublicKey}; use rsa::{RsaPrivateKey, RsaPublicKey};
use std::sync::Arc; use std::sync::{Arc, RwLock};
use tokio::sync::RwLock;
#[derive(Clone)] #[derive(Clone)]
pub struct State { pub struct State {
@ -78,12 +77,12 @@ impl State {
.collect()) .collect())
} }
pub(crate) async fn is_cached(&self, object_id: &IriString) -> bool { pub(crate) fn is_cached(&self, object_id: &IriString) -> bool {
self.object_cache.read().await.contains(object_id) self.object_cache.read().unwrap().contains(object_id)
} }
pub(crate) async fn cache(&self, object_id: IriString, actor_id: IriString) { pub(crate) fn cache(&self, object_id: IriString, actor_id: IriString) {
self.object_cache.write().await.put(object_id, actor_id); self.object_cache.write().unwrap().put(object_id, actor_id);
} }
#[tracing::instrument(level = "debug", name = "Building state", skip_all)] #[tracing::instrument(level = "debug", name = "Building state", skip_all)]

View file

@ -42,7 +42,7 @@ impl Announce {
.queue(DeliverMany::new(inboxes, announce)?) .queue(DeliverMany::new(inboxes, announce)?)
.await?; .await?;
state.state.cache(self.object_id, activity_id).await; state.state.cache(self.object_id, activity_id);
Ok(()) Ok(())
} }
} }

View file

@ -203,7 +203,7 @@ async fn handle_announce(
.as_single_id() .as_single_id()
.ok_or(ErrorKind::MissingId)?; .ok_or(ErrorKind::MissingId)?;
if state.is_cached(object_id).await { if state.is_cached(object_id) {
return Err(ErrorKind::Duplicate.into()); return Err(ErrorKind::Duplicate.into());
} }