Commit f3bbc6ad9f

f3bbc6ad9fe0e519f92944f9da22a90532cc5d11

parent: 2dd8c0fddf

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-12 22:43 UTC

Support configuring client request timeout (#54)

Layout: unified · split

src/config.rs +6
@@ -25,6 +25,8 @@ macro_rules! usage {
25 } 25 }
26} 26}
27 27
28const DEFAULT_REQ_TIMEOUT: u64 = 120;
29
28const COLORS: [Color; 13] = [ 30const COLORS: [Color; 13] = [
29 Color::Blue, 31 Color::Blue,
30 Color::Cyan, 32 Color::Cyan,
@@ -182,6 +184,7 @@ pub struct TunableValues {
182 pub reaction_shortcode_display: bool, 184 pub reaction_shortcode_display: bool,
183 pub read_receipt_send: bool, 185 pub read_receipt_send: bool,
184 pub read_receipt_display: bool, 186 pub read_receipt_display: bool,
187 pub request_timeout: u64,
185 pub typing_notice_send: bool, 188 pub typing_notice_send: bool,
186 pub typing_notice_display: bool, 189 pub typing_notice_display: bool,
187 pub users: UserOverrides, 190 pub users: UserOverrides,
@@ -194,6 +197,7 @@ pub struct Tunables {
194 pub reaction_shortcode_display: Option<bool>, 197 pub reaction_shortcode_display: Option<bool>,
195 pub read_receipt_send: Option<bool>, 198 pub read_receipt_send: Option<bool>,
196 pub read_receipt_display: Option<bool>, 199 pub read_receipt_display: Option<bool>,
200 pub request_timeout: Option<u64>,
197 pub typing_notice_send: Option<bool>, 201 pub typing_notice_send: Option<bool>,
198 pub typing_notice_display: Option<bool>, 202 pub typing_notice_display: Option<bool>,
199 pub users: Option<UserOverrides>, 203 pub users: Option<UserOverrides>,
@@ -209,6 +213,7 @@ impl Tunables {
209 .or(other.reaction_shortcode_display), 213 .or(other.reaction_shortcode_display),
210 read_receipt_send: self.read_receipt_send.or(other.read_receipt_send), 214 read_receipt_send: self.read_receipt_send.or(other.read_receipt_send),
211 read_receipt_display: self.read_receipt_display.or(other.read_receipt_display), 215 read_receipt_display: self.read_receipt_display.or(other.read_receipt_display),
216 request_timeout: self.request_timeout.or(other.request_timeout),
212 typing_notice_send: self.typing_notice_send.or(other.typing_notice_send), 217 typing_notice_send: self.typing_notice_send.or(other.typing_notice_send),
213 typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), 218 typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
214 users: merge_users(self.users, other.users), 219 users: merge_users(self.users, other.users),
@@ -222,6 +227,7 @@ impl Tunables {
222 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false), 227 reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false),
223 read_receipt_send: self.read_receipt_send.unwrap_or(true), 228 read_receipt_send: self.read_receipt_send.unwrap_or(true),
224 read_receipt_display: self.read_receipt_display.unwrap_or(true), 229 read_receipt_display: self.read_receipt_display.unwrap_or(true),
230 request_timeout: self.request_timeout.unwrap_or(DEFAULT_REQ_TIMEOUT),
225 typing_notice_send: self.typing_notice_send.unwrap_or(true), 231 typing_notice_send: self.typing_notice_send.unwrap_or(true),
226 typing_notice_display: self.typing_notice_display.unwrap_or(true), 232 typing_notice_display: self.typing_notice_display.unwrap_or(true),
227 users: self.users.unwrap_or_default(), 233 users: self.users.unwrap_or_default(),
src/tests.rs +1
@@ -174,6 +174,7 @@ pub fn mock_tunables() -> TunableValues {
174 reaction_shortcode_display: false, 174 reaction_shortcode_display: false,
175 read_receipt_send: true, 175 read_receipt_send: true,
176 read_receipt_display: true, 176 read_receipt_display: true,
177 request_timeout: 120,
177 typing_notice_send: true, 178 typing_notice_send: true,
178 typing_notice_display: true, 179 typing_notice_display: true,
179 users: vec![(TEST_USER5.clone(), UserDisplayTunables { 180 users: vec![(TEST_USER5.clone(), UserDisplayTunables {
src/worker.rs +7 −8
@@ -82,7 +82,6 @@ use crate::{
82 82
83const IAMB_DEVICE_NAME: &str = "iamb"; 83const IAMB_DEVICE_NAME: &str = "iamb";
84const IAMB_USER_AGENT: &str = "iamb"; 84const IAMB_USER_AGENT: &str = "iamb";
85const REQ_TIMEOUT: Duration = Duration::from_secs(60);
86 85
87fn initial_devname() -> String { 86fn initial_devname() -> String {
88 format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) 87 format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy())
@@ -447,20 +446,20 @@ impl ClientWorker {
447 let (tx, rx) = unbounded_channel(); 446 let (tx, rx) = unbounded_channel();
448 let account = &settings.profile; 447 let account = &settings.profile;
449 448
450 // Set up a custom client that only uses HTTP/1. 449 let req_timeout = Duration::from_secs(settings.tunables.request_timeout);
451 // 450
452 // During my testing, I kept stumbling across something weird with sync and HTTP/2 that 451 // Set up the HTTP client.
453 // will need to be revisited in the future.
454 let http = reqwest::Client::builder() 452 let http = reqwest::Client::builder()
455 .user_agent(IAMB_USER_AGENT) 453 .user_agent(IAMB_USER_AGENT)
456 .timeout(Duration::from_secs(30)) 454 .timeout(req_timeout)
457 .pool_idle_timeout(Duration::from_secs(60)) 455 .pool_idle_timeout(Duration::from_secs(60))
458 .pool_max_idle_per_host(10) 456 .pool_max_idle_per_host(10)
459 .tcp_keepalive(Duration::from_secs(10)) 457 .tcp_keepalive(Duration::from_secs(10))
460 .http1_only()
461 .build() 458 .build()
462 .unwrap(); 459 .unwrap();
463 460
461 let req_config = RequestConfig::new().timeout(req_timeout).retry_timeout(req_timeout);
462
464 // Set up the Matrix client for the selected profile. 463 // Set up the Matrix client for the selected profile.
465 let client = Client::builder() 464 let client = Client::builder()
466 .http_client(Arc::new(http)) 465 .http_client(Arc::new(http))
@@ -468,7 +467,7 @@ impl ClientWorker {
468 .store_config(StoreConfig::default()) 467 .store_config(StoreConfig::default())
469 .sled_store(settings.matrix_dir.as_path(), None) 468 .sled_store(settings.matrix_dir.as_path(), None)
470 .expect("Failed to setup up sled store for Matrix SDK") 469 .expect("Failed to setup up sled store for Matrix SDK")
471 .request_config(RequestConfig::new().timeout(REQ_TIMEOUT).retry_timeout(REQ_TIMEOUT)) 470 .request_config(req_config)
472 .build() 471 .build()
473 .await 472 .await
474 .expect("Failed to instantiate Matrix client"); 473 .expect("Failed to instantiate Matrix client");