mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-24 17:37:34 +00:00
refactor(service): Use SyncRwLock and SyncMutex for in-memory services
Convert in-memory data structures in appservice, rooms::typing, and sync wakers to use SyncRwLock and SyncMutex. Drop appservice locks before making outbound network requests in room alias queries and federation invites.
This commit is contained in:
20 files changed
+126
-160
No files matched your search
@@ -20,8 +20,7 @@ pub(crate) async fn create_alias_route(
|
||||
services
|
||||
.rooms
|
||||
.alias
|
||||
.appservice_checks(&body.room_alias, body.identity.appservice_info())
|
||||
.await?;
|
||||
.appservice_checks(&body.room_alias, body.identity.appservice_info())?;
|
||||
|
||||
// this isn't apart of alias_checks or delete alias route because we should
|
||||
// allow removing forbidden room aliases
|
||||
@@ -69,8 +68,7 @@ pub(crate) async fn delete_alias_route(
|
||||
services
|
||||
.rooms
|
||||
.alias
|
||||
.appservice_checks(&body.room_alias, body.identity.appservice_info())
|
||||
.await?;
|
||||
.appservice_checks(&body.room_alias, body.identity.appservice_info())?;
|
||||
|
||||
services
|
||||
.rooms
|
||||
|
||||
@@ -758,11 +758,7 @@ async fn room_alias_check(
|
||||
if !info.aliases.is_match(full_room_alias.as_str()) {
|
||||
return Err!(Request(Exclusive("Room alias is not in namespace.")));
|
||||
}
|
||||
} else if services
|
||||
.appservice
|
||||
.is_exclusive_alias(&full_room_alias)
|
||||
.await
|
||||
{
|
||||
} else if services.appservice.is_exclusive_alias(&full_room_alias) {
|
||||
return Err!(Request(Exclusive("Room alias reserved by appservice.",)));
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ pub(crate) async fn sync_events_route(
|
||||
.update_device_last_seen(sender_user, Some(sender_device), client_ip)
|
||||
.await;
|
||||
|
||||
let mut wake_receiver = services.sync.subscribe_to_wake(sender_user).await;
|
||||
let mut wake_receiver = services.sync.subscribe_to_wake(sender_user);
|
||||
let response = build_sync_events(&services, &body).await?;
|
||||
if body.body.full_state
|
||||
|| !(response.rooms.is_empty()
|
||||
|
||||
@@ -147,7 +147,7 @@ pub(crate) async fn sync_events_v5_route(
|
||||
.sync
|
||||
.update_snake_sync_request_with_cache(&snake_key, globalsince, &mut body);
|
||||
|
||||
let mut wake_receiver = services.sync.subscribe_to_wake(sender_user).await;
|
||||
let mut wake_receiver = services.sync.subscribe_to_wake(sender_user);
|
||||
let mut collection = collect_sync_response(
|
||||
services,
|
||||
sender_user,
|
||||
|
||||
@@ -64,7 +64,7 @@ pub(crate) async fn continuwuity_local_user_count(
|
||||
.stream_local_users()
|
||||
.filter_map(async |user_id| {
|
||||
if !services.users.status(&user_id).await.is_active()
|
||||
|| services.appservice.is_exclusive_user_id(&user_id).await
|
||||
|| services.appservice.is_exclusive_user_id(&user_id)
|
||||
{
|
||||
None
|
||||
} else {
|
||||
|
||||
@@ -346,7 +346,7 @@ async fn check_access_token(
|
||||
}
|
||||
|
||||
Ok(ClientIdentity::User { sender_user, sender_device })
|
||||
} else if let Ok(appservice_info) = services.appservice.find_from_token(token).await {
|
||||
} else if let Ok(appservice_info) = services.appservice.find_from_token(token) {
|
||||
let Ok(sender_user) = query.user_id.clone().map_or_else(
|
||||
|| {
|
||||
UserId::parse_with_server_name(
|
||||
@@ -405,7 +405,7 @@ async fn check_access_token(
|
||||
}
|
||||
|
||||
async fn check_appservice_token(services: &Services, token: &str) -> Result<RegistrationInfo> {
|
||||
let Ok(appservice_info) = services.appservice.find_from_token(token).await else {
|
||||
let Ok(appservice_info) = services.appservice.find_from_token(token) else {
|
||||
return Err!(Request(Unauthorized("Invalid appservice token.")));
|
||||
};
|
||||
|
||||
|
||||
+27
-23
@@ -209,31 +209,35 @@ async fn create_invite(
|
||||
|
||||
services.sync.wake(&recipient_user).await;
|
||||
|
||||
for appservice in services.appservice.read().await.values() {
|
||||
if appservice.is_user_match(&recipient_user) {
|
||||
let transaction_id = general_purpose::URL_SAFE_NO_PAD
|
||||
.encode(sha256::hash(pdu.event_id.as_bytes()))
|
||||
.into();
|
||||
let matching_appservices: Vec<_> = services
|
||||
.appservice
|
||||
.read()
|
||||
.values()
|
||||
.filter(|appservice| appservice.is_user_match(&recipient_user))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let request = ruma::api::appservice::event::push_events::v1::Request::new(
|
||||
transaction_id,
|
||||
vec![pdu.to_format()],
|
||||
);
|
||||
for appservice in matching_appservices {
|
||||
let transaction_id = general_purpose::URL_SAFE_NO_PAD
|
||||
.encode(sha256::hash(pdu.event_id.as_bytes()))
|
||||
.into();
|
||||
|
||||
services
|
||||
.sending
|
||||
.send_appservice_request(appservice.registration.clone(), request)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!(
|
||||
"failed to notify appservice {} about incoming invite: {e}",
|
||||
appservice.registration.id
|
||||
);
|
||||
err!(BadServerResponse(
|
||||
"Failed to notify appservice about incoming invite."
|
||||
))
|
||||
})?;
|
||||
}
|
||||
let request = ruma::api::appservice::event::push_events::v1::Request::new(
|
||||
transaction_id,
|
||||
vec![pdu.to_format()],
|
||||
);
|
||||
|
||||
services
|
||||
.sending
|
||||
.send_appservice_request(appservice.registration.clone(), request)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!(
|
||||
"failed to notify appservice {} about incoming invite: {e}",
|
||||
appservice.registration.id
|
||||
);
|
||||
err!(BadServerResponse("Failed to notify appservice about incoming invite."))
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user