matrix-sdk: prevent dupplicated members requests

master
Julian Sparber 2021-03-12 17:02:25 +01:00
parent 31dd031269
commit 2f769726dd
2 changed files with 38 additions and 15 deletions

View File

@ -88,16 +88,15 @@ use matrix_sdk_common::{
}; };
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
use matrix_sdk_common::{ use matrix_sdk_common::api::r0::{
api::r0::{ keys::{get_keys, upload_keys, upload_signing_keys::Request as UploadSigningKeysRequest},
keys::{get_keys, upload_keys, upload_signing_keys::Request as UploadSigningKeysRequest}, to_device::send_event_to_device::{
to_device::send_event_to_device::{ Request as RumaToDeviceRequest, Response as ToDeviceResponse,
Request as RumaToDeviceRequest, Response as ToDeviceResponse,
},
}, },
locks::Mutex,
}; };
use matrix_sdk_common::locks::Mutex;
use crate::{ use crate::{
error::HttpError, error::HttpError,
http_client::{client_with_config, HttpClient, HttpSend}, http_client::{client_with_config, HttpClient, HttpSend},
@ -138,6 +137,7 @@ pub struct Client {
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
/// Lock making sure we're only doing one key claim request at a time. /// Lock making sure we're only doing one key claim request at a time.
key_claim_lock: Arc<Mutex<()>>, key_claim_lock: Arc<Mutex<()>>,
pub(crate) members_request_locks: DashMap<RoomId, Arc<Mutex<()>>>,
} }
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]
@ -393,6 +393,7 @@ impl Client {
group_session_locks: DashMap::new(), group_session_locks: DashMap::new(),
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
key_claim_lock: Arc::new(Mutex::new(())), key_claim_lock: Arc::new(Mutex::new(())),
members_request_locks: DashMap::new(),
}) })
} }

View File

@ -2,7 +2,9 @@ use matrix_sdk_common::api::r0::{
membership::{get_member_events, join_room_by_id, leave_room}, membership::{get_member_events, join_room_by_id, leave_room},
message::get_message_events, message::get_message_events,
}; };
use std::ops::Deref; use matrix_sdk_common::locks::Mutex;
use std::{ops::Deref, sync::Arc};
use crate::{Client, Result, Room, RoomMember}; use crate::{Client, Result, Room, RoomMember};
@ -96,14 +98,34 @@ impl Common {
} }
pub(crate) async fn request_members(&self) -> Result<()> { pub(crate) async fn request_members(&self) -> Result<()> {
// TODO: don't send a request if a request is being sent #[allow(clippy::map_clone)]
let request = get_member_events::Request::new(self.inner.room_id()); if let Some(mutex) = self
let response = self.client.send(request, None).await?; .client
.members_request_locks
.get(self.inner.room_id())
.map(|m| m.clone())
{
mutex.lock().await;
} else {
let mutex = Arc::new(Mutex::new(()));
self.client
.members_request_locks
.insert(self.inner.room_id().clone(), mutex.clone());
self.client let _guard = mutex.lock().await;
.base_client
.receive_members(self.inner.room_id(), &response) let request = get_member_events::Request::new(self.inner.room_id());
.await?; let response = self.client.send(request, None).await?;
self.client
.base_client
.receive_members(self.inner.room_id(), &response)
.await?;
self.client
.members_request_locks
.remove(self.inner.room_id());
}
Ok(()) Ok(())
} }