From 9bcc50fe2f2d6df00f1b23cade094c4253fa7202 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 12 Jun 2023 23:14:58 +0200 Subject: [PATCH] feat(sdk): `Client::sliding_sync_proxy` is a `StdRwLock`. This patch changes `matrix_sdk::Client::sliding_sync_proxy` to be a `std::sync::RwLock>` instead of `tokio::sync::RwLock<_>`. It means that all methods reading or writing this field are sync instead of async, which makes the code a lot more easier. Having an async-aware lock wasn't necessary here. --- bindings/matrix-sdk-ffi/src/client.rs | 6 ++---- bindings/matrix-sdk-ffi/src/client_builder.rs | 2 +- bindings/matrix-sdk-ffi/src/sliding_sync.rs | 4 +--- crates/matrix-sdk-ui/src/room_list/mod.rs | 2 +- crates/matrix-sdk/src/client/builder.rs | 7 +++++-- crates/matrix-sdk/src/client/mod.rs | 12 ++++++------ 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 23231ee78..518abd1d0 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -240,7 +240,7 @@ impl Client { let sliding_sync_proxy = Url::parse(&sliding_sync_proxy) .map_err(|error| ClientError::Generic { msg: error.to_string() })?; - RUNTIME.block_on(async { self.inner.set_sliding_sync_proxy(sliding_sync_proxy).await }); + self.inner.set_sliding_sync_proxy(sliding_sync_proxy); } Ok(()) @@ -269,9 +269,7 @@ impl Client { /// The sliding sync proxy that is trusted by the homeserver. `None` when /// not configured. pub fn discovered_sliding_sync_proxy(&self) -> Option { - RUNTIME.block_on(async move { - self.inner.sliding_sync_proxy().await.map(|server| server.to_string()) - }) + self.inner.sliding_sync_proxy().map(|server| server.to_string()) } pub(crate) fn set_sliding_sync_proxy(&self, sliding_sync_proxy: Option) { diff --git a/bindings/matrix-sdk-ffi/src/client_builder.rs b/bindings/matrix-sdk-ffi/src/client_builder.rs index 44979a419..5d332af71 100644 --- a/bindings/matrix-sdk-ffi/src/client_builder.rs +++ b/bindings/matrix-sdk-ffi/src/client_builder.rs @@ -133,7 +133,7 @@ impl ClientBuilder { let sdk_client = inner_builder.build().await?; if let Some(sliding_sync_proxy) = &builder.sliding_sync_proxy { - sdk_client.set_sliding_sync_proxy(Url::parse(&sliding_sync_proxy).unwrap()).await; + sdk_client.set_sliding_sync_proxy(Url::parse(sliding_sync_proxy).unwrap()); } let client = Client::new(sdk_client); diff --git a/bindings/matrix-sdk-ffi/src/sliding_sync.rs b/bindings/matrix-sdk-ffi/src/sliding_sync.rs index 26a84399d..bc25e654b 100644 --- a/bindings/matrix-sdk-ffi/src/sliding_sync.rs +++ b/bindings/matrix-sdk-ffi/src/sliding_sync.rs @@ -867,9 +867,7 @@ impl Client { pub fn sliding_sync(&self, id: String) -> Result, ClientError> { let mut inner = self.inner.sliding_sync(id)?; - if let Some(sliding_sync_proxy) = - RUNTIME.block_on(async { self.inner.sliding_sync_proxy().await }) - { + if let Some(sliding_sync_proxy) = self.inner.sliding_sync_proxy() { inner = inner.sliding_sync_proxy(sliding_sync_proxy); } diff --git a/crates/matrix-sdk-ui/src/room_list/mod.rs b/crates/matrix-sdk-ui/src/room_list/mod.rs index af7d48078..7e9ad6ac1 100644 --- a/crates/matrix-sdk-ui/src/room_list/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list/mod.rs @@ -99,7 +99,7 @@ impl RoomList { let mut sliding_sync_builder = client.sliding_sync("room-list").map_err(Error::SlidingSync)?; - if let Some(sliding_sync_proxy_url) = client.sliding_sync_proxy().await { + if let Some(sliding_sync_proxy_url) = client.sliding_sync_proxy() { sliding_sync_builder = sliding_sync_builder.sliding_sync_proxy(sliding_sync_proxy_url); } diff --git a/crates/matrix-sdk/src/client/builder.rs b/crates/matrix-sdk/src/client/builder.rs index c6b159425..3b8fd062d 100644 --- a/crates/matrix-sdk/src/client/builder.rs +++ b/crates/matrix-sdk/src/client/builder.rs @@ -13,7 +13,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::{fmt, sync::Arc}; +use std::{ + fmt, + sync::{Arc, RwLock as StdRwLock}, +}; use matrix_sdk_base::{store::StoreConfig, BaseClient}; use ruma::{ @@ -400,7 +403,7 @@ impl ClientBuilder { homeserver, authentication_issuer, #[cfg(feature = "experimental-sliding-sync")] - sliding_sync_proxy: RwLock::new(sliding_sync_proxy), + sliding_sync_proxy: StdRwLock::new(sliding_sync_proxy), http_client, base_client, server_versions: OnceCell::new_with(self.server_versions), diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index fdb07c746..2cf9ef112 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -19,7 +19,7 @@ use std::{ fmt::{self, Debug}, future::Future, pin::Pin, - sync::{Arc, Mutex as StdMutex}, + sync::{Arc, Mutex as StdMutex, RwLock as StdRwLock}, }; use dashmap::DashMap; @@ -142,7 +142,7 @@ pub(crate) struct ClientInner { authentication_issuer: Option>, /// The sliding sync proxy that is trusted by the homeserver. #[cfg(feature = "experimental-sliding-sync")] - sliding_sync_proxy: RwLock>, + sliding_sync_proxy: StdRwLock>, /// The underlying HTTP client. http_client: HttpClient, /// User session data. @@ -336,15 +336,15 @@ impl Client { /// The sliding sync proxy that is trusted by the homeserver. #[cfg(feature = "experimental-sliding-sync")] - pub async fn sliding_sync_proxy(&self) -> Option { - let server = self.inner.sliding_sync_proxy.read().await; + pub fn sliding_sync_proxy(&self) -> Option { + let server = self.inner.sliding_sync_proxy.read().unwrap(); Some(server.as_ref()?.clone()) } /// Force to set the sliding sync proxy URL. #[cfg(feature = "experimental-sliding-sync")] - pub async fn set_sliding_sync_proxy(&self, sliding_sync_proxy: Url) { - let mut lock = self.inner.sliding_sync_proxy.write().await; + pub fn set_sliding_sync_proxy(&self, sliding_sync_proxy: Url) { + let mut lock = self.inner.sliding_sync_proxy.write().unwrap(); *lock = Some(sliding_sync_proxy); }