diff --git a/crates/matrix-sdk/src/config/request.rs b/crates/matrix-sdk/src/config/request.rs index 650bbf4758d..88b66e4ff05 100644 --- a/crates/matrix-sdk/src/config/request.rs +++ b/crates/matrix-sdk/src/config/request.rs @@ -14,6 +14,7 @@ use std::{ fmt::{self, Debug}, + num::NonZeroUsize, time::Duration, }; @@ -44,7 +45,7 @@ pub struct RequestConfig { pub(crate) timeout: Duration, pub(crate) retry_limit: Option, pub(crate) retry_timeout: Option, - pub(crate) max_concurrent_requests: usize, + pub(crate) max_concurrent_requests: Option, pub(crate) force_auth: bool, } @@ -58,7 +59,7 @@ impl Debug for RequestConfig { res.field("timeout", timeout) .maybe_field("retry_limit", retry_limit) .maybe_field("retry_timeout", retry_timeout) - .field("max_concurrent_requests", max_concurrent_requests); + .maybe_field("max_concurrent_requests", max_concurrent_requests); if *force_auth { res.field("force_auth", &true); @@ -74,7 +75,7 @@ impl Default for RequestConfig { timeout: DEFAULT_REQUEST_TIMEOUT, retry_limit: Default::default(), retry_timeout: Default::default(), - max_concurrent_requests: 0, + max_concurrent_requests: Default::default(), force_auth: false, } } @@ -114,18 +115,11 @@ impl RequestConfig { /// Any additional request beyond that number will be waiting until another /// concurrent requests finished. Requests are queued fairly. #[must_use] - pub fn max_concurrent_requests(mut self, limit: usize) -> Self { + pub fn max_concurrent_requests(mut self, limit: Option) -> Self { self.max_concurrent_requests = limit; self } - /// Disable the limit of concurrent requests. Setting the limit to 0 - /// has the same effect. - #[must_use] - pub fn disable_max_concurrent_requests(mut self) -> Self { - self.max_concurrent_requests = 0; - self - } /// Set the timeout duration for all HTTP requests. #[must_use] pub fn timeout(mut self, timeout: Duration) -> Self { diff --git a/crates/matrix-sdk/src/http_client/mod.rs b/crates/matrix-sdk/src/http_client/mod.rs index e12844f51a5..243b85c33d6 100644 --- a/crates/matrix-sdk/src/http_client/mod.rs +++ b/crates/matrix-sdk/src/http_client/mod.rs @@ -15,6 +15,7 @@ use std::{ any::type_name, fmt::Debug, + num::NonZeroUsize, sync::{ atomic::{AtomicU64, Ordering}, Arc, @@ -56,8 +57,8 @@ struct MaybeSemaphore(Arc>); struct MaybeSemaphorePermit<'a>(Option>); impl MaybeSemaphore { - fn new(max: usize) -> Self { - let inner = if max > 0 { Some(Semaphore::new(max)) } else { None }; + fn new(max: Option) -> Self { + let inner = max.map(|i| Semaphore::new(i.into())); MaybeSemaphore(Arc::new(inner)) } @@ -76,7 +77,7 @@ impl MaybeSemaphore { pub(crate) struct HttpClient { pub(crate) inner: reqwest::Client, pub(crate) request_config: RequestConfig, - queue: MaybeSemaphore, + concurrent_request_semaphore: MaybeSemaphore, next_request_id: Arc, } @@ -85,7 +86,9 @@ impl HttpClient { HttpClient { inner, request_config, - queue: MaybeSemaphore::new(request_config.max_concurrent_requests), + concurrent_request_semaphore: MaybeSemaphore::new( + request_config.max_concurrent_requests, + ), next_request_id: AtomicU64::new(0).into(), } } @@ -215,7 +218,7 @@ impl HttpClient { }; // will be automatically dropped at the end of this function - let _handle = self.queue.acquire().await; + let _handle = self.concurrent_request_semaphore.acquire().await; debug!("Sending request");