From: Ian Jackson Date: Wed, 4 Aug 2021 00:21:26 +0000 (+0100) Subject: report, wip X-Git-Tag: hippotat/1.0.0~356 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=7197b80104d08ef240629e1dd95e3ae297dc96d2;p=hippotat.git report, wip Signed-off-by: Ian Jackson --- diff --git a/src/prelude.rs b/src/prelude.rs index a133343..c60db81 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -41,7 +41,7 @@ pub use tokio::io::{AsyncBufReadExt, AsyncWriteExt}; pub use tokio::pin; pub use tokio::select; pub use tokio::task; -pub use tokio::time::Duration; +pub use tokio::time::{Duration, Instant}; pub use void::{self, Void, ResultVoidExt, ResultVoidErrExt}; pub use crate::config::{self, InstanceConfig, u32Ext as _}; diff --git a/src/reporter.rs b/src/reporter.rs index 68f929d..915d7e7 100644 --- a/src/reporter.rs +++ b/src/reporter.rs @@ -6,8 +6,16 @@ use crate::prelude::*; pub struct Reporter<'r> { ic: &'r InstanceConfig, + successes: u64, + last_report: Option, } +#[derive(Debug)] +struct Report { + when: Instant, + ok: Result<(),()>, +} + // Reporting strategy // - report all errors // - report first success after a period of lack of messages @@ -15,14 +23,30 @@ pub struct Reporter<'r> { impl<'r> Reporter<'r> { pub fn new(ic: &'r InstanceConfig) -> Self { Reporter { - ic + ic, + successes: 0, + last_report: None, } } pub fn success(&mut self) { - info!("{}: success", self.ic); // xxx + self.successes += 1; + let now = Instant::now(); + if let Some(rep) = &self.last_report { + if now - rep.when < match rep.ok { + Ok(()) => Duration::from_secs(3600), + Err(()) => Duration::from_secs(30), + } { + return + } + } + + info!("{} ({}ok): running", self.ic, self.successes); + self.last_report = Some(Report { when: now, ok: Ok(()) }); } + pub fn filter(&mut self, req_num: Option, r: Result) -> Option { + let now = Instant::now(); match r { Ok(t) => { // xxx something something success @@ -35,10 +59,15 @@ impl<'r> Reporter<'r> { if let Some(req_num) = req_num { write!(m, " #{}", req_num)?; } + if self.successes > 0 { + write!(m, " ({}ok)", self.successes)?; + self.successes = 0; + } write!(m, ": {:?}", e)?; Ok::<_,fmt::Error>(m) })().unwrap(); warn!("{}", m); + self.last_report = Some(Report { when: now, ok: Err(()) }); None }, }