Skip to content

Commit

Permalink
feat(sdk): Add Client::cross_proces_store_locks_holder_name().
Browse files Browse the repository at this point in the history
This patch adds `ClientInner::cross_process_store_locks_holder_name` and
its public method `Client::cross_process_store_locks_holder_name`. This
patch also adds `ClientBuilder::cross_process_store_locks_holider_name`
to configure this value.
  • Loading branch information
Hywan committed Nov 6, 2024
1 parent 0942dab commit a0240e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/matrix-sdk/src/client/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct ClientBuilder {
room_key_recipient_strategy: CollectStrategy,
#[cfg(feature = "e2e-encryption")]
decryption_trust_requirement: TrustRequirement,
cross_process_store_locks_holder_name: String,
}

impl ClientBuilder {
Expand All @@ -122,6 +123,7 @@ impl ClientBuilder {
room_key_recipient_strategy: Default::default(),
#[cfg(feature = "e2e-encryption")]
decryption_trust_requirement: TrustRequirement::Untrusted,
cross_process_store_locks_holder_name: "main".to_owned(),
}
}

Expand Down Expand Up @@ -424,6 +426,20 @@ impl ClientBuilder {
self
}

/// Set the cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// `holder_name` will be the value used for all cross-process store locks
/// used by the `Client` being built.
///
/// If 2 concurrent `Client`s are running in 2 different process, this
/// method must be called with different `hold_name` values.
pub fn cross_process_store_locks_holder_name(mut self, holder_name: String) -> Self {
self.cross_process_store_locks_holder_name = holder_name;
self
}

/// Create a [`Client`] with the options set on this builder.
///
/// # Errors
Expand Down Expand Up @@ -529,6 +545,7 @@ impl ClientBuilder {
send_queue,
#[cfg(feature = "e2e-encryption")]
self.encryption_settings,
self.cross_process_store_locks_holder_name,
)
.await;

Expand Down Expand Up @@ -1129,4 +1146,27 @@ pub(crate) mod tests {
object
})
}

#[async_test]
async fn test_cross_process_lock_stores_holder_name() {
{
let homeserver = make_mock_homeserver().await;
let client =
ClientBuilder::new().homeserver_url(homeserver.uri()).build().await.unwrap();

assert_eq!(client.cross_process_lock_stores_holder_name(), "main");
}

{
let homeserver = make_mock_homeserver().await;
let client = ClientBuilder::new()
.homeserver_url(homeserver.uri())
.cross_process_store_locks_holder_name("foo".to_owned())
.build()
.await
.unwrap();

assert_eq!(client.cross_process_lock_stores_holder_name(), "foo");
}
}
}
24 changes: 24 additions & 0 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ pub(crate) struct ClientInner {
/// deduplicate multiple calls to a method.
pub(crate) locks: ClientLocks,

/// The cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// `holder_name` is the value used for all cross-process store locks
/// used by this `Client`.
///
/// If multiple `Client`s are running in different processes, this
/// value MUST be different for each `Client`.
cross_process_store_locks_holder_name: String,

/// A mapping of the times at which the current user sent typing notices,
/// keyed by room.
pub(crate) typing_notice_times: StdRwLock<BTreeMap<OwnedRoomId, Instant>>,
Expand Down Expand Up @@ -340,6 +351,7 @@ impl ClientInner {
event_cache: OnceCell<EventCache>,
send_queue: Arc<SendQueueData>,
#[cfg(feature = "e2e-encryption")] encryption_settings: EncryptionSettings,
cross_process_store_locks_holder_name: String,
) -> Arc<Self> {
let client = Self {
server,
Expand All @@ -350,6 +362,7 @@ impl ClientInner {
http_client,
base_client,
locks: Default::default(),
cross_process_store_locks_holder_name,
server_capabilities: RwLock::new(server_capabilities),
typing_notice_times: Default::default(),
event_handlers: Default::default(),
Expand Down Expand Up @@ -424,6 +437,16 @@ impl Client {
&self.inner.locks
}

/// The cross-process store locks holder name.
///
/// The SDK provides cross-process store locks (see
/// [`matrix_sdk_common::store_locks::CrossProcessStoreLock`]). The
/// `holder_name` is the value used for all cross-process store locks
/// used by this `Client`.
pub fn cross_process_lock_stores_holder_name(&self) -> &str {
&self.inner.cross_process_store_locks_holder_name
}

/// Change the homeserver URL used by this client.
///
/// # Arguments
Expand Down Expand Up @@ -2217,6 +2240,7 @@ impl Client {
self.inner.send_queue_data.clone(),
#[cfg(feature = "e2e-encryption")]
self.inner.e2ee.encryption_settings,
self.inner.cross_process_store_locks_holder_name.clone(),
)
.await,
};
Expand Down

0 comments on commit a0240e0

Please sign in to comment.