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")]
use matrix_sdk_common::{
api::r0::{
use matrix_sdk_common::api::r0::{
keys::{get_keys, upload_keys, upload_signing_keys::Request as UploadSigningKeysRequest},
to_device::send_event_to_device::{
Request as RumaToDeviceRequest, Response as ToDeviceResponse,
},
},
locks::Mutex,
};
use matrix_sdk_common::locks::Mutex;
use crate::{
error::HttpError,
http_client::{client_with_config, HttpClient, HttpSend},
@ -138,6 +137,7 @@ pub struct Client {
#[cfg(feature = "encryption")]
/// Lock making sure we're only doing one key claim request at a time.
key_claim_lock: Arc<Mutex<()>>,
pub(crate) members_request_locks: DashMap<RoomId, Arc<Mutex<()>>>,
}
#[cfg(not(tarpaulin_include))]
@ -393,6 +393,7 @@ impl Client {
group_session_locks: DashMap::new(),
#[cfg(feature = "encryption")]
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},
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};
@ -96,7 +98,22 @@ impl Common {
}
pub(crate) async fn request_members(&self) -> Result<()> {
// TODO: don't send a request if a request is being sent
#[allow(clippy::map_clone)]
if let Some(mutex) = self
.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());
let _guard = mutex.lock().await;
let request = get_member_events::Request::new(self.inner.room_id());
let response = self.client.send(request, None).await?;
@ -105,6 +122,11 @@ impl Common {
.receive_members(self.inner.room_id(), &response)
.await?;
self.client
.members_request_locks
.remove(self.inner.room_id());
}
Ok(())
}