From 22b4daa26a4d845510c82b847a2e0b5337adda1b Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Fri, 11 Jun 2021 14:36:33 +0200 Subject: [PATCH] Fix timeout issue & avoid crashing on http errors --- cli/src/daemon.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/cli/src/daemon.rs b/cli/src/daemon.rs index 0fe94ac..aedf366 100644 --- a/cli/src/daemon.rs +++ b/cli/src/daemon.rs @@ -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,12 +78,19 @@ 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?; - last_update = Instant::now(); + if let Some(()) = soft_fail(update_category_list(opt, cookie).await) { + last_update = Instant::now(); + } } if update_categories || restart_wfe { @@ -100,7 +108,13 @@ struct WaitForEvent { async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result { 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(result: Result) -> Option +where + E: Display, +{ + match result { + Ok(t) => Some(t), + Err(e) => { + error!("{}", e); + None + } + } +}