use std::{
collections::{hash_map, HashMap},
time::Duration,
};
use rho_sdk::SessionId;
use super::{
checkout::Checkout,
model::PrId,
poller::{LINKED_POLL, WATCH_POLL},
};
pub(crate) const SHOWN_POLL: Duration = Duration::from_secs(30);
pub(crate) const LISTED_POLL: Duration = Duration::from_secs(300);
const LEASE_PERIODS: u32 = 2;
pub(super) const DUE_SLACK: Duration = Duration::from_secs(15);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum Cadence {
Shown,
Watched,
Listed,
Linked,
}
impl Cadence {
pub(super) fn period(self) -> Duration {
match self {
Self::Shown => SHOWN_POLL,
Self::Watched => WATCH_POLL,
Self::Listed => LISTED_POLL,
Self::Linked => LINKED_POLL,
}
}
pub(super) fn due(self, asked_at: Option<u64>, now: u64) -> bool {
asked_at.is_none_or(|at| {
now.saturating_add(millis(DUE_SLACK)) >= at.saturating_add(millis(self.period()))
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Shown {
Branch,
Linked(PrId),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Lease {
New,
Renewed,
}
#[derive(Debug, Default)]
pub(super) struct Interest {
sessions: HashMap<SessionId, SessionInterest>,
}
#[derive(Debug)]
pub(super) struct SessionInterest {
listed_until: u64,
shown: Vec<(Shown, u64)>,
checkout: Checkout,
}
impl SessionInterest {
pub(super) fn checkout(&self) -> &Checkout {
&self.checkout
}
pub(super) fn shown(&self, now: u64) -> impl Iterator<Item = &Shown> + '_ {
self.shown
.iter()
.filter(move |(_, until)| *until > now)
.map(|(shown, _)| shown)
}
}
impl Interest {
pub(super) fn list(&mut self, session: &SessionId, checkout: Checkout, now: u64) -> Lease {
let (entry, moved) = self.keep(session, checkout);
let live = entry.listed_until > now;
entry.listed_until = now.saturating_add(lease(LISTED_POLL));
if live && !moved {
Lease::Renewed
} else {
Lease::New
}
}
pub(super) fn show(
&mut self,
session: &SessionId,
checkout: Checkout,
what: Shown,
now: u64,
) -> Lease {
let (entry, moved) = self.keep(session, checkout);
let lasts_until = now.saturating_add(lease(SHOWN_POLL));
entry.shown.retain(|(_, until)| *until > now);
let live = match entry.shown.iter().position(|(shown, _)| *shown == what) {
Some(ix) => {
entry.shown[ix].1 = lasts_until;
true
}
None => {
entry.shown.push((what, lasts_until));
false
}
};
if live && !moved {
Lease::Renewed
} else {
Lease::New
}
}
pub(super) fn cadence(&self, session: &SessionId, target: &Shown, now: u64) -> Option<Cadence> {
let entry = self.sessions.get(session)?;
if entry.shown(now).any(|shown| shown == target) {
return Some(Cadence::Shown);
}
(entry.listed_until > now).then_some(Cadence::Listed)
}
pub(super) fn leased(&self, session: &SessionId, now: u64) -> bool {
self.sessions
.get(session)
.is_some_and(|entry| entry.listed_until > now || entry.shown(now).next().is_some())
}
pub(super) fn shows_any(&self, now: u64) -> bool {
self.sessions
.values()
.any(|entry| entry.shown(now).next().is_some())
}
pub(super) fn session(&self, session: &SessionId) -> Option<&SessionInterest> {
self.sessions.get(session)
}
pub(super) fn sessions(&self) -> impl Iterator<Item = (&SessionId, &SessionInterest)> + '_ {
self.sessions.iter()
}
pub(super) fn forget(&mut self, session: &SessionId) {
self.sessions.remove(session);
}
fn keep(&mut self, session: &SessionId, checkout: Checkout) -> (&mut SessionInterest, bool) {
match self.sessions.entry(session.clone()) {
hash_map::Entry::Occupied(entry) => {
let entry = entry.into_mut();
let moved = entry.checkout != checkout;
entry.checkout = checkout;
(entry, moved)
}
hash_map::Entry::Vacant(entry) => {
let entry = entry.insert(SessionInterest {
listed_until: 0,
shown: Vec::new(),
checkout,
});
(entry, true)
}
}
}
}
fn lease(period: Duration) -> u64 {
millis(period).saturating_mul(u64::from(LEASE_PERIODS))
}
pub(super) fn millis(duration: Duration) -> u64 {
u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
}