mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-09-11 21:13:02 +02:00
refactor: Replace remaining std Mutexes
This commit is contained in:
parent
a1d616e3e3
commit
374fb2745c
11 changed files with 83 additions and 117 deletions
|
@ -1,9 +1,6 @@
|
|||
use std::{
|
||||
mem::size_of,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::{mem::size_of, sync::Arc};
|
||||
|
||||
use conduwuit::{Err, Result, err, utils, utils::math::usize_from_f64};
|
||||
use conduwuit::{Err, Result, SyncMutex, err, utils, utils::math::usize_from_f64};
|
||||
use database::Map;
|
||||
use lru_cache::LruCache;
|
||||
|
||||
|
@ -11,7 +8,7 @@ use crate::rooms::short::ShortEventId;
|
|||
|
||||
pub(super) struct Data {
|
||||
shorteventid_authchain: Arc<Map>,
|
||||
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<[ShortEventId]>>>,
|
||||
pub(super) auth_chain_cache: SyncMutex<LruCache<Vec<u64>, Arc<[ShortEventId]>>>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
|
@ -23,7 +20,7 @@ impl Data {
|
|||
.expect("valid cache size");
|
||||
Self {
|
||||
shorteventid_authchain: db["shorteventid_authchain"].clone(),
|
||||
auth_chain_cache: Mutex::new(LruCache::new(cache_size)),
|
||||
auth_chain_cache: SyncMutex::new(LruCache::new(cache_size)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,12 +31,7 @@ impl Data {
|
|||
debug_assert!(!key.is_empty(), "auth_chain key must not be empty");
|
||||
|
||||
// Check RAM cache
|
||||
if let Some(result) = self
|
||||
.auth_chain_cache
|
||||
.lock()
|
||||
.expect("cache locked")
|
||||
.get_mut(key)
|
||||
{
|
||||
if let Some(result) = self.auth_chain_cache.lock().get_mut(key) {
|
||||
return Ok(Arc::clone(result));
|
||||
}
|
||||
|
||||
|
@ -63,7 +55,6 @@ impl Data {
|
|||
// Cache in RAM
|
||||
self.auth_chain_cache
|
||||
.lock()
|
||||
.expect("cache locked")
|
||||
.insert(vec![key[0]], Arc::clone(&chain));
|
||||
|
||||
Ok(chain)
|
||||
|
@ -84,9 +75,6 @@ impl Data {
|
|||
}
|
||||
|
||||
// Cache in RAM
|
||||
self.auth_chain_cache
|
||||
.lock()
|
||||
.expect("cache locked")
|
||||
.insert(key, auth_chain);
|
||||
self.auth_chain_cache.lock().insert(key, auth_chain);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -248,10 +248,10 @@ pub fn cache_auth_chain_vec(&self, key: Vec<u64>, auth_chain: &[ShortEventId]) {
|
|||
|
||||
#[implement(Service)]
|
||||
pub fn get_cache_usage(&self) -> (usize, usize) {
|
||||
let cache = self.db.auth_chain_cache.lock().expect("locked");
|
||||
let cache = self.db.auth_chain_cache.lock();
|
||||
|
||||
(cache.len(), cache.capacity())
|
||||
}
|
||||
|
||||
#[implement(Service)]
|
||||
pub fn clear_cache(&self) { self.db.auth_chain_cache.lock().expect("locked").clear(); }
|
||||
pub fn clear_cache(&self) { self.db.auth_chain_cache.lock().clear(); }
|
||||
|
|
|
@ -2,12 +2,12 @@ use std::{
|
|||
collections::{BTreeSet, HashMap},
|
||||
fmt::{Debug, Write},
|
||||
mem::size_of,
|
||||
sync::{Arc, Mutex},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use conduwuit::{
|
||||
Result,
|
||||
Result, SyncMutex,
|
||||
arrayvec::ArrayVec,
|
||||
at, checked, err, expected, implement, utils,
|
||||
utils::{bytes, math::usize_from_f64, stream::IterStream},
|
||||
|
@ -23,7 +23,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct Service {
|
||||
pub stateinfo_cache: Mutex<StateInfoLruCache>,
|
||||
pub stateinfo_cache: SyncMutex<StateInfoLruCache>,
|
||||
db: Data,
|
||||
services: Services,
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ impl crate::Service for Service {
|
|||
|
||||
async fn memory_usage(&self, out: &mut (dyn Write + Send)) -> Result {
|
||||
let (cache_len, ents) = {
|
||||
let cache = self.stateinfo_cache.lock().expect("locked");
|
||||
let cache = self.stateinfo_cache.lock();
|
||||
let ents = cache.iter().map(at!(1)).flat_map(|vec| vec.iter()).fold(
|
||||
HashMap::new(),
|
||||
|mut ents, ssi| {
|
||||
|
@ -110,7 +110,7 @@ impl crate::Service for Service {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn clear_cache(&self) { self.stateinfo_cache.lock().expect("locked").clear(); }
|
||||
async fn clear_cache(&self) { self.stateinfo_cache.lock().clear(); }
|
||||
|
||||
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ pub async fn load_shortstatehash_info(
|
|||
&self,
|
||||
shortstatehash: ShortStateHash,
|
||||
) -> Result<ShortStateInfoVec> {
|
||||
if let Some(r) = self.stateinfo_cache.lock()?.get_mut(&shortstatehash) {
|
||||
if let Some(r) = self.stateinfo_cache.lock().get_mut(&shortstatehash) {
|
||||
return Ok(r.clone());
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ async fn cache_shortstatehash_info(
|
|||
shortstatehash: ShortStateHash,
|
||||
stack: ShortStateInfoVec,
|
||||
) -> Result {
|
||||
self.stateinfo_cache.lock()?.insert(shortstatehash, stack);
|
||||
self.stateinfo_cache.lock().insert(shortstatehash, stack);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue