matrix_sdk/config/
request.rs1use std::{
16 fmt::{self, Debug},
17 num::NonZeroUsize,
18 time::Duration,
19};
20
21use matrix_sdk_common::debug::DebugStructExt;
22use ruma::api::MatrixVersion;
23
24use crate::http_client::DEFAULT_REQUEST_TIMEOUT;
25
26#[derive(Copy, Clone)]
45pub struct RequestConfig {
46 pub(crate) timeout: Duration,
47 pub(crate) retry_limit: Option<usize>,
48 pub(crate) max_retry_time: Option<Duration>,
49 pub(crate) max_concurrent_requests: Option<NonZeroUsize>,
50 pub(crate) force_auth: bool,
51 pub(crate) force_matrix_version: Option<MatrixVersion>,
52}
53
54#[cfg(not(tarpaulin_include))]
55impl Debug for RequestConfig {
56 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let Self {
58 timeout,
59 retry_limit,
60 max_retry_time: retry_timeout,
61 force_auth,
62 max_concurrent_requests,
63 force_matrix_version,
64 } = self;
65
66 let mut res = fmt.debug_struct("RequestConfig");
67 res.field("timeout", timeout)
68 .maybe_field("retry_limit", retry_limit)
69 .maybe_field("max_retry_time", retry_timeout)
70 .maybe_field("max_concurrent_requests", max_concurrent_requests)
71 .maybe_field("force_matrix_version", force_matrix_version);
72
73 if *force_auth {
74 res.field("force_auth", &true);
75 }
76
77 res.finish()
78 }
79}
80
81impl Default for RequestConfig {
82 fn default() -> Self {
83 Self {
84 timeout: DEFAULT_REQUEST_TIMEOUT,
85 retry_limit: Default::default(),
86 max_retry_time: Default::default(),
87 max_concurrent_requests: Default::default(),
88 force_auth: false,
89 force_matrix_version: Default::default(),
90 }
91 }
92}
93
94impl RequestConfig {
95 #[must_use]
97 pub fn new() -> Self {
98 Default::default()
99 }
100
101 #[must_use]
104 pub fn short_retry() -> Self {
105 Self::default().retry_limit(3)
106 }
107
108 #[must_use]
111 pub fn disable_retry(mut self) -> Self {
112 self.retry_limit = Some(0);
113 self
114 }
115
116 #[must_use]
119 pub fn retry_limit(mut self, retry_limit: usize) -> Self {
120 self.retry_limit = Some(retry_limit);
121 self
122 }
123
124 #[must_use]
128 pub fn max_concurrent_requests(mut self, limit: Option<NonZeroUsize>) -> Self {
129 self.max_concurrent_requests = limit;
130 self
131 }
132
133 #[must_use]
135 pub fn timeout(mut self, timeout: Duration) -> Self {
136 self.timeout = timeout;
137 self
138 }
139
140 #[must_use]
146 pub fn max_retry_time(mut self, retry_timeout: Duration) -> Self {
147 self.max_retry_time = Some(retry_timeout);
148 self
149 }
150
151 #[must_use]
154 pub fn force_auth(mut self) -> Self {
155 self.force_auth = true;
156 self
157 }
158
159 #[must_use]
165 pub(crate) fn force_matrix_version(mut self, version: MatrixVersion) -> Self {
166 self.force_matrix_version = Some(version);
167 self
168 }
169}
170
171#[cfg(test)]
172mod tests {
173 use std::time::Duration;
174
175 use super::RequestConfig;
176
177 #[test]
178 fn smoketest() {
179 let cfg = RequestConfig::new()
180 .force_auth()
181 .max_retry_time(Duration::from_secs(32))
182 .retry_limit(4)
183 .timeout(Duration::from_secs(600));
184
185 assert!(cfg.force_auth);
186 assert_eq!(cfg.retry_limit, Some(4));
187 assert_eq!(cfg.max_retry_time, Some(Duration::from_secs(32)));
188 assert_eq!(cfg.timeout, Duration::from_secs(600));
189 }
190
191 #[test]
192 fn testing_retry_settings() {
193 let mut cfg = RequestConfig::new();
194 assert_eq!(cfg.retry_limit, None);
195 cfg = cfg.retry_limit(10);
196 assert_eq!(cfg.retry_limit, Some(10));
197 cfg = cfg.disable_retry();
198 assert_eq!(cfg.retry_limit, Some(0));
199
200 let cfg = RequestConfig::short_retry();
201 assert_eq!(cfg.retry_limit, Some(3));
202 }
203}