mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 07:41:23 +01:00
move auth_chain_cache to service
This commit is contained in:
parent
47502d1f36
commit
095ee483ac
5 changed files with 65 additions and 52 deletions
|
|
@ -1,28 +1,67 @@
|
|||
use std::{
|
||||
collections::{BTreeSet, HashSet},
|
||||
sync::Arc,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use lru_cache::LruCache;
|
||||
use ruma::{api::client::error::ErrorKind, EventId, RoomId};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use super::short::ShortEventId;
|
||||
use crate::{services, utils::debug_slice_truncated, Error, Result};
|
||||
use crate::{
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
services,
|
||||
utils::debug_slice_truncated,
|
||||
Error, Result,
|
||||
};
|
||||
|
||||
mod data;
|
||||
|
||||
pub(crate) use data::Data;
|
||||
|
||||
pub(crate) struct Service {
|
||||
pub(crate) db: &'static dyn Data,
|
||||
db: &'static dyn Data,
|
||||
auth_chain_cache:
|
||||
Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub(crate) fn new(
|
||||
db: &'static dyn Data,
|
||||
auth_chain_cache_size: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
auth_chain_cache: Mutex::new(LruCache::new(auth_chain_cache_size)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_cached_eventid_authchain(
|
||||
&self,
|
||||
key: &[ShortEventId],
|
||||
) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
|
||||
self.db.get_cached_eventid_authchain(key)
|
||||
let lookup = Lookup::AuthChain;
|
||||
|
||||
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key)
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(Arc::clone(result)));
|
||||
}
|
||||
|
||||
let Some(chain) = self.db.get_cached_eventid_authchain(key)? else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
let chain = Arc::new(chain);
|
||||
|
||||
self.auth_chain_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(vec![key[0]], Arc::clone(&chain));
|
||||
|
||||
Ok(Some(chain))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
|
|
@ -31,7 +70,9 @@ impl Service {
|
|||
key: Vec<ShortEventId>,
|
||||
auth_chain: Arc<HashSet<ShortEventId>>,
|
||||
) -> Result<()> {
|
||||
self.db.cache_auth_chain(key, auth_chain)
|
||||
self.db.cache_auth_chain(&key, &auth_chain)?;
|
||||
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue