mm1_sup/common/
restart_intensity.rs1use crate::common::time;
2
3#[derive(Debug, thiserror::Error)]
4#[error("max restart intensity reached")]
5pub struct MaxRestartIntensityReached;
6
7#[derive(Debug, Clone, Copy)]
8pub struct RestartIntensity<D> {
9 pub max_restarts: usize,
10 pub within: D,
11}
12
13#[derive(Debug)]
14pub struct RestartStats<T>(Vec<T>);
15
16impl<D> RestartIntensity<D>
17where
18 D: time::D,
19{
20 pub fn new_stats(&self) -> RestartStats<D::I> {
21 RestartStats(Vec::with_capacity(self.max_restarts + 1))
22 }
23
24 pub fn report_exit(
25 &self,
26 stats: &mut RestartStats<D::I>,
27 now: D::I,
28 ) -> Result<(), MaxRestartIntensityReached> {
29 stats.0.retain(|i| (now - *i) < self.within);
30 stats.0.push(now);
31
32 if stats.0.len() > self.max_restarts {
33 Err(MaxRestartIntensityReached)
34 } else {
35 Ok(())
36 }
37 }
38}