//! THE KEYS THE OBJECT SAID IT DOES NOT HOLD, remembered so they are not//! asked again at once.//!//! On live the object answered 404 many times a minute for thumbnails and//! message pictures: a message imported on another device names its picture//! the moment its row syncs, while the bytes come up later in that device's//! drain (or never, when it was reinstalled first), and a phone asked for the//! same missing keys after every pass — the pictures sweep after each idle//! pass of the sync loop (up to 2,000 keys), and a card's picture on every//! composition (`media.ensure`).//!//! A 404 is now held here, per key, and the key is not asked again until its//! wait is over: [`FIRST_WAIT_MS`], doubling with each further 404 up to//! [`LONGEST_WAIT_MS`]. Bytes that do arrive (here or fetched) end the wait//! ([`AbsentKeys::forget`]). So is every answer that says the same of the key//! however often it is asked ([`is_held_answer`]: 404, 410, and a picture the//! object will not draw at a size, 400/413/415); a line that dropped, an//! object that did not answer or a credential refused says nothing about the//! key and is never held.//!//! In memory, for the life of the client: a launch asks each key once more.use std::collections::HashMap;use std::sync::Mutex;/// The wait after a key's first 404: fifteen minutes.pubconst FIRST_WAIT_MS: i64 = 15 * 60 * 1000;/// The longest wait, however often a key was missing: six hours.pubconst LONGEST_WAIT_MS: i64 = 6 * 60 * 60 * 1000;#[derive(Debug, Clone, Copy)]structMissing { times: u32, until_ms: i64, status: u16,}/// Whether an answer of `status` is one the same key gets however often it is/// asked, and so waits: gone (404, 410), or refused as it is (400, 413, 415).pubfn is_held_answer(status: u16) -> bool { matches!(status, 400 | 404 | 410 | 413 | 415)}/// The keys the object answered 404 for, and until when each waits.#[derive(Debug, Default)]pubstructAbsentKeys(Mutex<HashMap<String, Missing>>);/// The wait after a key's `times`-th 404 in a row.pubfn wait_after(times: u32) -> i64 {let doublings = times.saturating_sub(1).min(16); FIRST_WAIT_MS.saturating_mul(1i64 << doublings).min(LONGEST_WAIT_MS)}implAbsentKeys {fn held(&self) -> std::sync::MutexGuard<'_, HashMap<String, Missing>> {self.0.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) }/// Until when `key` waits at `now_ms`, and the answer it last had;/// `None` when it may be asked.pubfn waits(&self, key: &str, now_ms: i64) -> Option<(i64, u16)> {self.held().get(key).filter(|missing| missing.until_ms > now_ms).map(|missing| (missing.until_ms, missing.status)) }/// The object answered `status` for `key` at `now_ms`: it waits, longer/// each time. Answers until when.pubfn note(&self, key: &str, status: u16, now_ms: i64) -> i64 {letmut held = self.held();let times = held.get(key).map_or(1, |missing| missing.times.saturating_add(1));let until_ms = now_ms.saturating_add(wait_after(times)); held.insert(key.to_string(), Missing { times, until_ms, status }); until_ms }/// `key`'s bytes are here: it is no longer missing.pubfn forget(&self, key: &str) {self.held().remove(key); }/// Every key may be asked again (the far end changed).pubfn clear(&self) {self.held().clear(); }/// How many keys wait.pubfn len(&self) -> usize {self.held().len() }pubfn is_empty(&self) -> bool {self.held().is_empty() }}#[cfg(test)]mod tests {usesuper::*;/// RULE: a key the object answered 404 for is not asked again for fifteen minutes, then for twice as long after each further 404, never more than six hours; bytes that arrive end the wait. #[test]fn a_missing_key_waits_longer_each_time_and_its_bytes_end_the_wait() {let absent = AbsentKeys::default();let key = "db/messages/thread/picture.jpg.t256.jpg"; assert_eq!(absent.waits(key, 0), None, "never missing: asked"); assert_eq!(absent.note(key, 404, 0), FIRST_WAIT_MS); assert_eq!(absent.waits(key, FIRST_WAIT_MS - 1), Some((FIRST_WAIT_MS, 404))); assert_eq!(absent.waits(key, FIRST_WAIT_MS), None, "the wait is over"); assert_eq!(absent.note(key, 404, FIRST_WAIT_MS), FIRST_WAIT_MS * 3, "the second wait is twice the first");for _ in0..20 { absent.note(key, 413, 0); } assert_eq!(absent.waits(key, 0), Some((LONGEST_WAIT_MS, 413))); assert_eq!(absent.waits("db/other", 0), None, "only that key waits"); absent.forget(key); assert_eq!(absent.waits(key, 0), None); assert!(absent.is_empty()); assert_eq!(wait_after(1), FIRST_WAIT_MS); assert_eq!(wait_after(2), 2 * FIRST_WAIT_MS); assert_eq!(wait_after(u32::MAX), LONGEST_WAIT_MS); }/// RULE: only an answer the same key gets however often it is asked waits (gone, or refused as it is); a dropped line (0), a refused credential (401, 403) or a busy object (429, 5xx) is asked again at once. #[test]fn only_an_answer_about_the_key_itself_waits() {for status in [400, 404, 410, 413, 415] { assert!(is_held_answer(status), "{status}"); }for status in [0, 200, 401, 403, 408, 429, 500, 502, 503] { assert!(!is_held_answer(status), "{status}"); } }}