add try_lock to MutexMap; allow TryFrom constructions

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-01-22 06:40:07 +00:00
parent 49023aa295
commit 7c0c029a4a
4 changed files with 71 additions and 19 deletions

26
Cargo.lock generated
View file

@ -3188,7 +3188,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.10.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"assign",
"js_int",
@ -3210,7 +3210,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.10.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"js_int",
"ruma-common",
@ -3222,7 +3222,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.18.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"as_variant",
"assign",
@ -3245,7 +3245,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"as_variant",
"base64 0.22.1",
@ -3276,7 +3276,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.28.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"as_variant",
"indexmap 2.7.0",
@ -3301,7 +3301,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"bytes",
"http",
@ -3319,7 +3319,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.9.5"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"js_int",
"thiserror 2.0.11",
@ -3328,7 +3328,7 @@ dependencies = [
[[package]]
name = "ruma-identity-service-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"js_int",
"ruma-common",
@ -3338,7 +3338,7 @@ dependencies = [
[[package]]
name = "ruma-macros"
version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"cfg-if",
"proc-macro-crate",
@ -3353,7 +3353,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"js_int",
"ruma-common",
@ -3365,7 +3365,7 @@ dependencies = [
[[package]]
name = "ruma-server-util"
version = "0.3.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"headers",
"http",
@ -3378,7 +3378,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.15.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"base64 0.22.1",
"ed25519-dalek",
@ -3394,7 +3394,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
version = "0.11.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=c4f55b39900b33b2d443dd12a6a2dab50961fdfb#c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
source = "git+https://github.com/girlbossceo/ruwuma?rev=427877d5bc14988ed877e500bbb27f8bc08b84e8#427877d5bc14988ed877e500bbb27f8bc08b84e8"
dependencies = [
"futures-util",
"js_int",

View file

@ -332,7 +332,7 @@ version = "0.1.2"
[workspace.dependencies.ruma]
git = "https://github.com/girlbossceo/ruwuma"
#branch = "conduwuit-changes"
rev = "c4f55b39900b33b2d443dd12a6a2dab50961fdfb"
rev = "427877d5bc14988ed877e500bbb27f8bc08b84e8"
features = [
"compat",
"rand",

View file

@ -725,7 +725,7 @@ pub(super) async fn force_set_room_state_from_server(
.save_state(room_id.clone().as_ref(), new_room_state)
.await?;
let state_lock = self.services.rooms.state.mutex.lock(&room_id).await;
let state_lock = self.services.rooms.state.mutex.lock(&*room_id).await;
self.services
.rooms
.state

View file

@ -1,7 +1,13 @@
use std::{fmt::Debug, hash::Hash, sync::Arc};
use std::{
fmt::Debug,
hash::Hash,
sync::{Arc, TryLockError::WouldBlock},
};
use tokio::sync::OwnedMutexGuard as Omg;
use crate::{err, Result};
/// Map of Mutexes
pub struct MutexMap<Key, Val> {
map: Map<Key, Val>,
@ -30,16 +36,17 @@ where
}
#[tracing::instrument(level = "trace", skip(self))]
pub async fn lock<K>(&self, k: &K) -> Guard<Key, Val>
pub async fn lock<'a, K>(&'a self, k: &'a K) -> Guard<Key, Val>
where
K: Debug + Send + ?Sized + Sync,
Key: for<'a> From<&'a K>,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
{
let val = self
.map
.lock()
.expect("locked")
.entry(k.into())
.entry(k.try_into().expect("failed to construct key"))
.or_default()
.clone();
@ -49,6 +56,51 @@ where
}
}
#[tracing::instrument(level = "trace", skip(self))]
pub fn try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
where
K: Debug + Send + ?Sized + Sync,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
{
let val = self
.map
.lock()
.expect("locked")
.entry(k.try_into().expect("failed to construct key"))
.or_default()
.clone();
Ok(Guard::<Key, Val> {
map: Arc::clone(&self.map),
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
})
}
#[tracing::instrument(level = "trace", skip(self))]
pub fn try_try_lock<'a, K>(&self, k: &'a K) -> Result<Guard<Key, Val>>
where
K: Debug + Send + ?Sized + Sync,
Key: TryFrom<&'a K>,
<Key as TryFrom<&'a K>>::Error: Debug,
{
let val = self
.map
.try_lock()
.map_err(|e| match e {
| WouldBlock => err!("would block"),
| _ => panic!("{e:?}"),
})?
.entry(k.try_into().expect("failed to construct key"))
.or_default()
.clone();
Ok(Guard::<Key, Val> {
map: Arc::clone(&self.map),
val: val.try_lock_owned().map_err(|_| err!("would yield"))?,
})
}
#[must_use]
pub fn contains(&self, k: &Key) -> bool { self.map.lock().expect("locked").contains_key(k) }