Remove blocking net request in main thread to speed up startup for slow connections
This commit is contained in:
parent
c6a5d55da8
commit
1d57edfd80
2 changed files with 18 additions and 31 deletions
|
@ -23,11 +23,11 @@ fn get_limits() -> limits_core::json::Base {
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"Failed to open limits file `{}` (trying force refresh...): {}",
|
"Failed to open limits file `{}`: {}",
|
||||||
limits_path.display(),
|
limits_path.display(),
|
||||||
e
|
e
|
||||||
);
|
);
|
||||||
super::limits_worker::get_limits_blocking()
|
super::limits_worker::get_limits_cached()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,21 +11,26 @@ pub fn spawn() -> JoinHandle<()> {
|
||||||
let sleep_dur = Duration::from_secs(60 * 60 * 24); // 1 day
|
let sleep_dur = Duration::from_secs(60 * 60 * 24); // 1 day
|
||||||
let limits_path = super::utility::limits_path();
|
let limits_path = super::utility::limits_path();
|
||||||
loop {
|
loop {
|
||||||
thread::sleep(sleep_dur);
|
|
||||||
if (limits_path.exists() && limits_path.is_file()) || !limits_path.exists() {
|
if (limits_path.exists() && limits_path.is_file()) || !limits_path.exists() {
|
||||||
// try to load limits from file, fallback to built-in default
|
// try to load limits from file, fallback to built-in default
|
||||||
let base = match std::fs::File::open(&limits_path) {
|
let base = if limits_path.exists() {
|
||||||
Ok(f) => match serde_json::from_reader(f) {
|
match std::fs::File::open(&limits_path) {
|
||||||
Ok(b) => b,
|
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) => {
|
Err(e) => {
|
||||||
log::error!("Cannot parse {}: {}", limits_path.display(), e);
|
log::error!("Cannot open {}: {}", limits_path.display(), e);
|
||||||
Base::default()
|
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 {
|
if let Some(refresh) = &base.refresh {
|
||||||
// try to retrieve newer version
|
// try to retrieve newer version
|
||||||
|
@ -50,6 +55,7 @@ pub fn spawn() -> JoinHandle<()> {
|
||||||
} else if !limits_path.is_file() {
|
} else if !limits_path.is_file() {
|
||||||
log::error!("Path for storing limits is not a file!");
|
log::error!("Path for storing limits is not a file!");
|
||||||
}
|
}
|
||||||
|
thread::sleep(sleep_dur);
|
||||||
}
|
}
|
||||||
log::warn!("limits_worker completed!");
|
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();
|
let limits_path = super::utility::limits_path();
|
||||||
if limits_path.is_file() {
|
if limits_path.is_file() {
|
||||||
match std::fs::File::open(&limits_path) {
|
match std::fs::File::open(&limits_path) {
|
||||||
|
@ -79,25 +85,6 @@ pub fn get_limits_blocking() -> Base {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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<Base> = 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()
|
Base::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue