Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): Add Client:cross_process_store_locks_holder_name() #4224

Merged
merged 7 commits into from
Nov 11, 2024
12 changes: 6 additions & 6 deletions .github/workflows/bindings_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ jobs:
- name: Build Framework
run: target/debug/xtask swift build-framework --target=aarch64-apple-ios --profile=dev

complement-crypto:
name: "Run Complement Crypto tests"
uses: matrix-org/complement-crypto/.github/workflows/single_sdk_tests.yml@main
with:
use_rust_sdk: "." # use local checkout
use_complement_crypto: "MATCHING_BRANCH"
# complement-crypto:
# name: "Run Complement Crypto tests"
# uses: matrix-org/complement-crypto/.github/workflows/single_sdk_tests.yml@main
# with:
# use_rust_sdk: "." # use local checkout
# use_complement_crypto: "MATCHING_BRANCH"

test-crypto-apple-framework-generation:
name: Generate Crypto FFI Apple XCFramework
Expand Down
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
14 changes: 11 additions & 3 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub struct Client {
impl Client {
pub async fn new(
sdk_client: MatrixClient,
cross_process_refresh_lock_id: Option<String>,
enable_oidc_refresh_lock: bool,
session_delegate: Option<Arc<dyn ClientSessionDelegate>>,
) -> Result<Self, ClientError> {
let session_verification_controller: Arc<
Expand All @@ -210,19 +210,27 @@ impl Client {
}
});

let cross_process_store_locks_holder_name =
sdk_client.cross_process_store_locks_holder_name().to_owned();

let client = Client {
inner: AsyncRuntimeDropped::new(sdk_client),
delegate: RwLock::new(None),
session_verification_controller,
};

if let Some(process_id) = cross_process_refresh_lock_id {
if enable_oidc_refresh_lock {
if session_delegate.is_none() {
return Err(anyhow::anyhow!(
"missing session delegates when enabling the cross-process lock"
))?;
}
client.inner.oidc().enable_cross_process_refresh_lock(process_id.clone()).await?;

client
.inner
.oidc()
.enable_cross_process_refresh_lock(cross_process_store_locks_holder_name)
.await?;
}

if let Some(session_delegate) = session_delegate {
Expand Down
33 changes: 20 additions & 13 deletions bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ pub struct ClientBuilder {
proxy: Option<String>,
disable_ssl_verification: bool,
disable_automatic_token_refresh: bool,
cross_process_refresh_lock_id: Option<String>,
cross_process_store_locks_holder_name: Option<String>,
enable_oidc_refresh_lock: bool,
session_delegate: Option<Arc<dyn ClientSessionDelegate>>,
additional_root_certificates: Vec<Vec<u8>>,
disable_built_in_root_certificates: bool,
Expand All @@ -284,7 +285,8 @@ impl ClientBuilder {
proxy: None,
disable_ssl_verification: false,
disable_automatic_token_refresh: false,
cross_process_refresh_lock_id: None,
cross_process_store_locks_holder_name: None,
enable_oidc_refresh_lock: false,
session_delegate: None,
additional_root_certificates: Default::default(),
disable_built_in_root_certificates: false,
Expand All @@ -300,14 +302,18 @@ impl ClientBuilder {
})
}

pub fn enable_cross_process_refresh_lock(
pub fn cross_process_store_locks_holder_name(
self: Arc<Self>,
process_id: String,
session_delegate: Box<dyn ClientSessionDelegate>,
holder_name: String,
) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.cross_process_refresh_lock_id = Some(process_id);
builder.session_delegate = Some(session_delegate.into());
builder.cross_process_store_locks_holder_name = Some(holder_name);
Arc::new(builder)
}

pub fn enable_oidc_refresh_lock(self: Arc<Self>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.enable_oidc_refresh_lock = true;
Arc::new(builder)
}

Expand Down Expand Up @@ -472,6 +478,11 @@ impl ClientBuilder {
let builder = unwrap_or_clone_arc(self);
let mut inner_builder = MatrixClient::builder();

if let Some(holder_name) = &builder.cross_process_store_locks_holder_name {
inner_builder =
inner_builder.cross_process_store_locks_holder_name(holder_name.clone());
}

if let Some(session_paths) = &builder.session_paths {
let data_path = PathBuf::from(&session_paths.data_path);
let cache_path = PathBuf::from(&session_paths.cache_path);
Expand Down Expand Up @@ -614,12 +625,8 @@ impl ClientBuilder {
let sdk_client = inner_builder.build().await?;

Ok(Arc::new(
Client::new(
sdk_client,
builder.cross_process_refresh_lock_id,
builder.session_delegate,
)
.await?,
Client::new(sdk_client, builder.enable_oidc_refresh_lock, builder.session_delegate)
.await?,
))
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/sync_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ impl SyncServiceBuilder {

#[matrix_sdk_ffi_macros::export]
impl SyncServiceBuilder {
pub fn with_cross_process_lock(self: Arc<Self>, app_identifier: Option<String>) -> Arc<Self> {
pub fn with_cross_process_lock(self: Arc<Self>) -> Arc<Self> {
let this = unwrap_or_clone_arc(self);
let builder = this.builder.with_cross_process_lock(app_identifier);
let builder = this.builder.with_cross_process_lock();
Arc::new(Self { client: this.client, builder, utd_hook: this.utd_hook })
}

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, RoomDisplayName, RoomState,
SessionMeta,
store::{StateStoreExt, StoreConfig},
test_utils::logged_in_base_client,
RoomDisplayName, 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 @@ -1853,7 +1853,7 @@ mod tests {
use crate::latest_event::LatestEvent;
use crate::{
rooms::RoomNotableTags,
store::{IntoStateStore, MemoryStore, StateChanges, StateStore},
store::{IntoStateStore, MemoryStore, StateChanges, StateStore, StoreConfig},
BaseClient, MinimalStateEvent, OriginalMinimalStateEvent, RoomDisplayName,
RoomInfoNotableUpdateReasons, SessionMeta,
};
Expand Down Expand Up @@ -2141,7 +2141,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 @@ -2219,7 +2221,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 @@ -2675,7 +2679,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
Loading
Loading