Skip to content

Commit

Permalink
feat: Client::cross_process_store_locks_holder_name is used everywh…
Browse files Browse the repository at this point in the history
…ere.

See the Changelog Section to get the details.

Changelog: `Client::cross_process_store_locks_holder_name` is used everywhere:
 - `StoreConfig::new()` now takes a
   `cross_process_store_locks_holder_name` argument.
 - `StoreConfig` no longer implements `Default`.
 - `BaseClient::new()` has been removed.
 - `BaseClient::clone_with_in_memory_state_store()` now takes a
   `cross_process_store_locks_holder_name` argument.
 - `BaseClient` no longer implements `Default`.
 - `EventCacheStoreLock::new()` no longer takes a `key` argument.
 - `BuilderStoreConfig` no longer has
   `cross_process_store_locks_holder_name` field for `Sqlite` and
   `IndexedDb`.
  • Loading branch information
Hywan committed Nov 6, 2024
1 parent a0240e0 commit b9bd58f
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 84 deletions.
5 changes: 4 additions & 1 deletion benchmarks/benches/room_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ pub fn receive_all_members_benchmark(c: &mut Criterion) {
.block_on(sqlite_store.save_changes(&changes))
.expect("initial filling of sqlite failed");

let base_client = BaseClient::with_store_config(StoreConfig::new().state_store(sqlite_store));
let base_client = BaseClient::with_store_config(
StoreConfig::new("cross-process-store-locks-holder-name".to_owned())
.state_store(sqlite_store),
);

runtime
.block_on(base_client.set_session_meta(
Expand Down
10 changes: 8 additions & 2 deletions benchmarks/benches/store_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub fn restore_session(c: &mut Criterion) {
b.to_async(&runtime).iter(|| async {
let client = Client::builder()
.homeserver_url("https://matrix.example.com")
.store_config(StoreConfig::new().state_store(store.clone()))
.store_config(
StoreConfig::new("cross-process-store-locks-holder-name".to_owned())
.state_store(store.clone()),
)
.build()
.await
.expect("Can't build client");
Expand All @@ -96,7 +99,10 @@ pub fn restore_session(c: &mut Criterion) {
b.to_async(&runtime).iter(|| async {
let client = Client::builder()
.homeserver_url("https://matrix.example.com")
.store_config(StoreConfig::new().state_store(store.clone()))
.store_config(
StoreConfig::new("cross-process-store-locks-holder-name".to_owned())
.state_store(store.clone()),
)
.build()
.await
.expect("Can't build client");
Expand Down
44 changes: 24 additions & 20 deletions crates/matrix-sdk-base/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ impl fmt::Debug for BaseClient {
}

impl BaseClient {
/// Create a new default client.
pub fn new() -> Self {
BaseClient::with_store_config(StoreConfig::default())
}

/// Create a new client.
///
/// # Arguments
Expand Down Expand Up @@ -173,8 +168,12 @@ impl BaseClient {
/// Clones the current base client to use the same crypto store but a
/// different, in-memory store config, and resets transient state.
#[cfg(feature = "e2e-encryption")]
pub async fn clone_with_in_memory_state_store(&self) -> Result<Self> {
let config = StoreConfig::new().state_store(MemoryStore::new());
pub async fn clone_with_in_memory_state_store(
&self,
cross_process_store_locks_holder_name: &str,
) -> Result<Self> {
let config = StoreConfig::new(cross_process_store_locks_holder_name.to_owned())
.state_store(MemoryStore::new());
let config = config.crypto_store(self.crypto_store.clone());

let copy = Self {
Expand Down Expand Up @@ -207,8 +206,12 @@ impl BaseClient {
/// different, in-memory store config, and resets transient state.
#[cfg(not(feature = "e2e-encryption"))]
#[allow(clippy::unused_async)]
pub async fn clone_with_in_memory_state_store(&self) -> Result<Self> {
let config = StoreConfig::new().state_store(MemoryStore::new());
pub async fn clone_with_in_memory_state_store(
&self,
cross_process_store_locks_holder: &str,
) -> Result<Self> {
let config = StoreConfig::new(cross_process_store_locks_holder.to_owned())
.state_store(MemoryStore::new());
Ok(Self::with_store_config(config))
}

Expand Down Expand Up @@ -1689,12 +1692,6 @@ impl BaseClient {
}
}

impl Default for BaseClient {
fn default() -> Self {
Self::new()
}
}

fn handle_room_member_event_for_profiles(
room_id: &RoomId,
event: &SyncStateEvent<RoomMemberEventContent>,
Expand Down Expand Up @@ -1737,8 +1734,9 @@ mod tests {

use super::BaseClient;
use crate::{
store::StateStoreExt, test_utils::logged_in_base_client, DisplayName, RoomState,
SessionMeta,
store::{StateStoreExt, StoreConfig},
test_utils::logged_in_base_client,
DisplayName, RoomState, SessionMeta,
};

#[async_test]
Expand Down Expand Up @@ -1945,7 +1943,9 @@ mod tests {
let user_id = user_id!("@alice:example.org");
let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");

let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));
client
.set_session_meta(
SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
Expand Down Expand Up @@ -2003,7 +2003,9 @@ mod tests {
let inviter_user_id = user_id!("@bob:example.org");
let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");

let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));
client
.set_session_meta(
SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
Expand Down Expand Up @@ -2063,7 +2065,9 @@ mod tests {
let inviter_user_id = user_id!("@bob:example.org");
let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");

let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));
client
.set_session_meta(
SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
Expand Down
7 changes: 5 additions & 2 deletions crates/matrix-sdk-base/src/event_cache_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl fmt::Debug for EventCacheStoreLock {

impl EventCacheStoreLock {
/// Create a new lock around the [`EventCacheStore`].
pub fn new<S>(store: S, key: String, holder: String) -> Self
///
/// The `holder` argument represents the holder inside the
/// [`CrossProcessStoreLock::new`].
pub fn new<S>(store: S, holder: String) -> Self
where
S: IntoEventCacheStore,
{
Expand All @@ -69,7 +72,7 @@ impl EventCacheStoreLock {
Self {
cross_process_lock: CrossProcessStoreLock::new(
LockableEventCacheStore(store.clone()),
key,
"default".to_owned(),
holder,
),
store,
Expand Down
14 changes: 10 additions & 4 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ mod tests {
use crate::latest_event::LatestEvent;
use crate::{
rooms::RoomNotableTags,
store::{IntoStateStore, MemoryStore, StateChanges, StateStore},
store::{IntoStateStore, MemoryStore, StateChanges, StateStore, StoreConfig},
BaseClient, DisplayName, MinimalStateEvent, OriginalMinimalStateEvent,
RoomInfoNotableUpdateReasons, SessionMeta,
};
Expand Down Expand Up @@ -2138,7 +2138,9 @@ mod tests {
#[async_test]
async fn test_is_favourite() {
// Given a room,
let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));

client
.set_session_meta(
Expand Down Expand Up @@ -2216,7 +2218,9 @@ mod tests {
#[async_test]
async fn test_is_low_priority() {
// Given a room,
let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));

client
.set_session_meta(
Expand Down Expand Up @@ -2672,7 +2676,9 @@ mod tests {
use crate::{RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons};

// Given a room,
let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));

client
.set_session_meta(
Expand Down
30 changes: 14 additions & 16 deletions crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,16 @@ impl StateChanges {
/// ```
/// # use matrix_sdk_base::store::StoreConfig;
///
/// let store_config = StoreConfig::new();
/// let store_config =
/// StoreConfig::new("cross-process-store-locks-holder-name".to_owned());
/// ```
#[derive(Clone)]
pub struct StoreConfig {
#[cfg(feature = "e2e-encryption")]
pub(crate) crypto_store: Arc<DynCryptoStore>,
pub(crate) state_store: Arc<DynStateStore>,
pub(crate) event_cache_store: event_cache_store::EventCacheStoreLock,
cross_process_store_locks_holder_name: String,
}

#[cfg(not(tarpaulin_include))]
Expand All @@ -501,17 +503,20 @@ impl fmt::Debug for StoreConfig {

impl StoreConfig {
/// Create a new default `StoreConfig`.
///
/// To learn more about `cross_process_store_locks_holder_name`, please read
/// [`CrossProcessStoreLock::new`](matrix_sdk_common::store_locks::CrossProcessStoreLock::new).
#[must_use]
pub fn new() -> Self {
pub fn new(cross_process_store_locks_holder_name: String) -> Self {
Self {
#[cfg(feature = "e2e-encryption")]
crypto_store: matrix_sdk_crypto::store::MemoryStore::new().into_crypto_store(),
state_store: Arc::new(MemoryStore::new()),
event_cache_store: event_cache_store::EventCacheStoreLock::new(
event_cache_store::MemoryStore::new(),
"default-key".to_owned(),
"matrix-sdk-base".to_owned(),
cross_process_store_locks_holder_name.clone(),
),
cross_process_store_locks_holder_name,
}
}

Expand All @@ -531,21 +536,14 @@ impl StoreConfig {
}

/// Set a custom implementation of an `EventCacheStore`.
///
/// The `key` and `holder` arguments represent the key and holder inside the
/// [`CrossProcessStoreLock::new`][matrix_sdk_common::store_locks::CrossProcessStoreLock::new].
pub fn event_cache_store<S>(mut self, event_cache_store: S, key: String, holder: String) -> Self
pub fn event_cache_store<S>(mut self, event_cache_store: S) -> Self
where
S: event_cache_store::IntoEventCacheStore,
{
self.event_cache_store =
event_cache_store::EventCacheStoreLock::new(event_cache_store, key, holder);
self.event_cache_store = event_cache_store::EventCacheStoreLock::new(
event_cache_store,
self.cross_process_store_locks_holder_name.clone(),
);
self
}
}

impl Default for StoreConfig {
fn default() -> Self {
Self::new()
}
}
6 changes: 4 additions & 2 deletions crates/matrix-sdk-base/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

use ruma::{owned_user_id, UserId};

use crate::{BaseClient, SessionMeta};
use crate::{store::StoreConfig, BaseClient, SessionMeta};

/// Create a [`BaseClient`] with the given user id, if provided, or an hardcoded
/// one otherwise.
pub(crate) async fn logged_in_base_client(user_id: Option<&UserId>) -> BaseClient {
let client = BaseClient::new();
let client = BaseClient::with_store_config(StoreConfig::new(
"cross-process-store-locks-holder-name".to_owned(),
));
let user_id =
user_id.map(|user_id| user_id.to_owned()).unwrap_or_else(|| owned_user_id!("@u:e.uk"));
client
Expand Down
Loading

0 comments on commit b9bd58f

Please sign in to comment.