Skip to content

Commit 0833ffd

Browse files
jmartinesppoljar
authored andcommitted
refactor: Add RequestConfig::read_timeout to cancel network connections that have been stale for longer than the specified timeout
1 parent 16f7239 commit 0833ffd

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

crates/matrix-sdk/src/config/request.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use crate::http_client::DEFAULT_REQUEST_TIMEOUT;
4343
/// ```
4444
#[derive(Copy, Clone)]
4545
pub struct RequestConfig {
46-
pub(crate) timeout: Duration,
4746
pub(crate) timeout: Option<Duration>,
47+
pub(crate) read_timeout: Option<Duration>,
4848
pub(crate) retry_limit: Option<usize>,
4949
pub(crate) max_retry_time: Option<Duration>,
5050
pub(crate) max_concurrent_requests: Option<NonZeroUsize>,
@@ -57,6 +57,7 @@ impl Debug for RequestConfig {
5757
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
5858
let Self {
5959
timeout,
60+
read_timeout,
6061
retry_limit,
6162
max_retry_time: retry_timeout,
6263
force_auth,
@@ -66,6 +67,7 @@ impl Debug for RequestConfig {
6667

6768
let mut res = fmt.debug_struct("RequestConfig");
6869
res.field("timeout", timeout)
70+
.maybe_field("read_timeout", read_timeout)
6971
.maybe_field("retry_limit", retry_limit)
7072
.maybe_field("max_retry_time", retry_timeout)
7173
.maybe_field("max_concurrent_requests", max_concurrent_requests)
@@ -82,8 +84,8 @@ impl Debug for RequestConfig {
8284
impl Default for RequestConfig {
8385
fn default() -> Self {
8486
Self {
85-
timeout: DEFAULT_REQUEST_TIMEOUT,
8687
timeout: Some(DEFAULT_REQUEST_TIMEOUT),
88+
read_timeout: None,
8789
retry_limit: Default::default(),
8890
max_retry_time: Default::default(),
8991
max_concurrent_requests: Default::default(),
@@ -139,6 +141,20 @@ impl RequestConfig {
139141
self
140142
}
141143

144+
/// Set the read timeout duration for all HTTP requests.
145+
///
146+
/// The timeout applies to each read operation, and resets after a
147+
/// successful read. This is more appropriate for detecting stalled
148+
/// connections when the size isn’t known beforehand.
149+
///
150+
/// **IMPORTANT**: note this value can only be applied when the HTTP client
151+
/// is instantiated, it won't have any effect on a per-request basis.
152+
#[must_use]
153+
pub fn read_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
154+
self.read_timeout = timeout.into();
155+
self
156+
}
157+
142158
/// Set a time limit for how long a request should be retried. The default
143159
/// is that there isn't a limit, meaning requests are retried forever.
144160
///
@@ -182,12 +198,14 @@ mod tests {
182198
.force_auth()
183199
.max_retry_time(Duration::from_secs(32))
184200
.retry_limit(4)
185-
.timeout(Duration::from_secs(600));
201+
.timeout(Duration::from_secs(600))
202+
.read_timeout(Duration::from_secs(10));
186203

187204
assert!(cfg.force_auth);
188205
assert_eq!(cfg.retry_limit, Some(4));
189206
assert_eq!(cfg.max_retry_time, Some(Duration::from_secs(32)));
190-
assert_eq!(cfg.timeout, Duration::from_secs(600));
207+
assert_eq!(cfg.timeout, Some(Duration::from_secs(600)));
208+
assert_eq!(cfg.read_timeout, Some(Duration::from_secs(10)));
191209
}
192210

193211
#[test]

crates/matrix-sdk/src/http_client/native.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ impl HttpSettings {
185185
http_client = http_client.timeout(timeout);
186186
}
187187

188+
if let Some(read_timeout) = self.read_timeout {
189+
http_client = http_client.read_timeout(read_timeout);
190+
}
191+
188192
if self.disable_ssl_verification {
189193
warn!("SSL verification disabled in the HTTP client!");
190194
http_client = http_client.danger_accept_invalid_certs(true)

0 commit comments

Comments
 (0)