From f3bbc6ad9fe0e519f92944f9da22a90532cc5d11 Mon Sep 17 00:00:00 2001 From: Ulyssa Date: Sun, 12 Mar 2023 15:43:13 -0700 Subject: [PATCH] Support configuring client request timeout (#54) --- src/config.rs | 6 ++++++ src/tests.rs | 1 + src/worker.rs | 15 +++++++-------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8d69e6a..47f51d2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,8 @@ macro_rules! usage { } } +const DEFAULT_REQ_TIMEOUT: u64 = 120; + const COLORS: [Color; 13] = [ Color::Blue, Color::Cyan, @@ -182,6 +184,7 @@ pub struct TunableValues { pub reaction_shortcode_display: bool, pub read_receipt_send: bool, pub read_receipt_display: bool, + pub request_timeout: u64, pub typing_notice_send: bool, pub typing_notice_display: bool, pub users: UserOverrides, @@ -194,6 +197,7 @@ pub struct Tunables { pub reaction_shortcode_display: Option, pub read_receipt_send: Option, pub read_receipt_display: Option, + pub request_timeout: Option, pub typing_notice_send: Option, pub typing_notice_display: Option, pub users: Option, @@ -209,6 +213,7 @@ impl Tunables { .or(other.reaction_shortcode_display), read_receipt_send: self.read_receipt_send.or(other.read_receipt_send), read_receipt_display: self.read_receipt_display.or(other.read_receipt_display), + request_timeout: self.request_timeout.or(other.request_timeout), typing_notice_send: self.typing_notice_send.or(other.typing_notice_send), typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), users: merge_users(self.users, other.users), @@ -222,6 +227,7 @@ impl Tunables { reaction_shortcode_display: self.reaction_shortcode_display.unwrap_or(false), read_receipt_send: self.read_receipt_send.unwrap_or(true), read_receipt_display: self.read_receipt_display.unwrap_or(true), + request_timeout: self.request_timeout.unwrap_or(DEFAULT_REQ_TIMEOUT), typing_notice_send: self.typing_notice_send.unwrap_or(true), typing_notice_display: self.typing_notice_display.unwrap_or(true), users: self.users.unwrap_or_default(), diff --git a/src/tests.rs b/src/tests.rs index 8d7fc1c..b2cbfc9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -174,6 +174,7 @@ pub fn mock_tunables() -> TunableValues { reaction_shortcode_display: false, read_receipt_send: true, read_receipt_display: true, + request_timeout: 120, typing_notice_send: true, typing_notice_display: true, users: vec![(TEST_USER5.clone(), UserDisplayTunables { diff --git a/src/worker.rs b/src/worker.rs index 21667f5..12eaa79 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -82,7 +82,6 @@ use crate::{ const IAMB_DEVICE_NAME: &str = "iamb"; const IAMB_USER_AGENT: &str = "iamb"; -const REQ_TIMEOUT: Duration = Duration::from_secs(60); fn initial_devname() -> String { format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) @@ -447,20 +446,20 @@ impl ClientWorker { let (tx, rx) = unbounded_channel(); let account = &settings.profile; - // Set up a custom client that only uses HTTP/1. - // - // During my testing, I kept stumbling across something weird with sync and HTTP/2 that - // will need to be revisited in the future. + let req_timeout = Duration::from_secs(settings.tunables.request_timeout); + + // Set up the HTTP client. let http = reqwest::Client::builder() .user_agent(IAMB_USER_AGENT) - .timeout(Duration::from_secs(30)) + .timeout(req_timeout) .pool_idle_timeout(Duration::from_secs(60)) .pool_max_idle_per_host(10) .tcp_keepalive(Duration::from_secs(10)) - .http1_only() .build() .unwrap(); + let req_config = RequestConfig::new().timeout(req_timeout).retry_timeout(req_timeout); + // Set up the Matrix client for the selected profile. let client = Client::builder() .http_client(Arc::new(http)) @@ -468,7 +467,7 @@ impl ClientWorker { .store_config(StoreConfig::default()) .sled_store(settings.matrix_dir.as_path(), None) .expect("Failed to setup up sled store for Matrix SDK") - .request_config(RequestConfig::new().timeout(REQ_TIMEOUT).retry_timeout(REQ_TIMEOUT)) + .request_config(req_config) .build() .await .expect("Failed to instantiate Matrix client");