Skip to content

Commit 7b3eb0b

Browse files
committed
feat(base,sdk): Client now uses EventCacheStoreLock.
1 parent 8b85ff2 commit 7b3eb0b

File tree

7 files changed

+65
-27
lines changed

7 files changed

+65
-27
lines changed

crates/matrix-sdk-base/src/client.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
#[cfg(feature = "e2e-encryption")]
17+
use std::sync::Arc;
1618
use std::{
1719
collections::{BTreeMap, BTreeSet},
1820
fmt, iter,
1921
ops::Deref,
20-
sync::Arc,
2122
};
2223

2324
use eyeball::{SharedObservable, Subscriber};
@@ -71,7 +72,7 @@ use crate::RoomMemberships;
7172
use crate::{
7273
deserialized_responses::{RawAnySyncOrStrippedTimelineEvent, SyncTimelineEvent},
7374
error::{Error, Result},
74-
event_cache_store::DynEventCacheStore,
75+
event_cache_store::EventCacheStoreLock,
7576
response_processors::AccountDataProcessor,
7677
rooms::{
7778
normal::{RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons},
@@ -95,7 +96,7 @@ pub struct BaseClient {
9596
pub(crate) store: Store,
9697

9798
/// The store used by the event cache.
98-
event_cache_store: Arc<DynEventCacheStore>,
99+
event_cache_store: EventCacheStoreLock,
99100

100101
/// The store used for encryption.
101102
///
@@ -114,8 +115,7 @@ pub struct BaseClient {
114115
pub(crate) ignore_user_list_changes: SharedObservable<Vec<String>>,
115116

116117
/// A sender that is used to communicate changes to room information. Each
117-
/// event contains the room and a boolean whether this event should
118-
/// trigger a room list update.
118+
/// tick contains the room ID and the reasons that have generated this tick.
119119
pub(crate) room_info_notable_update_sender: broadcast::Sender<RoomInfoNotableUpdate>,
120120

121121
/// The strategy to use for picking recipient devices, when sending an
@@ -249,14 +249,13 @@ impl BaseClient {
249249
}
250250

251251
/// Get a reference to the store.
252-
#[allow(unknown_lints, clippy::explicit_auto_deref)]
253252
pub fn store(&self) -> &DynStateStore {
254253
self.store.deref()
255254
}
256255

257256
/// Get a reference to the event cache store.
258-
pub fn event_cache_store(&self) -> &DynEventCacheStore {
259-
self.event_cache_store.as_ref()
257+
pub fn event_cache_store(&self) -> &EventCacheStoreLock {
258+
&self.event_cache_store
260259
}
261260

262261
/// Is the client logged in.

crates/matrix-sdk-base/src/event_cache_store/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub use self::{
4040
};
4141

4242
/// The high-level public type to represent an `EventCacheStore` lock.
43+
#[derive(Clone)]
4344
pub struct EventCacheStoreLock {
4445
/// The inner cross process lock that is used to lock the `EventCacheStore`.
4546
cross_process_lock: CrossProcessStoreLock<LockableEventCacheStore>,
@@ -50,6 +51,7 @@ pub struct EventCacheStoreLock {
5051
store: Arc<DynEventCacheStore>,
5152
}
5253

54+
#[cfg(not(tarpaulin_include))]
5355
impl fmt::Debug for EventCacheStoreLock {
5456
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
5557
formatter.debug_struct("EventCacheStoreLock").finish_non_exhaustive()
@@ -94,6 +96,7 @@ pub struct EventCacheStoreLockGuard<'a> {
9496
store: &'a DynEventCacheStore,
9597
}
9698

99+
#[cfg(not(tarpaulin_include))]
97100
impl<'a> fmt::Debug for EventCacheStoreLockGuard<'a> {
98101
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
99102
formatter.debug_struct("EventCacheStoreLockGuard").finish_non_exhaustive()

crates/matrix-sdk-base/src/store/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use tokio::sync::{broadcast, Mutex, RwLock};
6060
use tracing::warn;
6161

6262
use crate::{
63-
event_cache_store::{DynEventCacheStore, IntoEventCacheStore},
63+
event_cache_store,
6464
rooms::{normal::RoomInfoNotableUpdate, RoomInfo, RoomState},
6565
MinimalRoomMemberEvent, Room, RoomStateFilter, SessionMeta,
6666
};
@@ -489,7 +489,7 @@ pub struct StoreConfig {
489489
#[cfg(feature = "e2e-encryption")]
490490
pub(crate) crypto_store: Arc<DynCryptoStore>,
491491
pub(crate) state_store: Arc<DynStateStore>,
492-
pub(crate) event_cache_store: Arc<DynEventCacheStore>,
492+
pub(crate) event_cache_store: event_cache_store::EventCacheStoreLock,
493493
}
494494

495495
#[cfg(not(tarpaulin_include))]
@@ -507,8 +507,11 @@ impl StoreConfig {
507507
#[cfg(feature = "e2e-encryption")]
508508
crypto_store: matrix_sdk_crypto::store::MemoryStore::new().into_crypto_store(),
509509
state_store: Arc::new(MemoryStore::new()),
510-
event_cache_store: crate::event_cache_store::MemoryStore::new()
511-
.into_event_cache_store(),
510+
event_cache_store: event_cache_store::EventCacheStoreLock::new(
511+
event_cache_store::MemoryStore::new(),
512+
"default-key".to_owned(),
513+
"matrix-sdk-base".to_owned(),
514+
),
512515
}
513516
}
514517

@@ -528,8 +531,12 @@ impl StoreConfig {
528531
}
529532

530533
/// Set a custom implementation of an `EventCacheStore`.
531-
pub fn event_cache_store(mut self, event_cache_store: impl IntoEventCacheStore) -> Self {
532-
self.event_cache_store = event_cache_store.into_event_cache_store();
534+
pub fn event_cache_store<S>(mut self, event_cache_store: S, key: String, holder: String) -> Self
535+
where
536+
S: event_cache_store::IntoEventCacheStore,
537+
{
538+
self.event_cache_store =
539+
event_cache_store::EventCacheStoreLock::new(event_cache_store, key, holder);
533540
self
534541
}
535542
}

crates/matrix-sdk/src/client/builder/mod.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl ClientBuilder {
208208
path: path.as_ref().to_owned(),
209209
cache_path: None,
210210
passphrase: passphrase.map(ToOwned::to_owned),
211+
event_cache_store_lock_holder: "matrix-sdk".to_owned(),
211212
};
212213
self
213214
}
@@ -225,6 +226,7 @@ impl ClientBuilder {
225226
path: path.as_ref().to_owned(),
226227
cache_path: Some(cache_path.as_ref().to_owned()),
227228
passphrase: passphrase.map(ToOwned::to_owned),
229+
event_cache_store_lock_holder: "matrix-sdk".to_owned(),
228230
};
229231
self
230232
}
@@ -235,6 +237,7 @@ impl ClientBuilder {
235237
self.store_config = BuilderStoreConfig::IndexedDb {
236238
name: name.to_owned(),
237239
passphrase: passphrase.map(ToOwned::to_owned),
240+
event_cache_store_lock_holder: "matrix-sdk".to_owned(),
238241
};
239242
self
240243
}
@@ -551,7 +554,12 @@ async fn build_store_config(
551554
#[allow(clippy::infallible_destructuring_match)]
552555
let store_config = match builder_config {
553556
#[cfg(feature = "sqlite")]
554-
BuilderStoreConfig::Sqlite { path, cache_path, passphrase } => {
557+
BuilderStoreConfig::Sqlite {
558+
path,
559+
cache_path,
560+
passphrase,
561+
event_cache_store_lock_holder,
562+
} => {
555563
let store_config = StoreConfig::new()
556564
.state_store(
557565
matrix_sdk_sqlite::SqliteStateStore::open(&path, passphrase.as_deref()).await?,
@@ -562,6 +570,8 @@ async fn build_store_config(
562570
passphrase.as_deref(),
563571
)
564572
.await?,
573+
"default-key".to_owned(),
574+
event_cache_store_lock_holder,
565575
);
566576

567577
#[cfg(feature = "e2e-encryption")]
@@ -573,8 +583,13 @@ async fn build_store_config(
573583
}
574584

575585
#[cfg(feature = "indexeddb")]
576-
BuilderStoreConfig::IndexedDb { name, passphrase } => {
577-
build_indexeddb_store_config(&name, passphrase.as_deref()).await?
586+
BuilderStoreConfig::IndexedDb { name, passphrase, event_cache_store_lock_holder } => {
587+
build_indexeddb_store_config(
588+
&name,
589+
passphrase.as_deref(),
590+
event_cache_store_lock_holder,
591+
)
592+
.await?
578593
}
579594

580595
BuilderStoreConfig::Custom(config) => config,
@@ -588,6 +603,7 @@ async fn build_store_config(
588603
async fn build_indexeddb_store_config(
589604
name: &str,
590605
passphrase: Option<&str>,
606+
event_cache_store_lock_holder: String,
591607
) -> Result<StoreConfig, ClientBuildError> {
592608
#[cfg(feature = "e2e-encryption")]
593609
let store_config = {
@@ -604,7 +620,11 @@ async fn build_indexeddb_store_config(
604620

605621
let store_config = {
606622
tracing::warn!("The IndexedDB backend does not implement an event cache store, falling back to the in-memory event cache store…");
607-
store_config.event_cache_store(matrix_sdk_base::event_cache_store::MemoryStore::new())
623+
store_config.event_cache_store(
624+
matrix_sdk_base::event_cache_store::MemoryStore::new(),
625+
"default-key".to_owned(),
626+
event_cache_store_lock_holder,
627+
)
608628
};
609629

610630
Ok(store_config)
@@ -614,6 +634,7 @@ async fn build_indexeddb_store_config(
614634
async fn build_indexeddb_store_config(
615635
_name: &str,
616636
_passphrase: Option<&str>,
637+
_event_cache_store_lock_holder: String,
617638
) -> Result<StoreConfig, ClientBuildError> {
618639
panic!("the IndexedDB is only available on the 'wasm32' arch")
619640
}
@@ -658,11 +679,13 @@ enum BuilderStoreConfig {
658679
path: std::path::PathBuf,
659680
cache_path: Option<std::path::PathBuf>,
660681
passphrase: Option<String>,
682+
event_cache_store_lock_holder: String,
661683
},
662684
#[cfg(feature = "indexeddb")]
663685
IndexedDb {
664686
name: String,
665687
passphrase: Option<String>,
688+
event_cache_store_lock_holder: String,
666689
},
667690
Custom(StoreConfig),
668691
}

crates/matrix-sdk/src/client/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use imbl::Vector;
3333
#[cfg(feature = "e2e-encryption")]
3434
use matrix_sdk_base::crypto::store::LockableCryptoStore;
3535
use matrix_sdk_base::{
36-
event_cache_store::DynEventCacheStore,
36+
event_cache_store::EventCacheStoreLock,
3737
store::{DynStateStore, ServerCapabilities},
3838
sync::{Notification, RoomUpdates},
3939
BaseClient, RoomInfoNotableUpdate, RoomState, RoomStateFilter, SendOutsideWasm, SessionMeta,
@@ -590,7 +590,7 @@ impl Client {
590590
}
591591

592592
/// Get a reference to the event cache store.
593-
pub(crate) fn event_cache_store(&self) -> &DynEventCacheStore {
593+
pub(crate) fn event_cache_store(&self) -> &EventCacheStoreLock {
594594
self.base_client().event_cache_store()
595595
}
596596

crates/matrix-sdk/src/media.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl Media {
377377
// Read from the cache.
378378
if use_cache {
379379
if let Some(content) =
380-
self.client.event_cache_store().get_media_content(request).await?
380+
self.client.event_cache_store().lock().await?.get_media_content(request).await?
381381
{
382382
return Ok(content);
383383
}
@@ -477,7 +477,12 @@ impl Media {
477477
};
478478

479479
if use_cache {
480-
self.client.event_cache_store().add_media_content(request, content.clone()).await?;
480+
self.client
481+
.event_cache_store()
482+
.lock()
483+
.await?
484+
.add_media_content(request, content.clone())
485+
.await?;
481486
}
482487

483488
Ok(content)
@@ -489,7 +494,7 @@ impl Media {
489494
///
490495
/// * `request` - The `MediaRequest` of the content.
491496
pub async fn remove_media_content(&self, request: &MediaRequest) -> Result<()> {
492-
Ok(self.client.event_cache_store().remove_media_content(request).await?)
497+
Ok(self.client.event_cache_store().lock().await?.remove_media_content(request).await?)
493498
}
494499

495500
/// Delete all the media content corresponding to the given
@@ -499,7 +504,7 @@ impl Media {
499504
///
500505
/// * `uri` - The `MxcUri` of the files.
501506
pub async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<()> {
502-
Ok(self.client.event_cache_store().remove_media_content_for_uri(uri).await?)
507+
Ok(self.client.event_cache_store().lock().await?.remove_media_content_for_uri(uri).await?)
503508
}
504509

505510
/// Get the file of the given media event content.

crates/matrix-sdk/src/room/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,14 +1988,15 @@ impl Room {
19881988
.await?;
19891989

19901990
if store_in_cache {
1991-
let cache_store = self.client.event_cache_store();
1991+
let cache_store_lock_guard = self.client.event_cache_store().lock().await?;
19921992

19931993
// A failure to cache shouldn't prevent the whole upload from finishing
19941994
// properly, so only log errors during caching.
19951995

19961996
debug!("caching the media");
19971997
let request = MediaRequest { source: media_source.clone(), format: MediaFormat::File };
1998-
if let Err(err) = cache_store.add_media_content(&request, data).await {
1998+
1999+
if let Err(err) = cache_store_lock_guard.add_media_content(&request, data).await {
19992000
warn!("unable to cache the media after uploading it: {err}");
20002001
}
20012002

@@ -2018,7 +2019,7 @@ impl Room {
20182019
}),
20192020
};
20202021

2021-
if let Err(err) = cache_store.add_media_content(&request, data).await {
2022+
if let Err(err) = cache_store_lock_guard.add_media_content(&request, data).await {
20222023
warn!("unable to cache the media after uploading it: {err}");
20232024
}
20242025
}

0 commit comments

Comments
 (0)