From ddab660945b12ab6676c20ae8829633427b4b4ca Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Thu, 2 May 2019 20:30:02 -0400 Subject: [PATCH] added RTT warning --- src/updater.rs | 64 +++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/updater.rs b/src/updater.rs index c295891..c77425a 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -51,59 +51,58 @@ fn get_result( ) -> EndpointStatus { let (label, path, port, code, body) = get_endpoint_info(endpoint.clone()); let url = get_url(&website_conf.base, &path, port).expect("reading config"); + let ping_start = Utc::now(); let ping_result = client.get(&url).send(); - let duration = Utc::now() - ping_start; - let duration = if duration.num_seconds() > 0 { - format!("{}s", duration.num_milliseconds() as f64 / 1000.) + let rtt = Utc::now() - ping_start; + let rtt_string = if rtt.num_seconds() > 0 { + format!("{}s", rtt.num_milliseconds() as f64 / 1000.) } else { - format!("{}ms", duration.num_milliseconds()) + format!("{}ms", rtt.num_milliseconds()) }; + match ping_result { Ok(mut res) => { let res_body = res.text().expect("could not get body of request"); let mut error = None; if res.status() != code { - error = Some(format!( - "Status code mismatch: {} != {}.", - res.status().as_u16(), - code - )); + error = append_err_msg( + error, + format!( + "Status code mismatch: {} != {}.", + res.status().as_u16(), + code + ), + ); } if let Some(expected_hash) = &endpoint.body_hash { let expected = from_hex(expected_hash).unwrap(); let actual = digest(&SHA256, String::from(res_body).as_bytes()); if &expected != &actual.as_ref() { - error = Some(if let Some(msg) = error { - format!("{} Body hash mismatch.", msg) - } else { - String::from("Body hash mismatch.") - }); + error = append_err_msg(error, String::from("Body hash mismatch.")); } } else if !body.is_empty() && res_body != body { - error = Some(if let Some(msg) = error { - format!( - "{} Body mismatch: {} != {}.", - msg, - res_body.len(), - body.len() - ) - } else { - format!("Body mismatch: {} != {}.", res_body.len(), body.len()) - }); + error = append_err_msg( + error, + format!("Body mismatch: {} != {}.", res_body.len(), body.len()), + ); + } + + if rtt.num_seconds() >= 2 { + error = append_err_msg(error, format!("RTT too long: {}", rtt_string)); } if error.is_some() { - EndpointStatus::warn(url, label, duration, error) + EndpointStatus::warn(url, label, rtt_string, error) } else { - EndpointStatus::ok(url, label, duration) + EndpointStatus::ok(url, label, rtt_string) } } Err(e) => { if let Some(true) = endpoint.should_err { - EndpointStatus::ok(url, label, duration) + EndpointStatus::ok(url, label, rtt_string) } else { EndpointStatus::error(url, label, Some(format!("{}", e))) } @@ -122,3 +121,14 @@ fn get_url(base: &Option, path: &String, port: Option) -> Result, to_append: String) -> Option { + // Turns out Rust doesn't recognize that only one of the FnOnce occurs, so + // as a result both closures want ownership of to_append + // Some(optional.map_or_else(|| to_append, |msg| format!("{} {}", msg, to_append))) + + Some(match optional { + Some(e) => format!("{} {}", e, to_append), + None => to_append, + }) +}