From f4db90b699465a7513906a62a4d4ab30c0d1313b Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 9 Dec 2022 17:47:45 -0600 Subject: [PATCH] Use sync RwLock for lru access --- src/data/state.rs | 11 +++++------ src/jobs/apub/announce.rs | 2 +- src/routes/inbox.rs | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/data/state.rs b/src/data/state.rs index 93a3f9a..31f0811 100644 --- a/src/data/state.rs +++ b/src/data/state.rs @@ -10,8 +10,7 @@ use actix_web::web; use lru::LruCache; use rand::thread_rng; use rsa::{RsaPrivateKey, RsaPublicKey}; -use std::sync::Arc; -use tokio::sync::RwLock; +use std::sync::{Arc, RwLock}; #[derive(Clone)] pub struct State { @@ -78,12 +77,12 @@ impl State { .collect()) } - pub(crate) async fn is_cached(&self, object_id: &IriString) -> bool { - self.object_cache.read().await.contains(object_id) + pub(crate) fn is_cached(&self, object_id: &IriString) -> bool { + self.object_cache.read().unwrap().contains(object_id) } - pub(crate) async fn cache(&self, object_id: IriString, actor_id: IriString) { - self.object_cache.write().await.put(object_id, actor_id); + pub(crate) fn cache(&self, object_id: IriString, actor_id: IriString) { + self.object_cache.write().unwrap().put(object_id, actor_id); } #[tracing::instrument(level = "debug", name = "Building state", skip_all)] diff --git a/src/jobs/apub/announce.rs b/src/jobs/apub/announce.rs index 93dec67..480f8db 100644 --- a/src/jobs/apub/announce.rs +++ b/src/jobs/apub/announce.rs @@ -42,7 +42,7 @@ impl Announce { .queue(DeliverMany::new(inboxes, announce)?) .await?; - state.state.cache(self.object_id, activity_id).await; + state.state.cache(self.object_id, activity_id); Ok(()) } } diff --git a/src/routes/inbox.rs b/src/routes/inbox.rs index 14286df..472b97d 100644 --- a/src/routes/inbox.rs +++ b/src/routes/inbox.rs @@ -203,7 +203,7 @@ async fn handle_announce( .as_single_id() .ok_or(ErrorKind::MissingId)?; - if state.is_cached(object_id).await { + if state.is_cached(object_id) { return Err(ErrorKind::Duplicate.into()); }