From 1d57edfd80ac88c3e25234cf4c4cbdaafeffc479 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 28 Mar 2023 22:49:15 -0400 Subject: [PATCH] Remove blocking net request in main thread to speed up startup for slow connections --- backend/src/settings/detect/auto_detect.rs | 4 +- backend/src/settings/detect/limits_worker.rs | 45 +++++++------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/backend/src/settings/detect/auto_detect.rs b/backend/src/settings/detect/auto_detect.rs index 0c26e1f..567837b 100644 --- a/backend/src/settings/detect/auto_detect.rs +++ b/backend/src/settings/detect/auto_detect.rs @@ -23,11 +23,11 @@ fn get_limits() -> limits_core::json::Base { }, Err(e) => { log::warn!( - "Failed to open limits file `{}` (trying force refresh...): {}", + "Failed to open limits file `{}`: {}", limits_path.display(), e ); - super::limits_worker::get_limits_blocking() + super::limits_worker::get_limits_cached() } } } diff --git a/backend/src/settings/detect/limits_worker.rs b/backend/src/settings/detect/limits_worker.rs index 8a0738b..8a52b96 100644 --- a/backend/src/settings/detect/limits_worker.rs +++ b/backend/src/settings/detect/limits_worker.rs @@ -11,21 +11,26 @@ pub fn spawn() -> JoinHandle<()> { let sleep_dur = Duration::from_secs(60 * 60 * 24); // 1 day let limits_path = super::utility::limits_path(); loop { - thread::sleep(sleep_dur); if (limits_path.exists() && limits_path.is_file()) || !limits_path.exists() { // try to load limits from file, fallback to built-in default - let base = match std::fs::File::open(&limits_path) { - Ok(f) => match serde_json::from_reader(f) { - Ok(b) => b, + let base = if limits_path.exists() { + match std::fs::File::open(&limits_path) { + Ok(f) => match serde_json::from_reader(f) { + Ok(b) => b, + Err(e) => { + log::error!("Cannot parse {}: {}", limits_path.display(), e); + Base::default() + } + }, Err(e) => { - log::error!("Cannot parse {}: {}", limits_path.display(), e); + log::error!("Cannot open {}: {}", limits_path.display(), e); Base::default() } - }, - Err(e) => { - log::error!("Cannot open {}: {}", limits_path.display(), e); - Base::default() } + } else { + let base = Base::default(); + save_base(&base, &limits_path); + base }; if let Some(refresh) = &base.refresh { // try to retrieve newer version @@ -50,6 +55,7 @@ pub fn spawn() -> JoinHandle<()> { } else if !limits_path.is_file() { log::error!("Path for storing limits is not a file!"); } + thread::sleep(sleep_dur); } log::warn!("limits_worker completed!"); }) @@ -62,7 +68,7 @@ pub fn spawn() -> JoinHandle<()> { }) } -pub fn get_limits_blocking() -> Base { +pub fn get_limits_cached() -> Base { let limits_path = super::utility::limits_path(); if limits_path.is_file() { match std::fs::File::open(&limits_path) { @@ -79,25 +85,6 @@ pub fn get_limits_blocking() -> Base { } } } else { - #[cfg(feature = "online")] - { - let refresh = Base::default().refresh.unwrap(); - match ureq::get(&refresh) // try to retrieve newer version - .call() - { - Ok(response) => { - let json_res: std::io::Result = response.into_json(); - match json_res { - Ok(new_base) => { - save_base(&new_base, &limits_path); - return new_base; - } - Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e), - } - } - Err(e) => log::warn!("Cannot download limits from `{}`: {}", refresh, e), - } - } Base::default() } }