use std::time::{Duration, Instant};
use crate::event::Name;
pub(crate) const TAIL_CAP: usize = 4096;
pub(crate) struct RetryState {
pub(crate) nth: usize,
pub(crate) total: usize,
pub(crate) wait: Duration,
pub(crate) at: Instant,
pub(crate) base: String,
pub(crate) err: String,
pub(crate) streak: usize,
}
impl RetryState {
pub(crate) fn rem(&self, now: Instant) -> Duration {
self
.wait
.saturating_sub(now.saturating_duration_since(self.at))
}
}
pub(crate) struct Run {
pub(crate) id: u32,
pub(crate) name: Name,
pub(crate) begin: Instant,
pub(crate) at: Instant,
pub(crate) slot: Option<usize>,
pub(crate) tok: u64,
pub(crate) think: String,
pub(crate) reply: String,
pub(crate) retry: Option<RetryState>,
pub(crate) retry_cnt: usize,
}
impl Run {
pub(crate) fn push_tail(&mut self, s: &str, reply: bool) {
self.retry = None;
self.at = Instant::now();
let buf = if reply {
&mut self.reply
} else {
&mut self.think
};
buf.push_str(s);
if let Some(over) = buf.len().checked_sub(TAIL_CAP) {
buf.drain(..buf.ceil_char_boundary(over));
}
}
}
pub(crate) struct Ended {
pub(crate) name: Name,
pub(crate) slot: Option<usize>,
pub(crate) slot_label: Option<String>,
pub(crate) tok: u64,
pub(crate) sec: f32,
pub(crate) retry_cnt: usize,
pub(crate) err: Option<String>,
pub(crate) think: String,
}