//! THE TIME BOX — how long a pass may still run. //! //! A phone in the background is given time it did not choose: about thirty //! seconds for a wake, a few minutes for a processing task, and then the //! process is frozen wherever it stands. A pass that does not know this runs //! up to ten pages with a sixty-second ceiling on each, and is frozen with a //! request in the air. //! //! The box is a property of the PROCESS, not of the caller that set it: every //! pass running while it is closed observes it, whoever started that pass — //! the loop's own timer, a poke, the slice itself — because they are all the //! same process about to be frozen. It lives on the loop, is read before every //! page, and cuts each request's ceiling to what is left. //! //! NOTHING IS LOST BY STOPPING, and that is not this file's doing: a page is //! folded in one transaction with its cursor and its outbox settle //! (`cotton_store::exchange::fold_answer`), so a pass stopped after any page //! has kept every page before it, and the next pass asks from the folded //! point. What the box adds is that the stop is CLEAN — between pages, with no //! request abandoned — and that a day of short slices adds up to a backfill. //! //! A foreground open lifts it: a person looking at the app has no limit. use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; /// The least time a page is started with. /// /// A page is a round trip and a fold. Started with less than this it is a /// request that will be cut in the air — the Mac folds what it was handed and /// its answer is thrown away, which loses nothing and wastes the round trip. pub const PAGE_FLOOR_MS: u64 = 2_000; /// When the process must be done, or no limit at all. Cheap to clone; every /// clone is the same box. #[derive(Debug, Clone, Default)] pub struct TimeBox { until: Arc>>, /// WHERE THE PASS UNDER THIS BOX STANDS — the page it is asking for or /// folding, with its size and table mix — so a pass the loop stops /// waiting for says what it was doing (architect, 13 Oct 2026). doing: Arc>>, /// THE PASS RUNNING NOW IS CUT: it starts no further page, whatever time /// is left. Said when a pass the loop stopped waiting for is in the way of /// a new one ([`Self::cut`]); cleared as the next pass begins. cut: Arc, } impl TimeBox { /// No limit — what a foreground app and a Mac run under. pub fn open() -> TimeBox { TimeBox::default() } /// The process has `budget` from now. Answers the instant it set, which is /// what [`Self::lift_if`] takes: a slice lifts only the box it closed. pub fn close_in(&self, budget: Duration) -> Instant { let until = Instant::now() + budget; *self.held() = Some(until); until } /// No limit any more. pub fn lift(&self) { *self.held() = None; } /// Lift the box if it is still the one closed at `until` — a later slice's /// box is that slice's to lift. pub fn lift_if(&self, until: Instant) { let mut held = self.held(); if *held == Some(until) { *held = None; } } /// The instant the box closes at; `None` is no limit. pub fn closed_at(&self) -> Option { *self.held() } /// The time left; `None` is no limit. pub fn left(&self) -> Option { self.held() .map(|until| until.saturating_duration_since(Instant::now())) } /// Is there time to start another page? pub fn room_for_a_page(&self) -> bool { !self.cut.load(Ordering::SeqCst) && self .left() .is_none_or(|left| left >= Duration::from_millis(PAGE_FLOOR_MS)) } /// The pass running now starts no further page. pub fn cut(&self) { self.cut.store(true, Ordering::SeqCst); } /// A new pass begins: the cut was the last one's. pub fn uncut(&self) { self.cut.store(false, Ordering::SeqCst); } /// The pass is now doing `what`; `None` when it is over. pub fn doing(&self, what: Option) { *self.doing.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) = what; } /// What the pass under this box is doing, if one is. pub fn what_it_is_doing(&self) -> Option { self.doing.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).clone() } fn held(&self) -> std::sync::MutexGuard<'_, Option> { self.until.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) } } #[cfg(test)] mod tests { use super::*; #[test] fn an_open_box_has_room_and_a_closed_one_has_what_is_left() { let time_box = TimeBox::open(); assert_eq!(time_box.left(), None); assert!(time_box.room_for_a_page()); time_box.close_in(Duration::from_secs(60)); assert!(time_box.left().is_some_and(|left| left > Duration::from_secs(50))); assert!(time_box.room_for_a_page()); // Under the floor a page is not started: it would be cut in the air. time_box.close_in(Duration::from_millis(PAGE_FLOOR_MS / 2)); assert!(!time_box.room_for_a_page()); time_box.lift(); assert!(time_box.room_for_a_page()); } #[test] fn a_slice_lifts_only_the_box_it_closed() { let time_box = TimeBox::open(); let first = time_box.close_in(Duration::from_secs(1)); let second = time_box.clone().close_in(Duration::from_secs(90)); // The first slice ends: the box is the second's, and stays closed. time_box.lift_if(first); assert!(time_box.left().is_some()); time_box.lift_if(second); assert_eq!(time_box.left(), None); } }