feat(sdk): Client::sliding_sync_proxy is a StdRwLock.

This patch changes `matrix_sdk::Client::sliding_sync_proxy` to be a
`std::sync::RwLock<Option<Url>>` 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.
This commit is contained in:
Ivan Enderlin
2023-06-12 23:14:58 +02:00
parent 7636a069b7
commit 9bcc50fe2f
6 changed files with 16 additions and 17 deletions

View File

@@ -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<String> {
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<String>) {

View File

@@ -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);

View File

@@ -867,9 +867,7 @@ impl Client {
pub fn sliding_sync(&self, id: String) -> Result<Arc<SlidingSyncBuilder>, 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);
}

View File

@@ -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);
}

View File

@@ -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),

View File

@@ -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<RwLock<String>>,
/// The sliding sync proxy that is trusted by the homeserver.
#[cfg(feature = "experimental-sliding-sync")]
sliding_sync_proxy: RwLock<Option<Url>>,
sliding_sync_proxy: StdRwLock<Option<Url>>,
/// 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<Url> {
let server = self.inner.sliding_sync_proxy.read().await;
pub fn sliding_sync_proxy(&self) -> Option<Url> {
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);
}