Skip to content

Commit

Permalink
ffi: add method for the ClientBuilder to provide a RequestConfig
Browse files Browse the repository at this point in the history
…for the client
  • Loading branch information
jmartinesp committed Aug 7, 2024
1 parent 7d9fdc4 commit 57963dc
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::PathBuf, sync::Arc};
use std::{fs, num::NonZeroUsize, path::PathBuf, sync::Arc, time::Duration};

use futures_util::StreamExt;
use matrix_sdk::{
Expand Down Expand Up @@ -259,6 +259,7 @@ pub struct ClientBuilder {
additional_root_certificates: Vec<Vec<u8>>,
disable_built_in_root_certificates: bool,
encryption_settings: EncryptionSettings,
request_config: Option<RequestConfig>,
}

#[uniffi::export(async_runtime = "tokio")]
Expand Down Expand Up @@ -288,6 +289,7 @@ impl ClientBuilder {
matrix_sdk::encryption::BackupDownloadStrategy::AfterDecryptionFailure,
auto_enable_backups: false,
},
request_config: Default::default(),
})
}

Expand Down Expand Up @@ -442,6 +444,13 @@ impl ClientBuilder {
Arc::new(builder)
}

/// Add a default request config to this client.
pub fn request_config(self: Arc<Self>, config: RequestConfig) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.request_config = Some(config);
Arc::new(builder)
}

pub async fn build(self: Arc<Self>) -> Result<Arc<Client>, ClientBuildError> {
let builder = unwrap_or_clone_arc(self);
let mut inner_builder = MatrixClient::builder();
Expand Down Expand Up @@ -537,6 +546,27 @@ impl ClientBuilder {
inner_builder = inner_builder.requires_sliding_sync();
}

if let Some(config) = builder.request_config {
let mut updated_config = matrix_sdk::config::RequestConfig::default();
if let Some(retry_limit) = config.retry_limit {
updated_config = updated_config.retry_limit(retry_limit);
}
if let Some(timeout) = config.timeout {
updated_config = updated_config.timeout(Duration::from_millis(timeout));
}
if let Some(max_concurrent_requests) = config.max_concurrent_requests {
if max_concurrent_requests > 0 {
updated_config = updated_config.max_concurrent_requests(NonZeroUsize::new(
max_concurrent_requests as usize,
));
}
}
if let Some(retry_timeout) = config.retry_timeout {
updated_config = updated_config.retry_timeout(Duration::from_millis(retry_timeout));
}
inner_builder = inner_builder.request_config(updated_config);
}

let sdk_client = inner_builder.build().await?;

Ok(Arc::new(
Expand Down Expand Up @@ -602,3 +632,16 @@ impl ClientBuilder {
Ok(client)
}
}

#[derive(Clone, uniffi::Record)]
/// The config to use for HTTP requests by default in this client.
pub struct RequestConfig {
/// Max number of retries.
retry_limit: Option<u64>,
/// Timeout for a request in milliseconds.
timeout: Option<u64>,
/// Max number of concurrent requests. No value means no limits.
max_concurrent_requests: Option<u64>,
/// Base delay between retries.
retry_timeout: Option<u64>,
}

0 comments on commit 57963dc

Please sign in to comment.