Fix timeout issue & avoid crashing on http errors
This commit is contained in:
@ -6,6 +6,7 @@ use futures::task::Poll;
|
|||||||
use notify::{INotifyWatcher, Op, RecursiveMode, Watcher};
|
use notify::{INotifyWatcher, Op, RecursiveMode, Watcher};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use stl_lib::api::url;
|
use stl_lib::api::url;
|
||||||
@ -31,7 +32,7 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
|
|||||||
.watch(&opt.file, RecursiveMode::NonRecursive)
|
.watch(&opt.file, RecursiveMode::NonRecursive)
|
||||||
.expect("failed to watch file");
|
.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 last_update = Instant::now();
|
||||||
|
|
||||||
let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt, cookie));
|
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();
|
let elapsed = last_update.elapsed();
|
||||||
|
debug!(
|
||||||
|
"elapsed={} update_categories={:?} restart_wfe={}",
|
||||||
|
elapsed.as_secs_f32(),
|
||||||
|
update_categories,
|
||||||
|
restart_wfe
|
||||||
|
);
|
||||||
let update_categories =
|
let update_categories =
|
||||||
update_categories.unwrap_or_else(|| elapsed > Duration::from_secs(opt.poll_delay));
|
update_categories.unwrap_or_else(|| elapsed > Duration::from_secs(opt.poll_delay));
|
||||||
|
|
||||||
if update_categories {
|
if update_categories {
|
||||||
update_category_list(opt, cookie).await?;
|
if let Some(()) = soft_fail(update_category_list(opt, cookie).await) {
|
||||||
last_update = Instant::now();
|
last_update = Instant::now();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if update_categories || restart_wfe {
|
if update_categories || restart_wfe {
|
||||||
@ -100,7 +108,13 @@ struct WaitForEvent {
|
|||||||
|
|
||||||
async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result<WaitForEvent, Error> {
|
async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result<WaitForEvent, Error> {
|
||||||
info!("calling wait_for_event");
|
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
|
let wfe: WaitForEvent = client
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}{}?timeout={}",
|
"{}{}?timeout={}",
|
||||||
@ -161,3 +175,19 @@ async fn get_active_categories(opt: &DaemonOpt, cookie: &str) -> Result<Vec<Stri
|
|||||||
.map(|category| category.name)
|
.map(|category| category.name)
|
||||||
.collect())
|
.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user