Fix timeout issue & avoid crashing on http errors

This commit is contained in:
2021-06-11 14:36:33 +02:00
parent f2ce648764
commit 22b4daa26a

View File

@ -6,6 +6,7 @@ use futures::task::Poll;
use notify::{INotifyWatcher, Op, RecursiveMode, Watcher};
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt::Display;
use std::sync::mpsc;
use std::time::{Duration, Instant};
use stl_lib::api::url;
@ -31,7 +32,7 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
.watch(&opt.file, RecursiveMode::NonRecursive)
.expect("failed to watch file");
update_category_list(opt, cookie).await?;
soft_fail(update_category_list(opt, cookie).await);
let mut last_update = Instant::now();
let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt, cookie));
@ -77,13 +78,20 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
}
let elapsed = last_update.elapsed();
debug!(
"elapsed={} update_categories={:?} restart_wfe={}",
elapsed.as_secs_f32(),
update_categories,
restart_wfe
);
let update_categories =
update_categories.unwrap_or_else(|| elapsed > Duration::from_secs(opt.poll_delay));
if update_categories {
update_category_list(opt, cookie).await?;
if let Some(()) = soft_fail(update_category_list(opt, cookie).await) {
last_update = Instant::now();
}
}
if update_categories || restart_wfe {
wfe = spawn(wait_for_event(opt, cookie));
@ -100,7 +108,13 @@ struct WaitForEvent {
async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result<WaitForEvent, Error> {
info!("calling wait_for_event");
let client = reqwest::Client::new();
const POLL_TIMEOUT: Duration = Duration::from_secs(5);
let client = reqwest::Client::builder()
.tcp_keepalive(Some(Duration::from_secs(opt.poll_delay)))
.timeout(Duration::from_secs(opt.poll_delay) + POLL_TIMEOUT)
.build()
.expect("TCP client");
let wfe: WaitForEvent = client
.get(format!(
"{}{}?timeout={}",
@ -161,3 +175,19 @@ async fn get_active_categories(opt: &DaemonOpt, cookie: &str) -> Result<Vec<Stri
.map(|category| category.name)
.collect())
}
/// Convert a Result into an Option.
///
/// If the Result was an error, log it and return None.
fn soft_fail<T, E>(result: Result<T, E>) -> Option<T>
where
E: Display,
{
match result {
Ok(t) => Some(t),
Err(e) => {
error!("{}", e);
None
}
}
}