Skip to content

Commit

Permalink
Switch concurrent_request_semaphore param to Option<NonZeroUsize>
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Jul 1, 2024
1 parent f866810 commit 5fe3b94
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
16 changes: 5 additions & 11 deletions crates/matrix-sdk/src/config/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::{
fmt::{self, Debug},
num::NonZeroUsize,
time::Duration,
};

Expand Down Expand Up @@ -44,7 +45,7 @@ pub struct RequestConfig {
pub(crate) timeout: Duration,
pub(crate) retry_limit: Option<u64>,
pub(crate) retry_timeout: Option<Duration>,
pub(crate) max_concurrent_requests: usize,
pub(crate) max_concurrent_requests: Option<NonZeroUsize>,
pub(crate) force_auth: bool,
}

Expand All @@ -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);
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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<NonZeroUsize>) -> 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 {
Expand Down
13 changes: 8 additions & 5 deletions crates/matrix-sdk/src/http_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::{
any::type_name,
fmt::Debug,
num::NonZeroUsize,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
Expand Down Expand Up @@ -56,8 +57,8 @@ struct MaybeSemaphore(Arc<Option<Semaphore>>);
struct MaybeSemaphorePermit<'a>(Option<SemaphorePermit<'a>>);

impl MaybeSemaphore {
fn new(max: usize) -> Self {
let inner = if max > 0 { Some(Semaphore::new(max)) } else { None };
fn new(max: Option<NonZeroUsize>) -> Self {
let inner = max.map(|i| Semaphore::new(i.into()));
MaybeSemaphore(Arc::new(inner))
}

Expand All @@ -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<AtomicU64>,
}

Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 5fe3b94

Please sign in to comment.