use std::collections::BTreeMap;
use std::time::Duration;
use crate::state::{MailCell, lock_mail};
use super::doubt::unfinished;
use super::{Call, Invocation};
mod exchange;
pub(crate) mod read;
mod run;
use run::Run;
use read::{HOLD_TICK, HOLD_WAITS};
const TTL_SECONDS: i64 = 3600;
#[derive(Debug, Clone, PartialEq)]
struct Slot {
client: String,
by: String,
invocation: Invocation,
handed: u32,
run: Run,
heard: i64,
}
impl Slot {
fn doubt(&mut self, why: &str) {
let said = unfinished(&self.client, self.handed, why, self.run.arrived());
self.run.doubt(&said);
}
}
#[derive(Default)]
pub(crate) struct Slots {
live: BTreeMap<String, Slot>,
seq: u64,
reading: BTreeMap<String, u64>,
reads: u64,
}
#[derive(Clone)]
pub struct Mailbox {
cell: MailCell,
waits: u32,
tick: Duration,
}
impl Default for Mailbox {
fn default() -> Self {
Self {
cell: MailCell::default(),
waits: HOLD_WAITS,
tick: HOLD_TICK,
}
}
}
impl Mailbox {
pub fn holding(waits: u32, tick: Duration) -> Self {
Self {
cell: MailCell::default(),
waits,
tick,
}
}
pub fn post(&self, now: i64, by: &str, call: &Call) -> String {
let mut slots = lock_mail(&self.cell);
slots.live.retain(|_, slot| now - slot.heard <= TTL_SECONDS);
slots.seq += 1;
let id = format!("inv-{}", slots.seq);
slots.live.insert(
id.clone(),
Slot {
client: call.client.clone(),
by: by.to_owned(),
invocation: Invocation {
id: id.clone(),
tool: call.tool.clone(),
input: call.input.clone(),
cwd: call.cwd.clone(),
},
handed: 0,
run: Run::default(),
heard: now,
},
);
id
}
}
#[cfg(test)]
mod tests;