Cotton code
83 lines · 3.0 KB · RustRaw
//! How long to wait after a failure.//!//! The SERVER's own answer wins when it gave one: `Retry-After` on a 429 is the//! operator saying how busy they are, and a client that shortens it is arguing//! with the thing it depends on. Otherwise the store's ladder, jitter and all//! (`cotton_store::outbox::sync_backoff_ms`) — declared there so the phone and//! the Mac climb the same one.use crate::seams::Random;/// A `f64` in `[0, 1)` from an injected source — the store's ladder takes the/// jitter as a parameter rather than reading a generator, so a test asserts the/// arithmetic instead of a range.pub fn unit_random(random: &dyn Random) -> f64 {    let mut bytes = [0u8; 8];    random.fill(&mut bytes);    // 53 bits, the mantissa's worth: the same construction `Math.random` is    // specified to produce, so a ported ladder cannot drift on rounding.    let raw = u64::from_le_bytes(bytes) >> 11;    raw as f64 / (1u64 << 53) as f64}/// How long the loop waits before trying again.pub fn wait_ms_for(retry_after_seconds: Option<u32>, failures: i64, random: &dyn Random) -> i64 {    if let Some(seconds) = retry_after_seconds.filter(|seconds| *seconds > 0) {        return i64::from(seconds) * 1_000;    }    cotton_store::outbox::sync_backoff_ms(failures, unit_random(random))}#[cfg(test)]mod tests {    use super::*;    use crate::seams::SeededRandom;    #[derive(Debug)]    struct Fixed(f64);    impl Random for Fixed {        fn fill(&self, out: &mut [u8]) {            // The bytes that decode to this unit value, so the ladder's jitter            // is exactly what the test asked for.            let raw = (self.0 * (1u64 << 53) as f64) as u64;            let bytes = (raw << 11).to_le_bytes();            for (slot, byte) in out.iter_mut().zip(bytes) {                *slot = byte;            }        }    }    #[test]    fn the_servers_own_retry_after_wins_over_this_clients_ladder() {        // 429 with `Retry-After: 3` is three seconds, not the ladder's 30.        assert_eq!(wait_ms_for(Some(3), 5, &SeededRandom::default()), 3_000);    }    #[test]    fn a_retry_after_of_zero_is_no_answer_at_all_and_falls_back_to_the_ladder() {        assert_eq!(            wait_ms_for(Some(0), 0, &Fixed(0.5)),            wait_ms_for(None, 0, &Fixed(0.5))        );    }    #[test]    fn without_one_it_is_the_stores_ladder_floor_ceiling_and_jitter() {        // The store's own numbers: a 30 s floor doubling to a 5 min ceiling,        // ±20% either side of the base.        assert_eq!(wait_ms_for(None, 0, &Fixed(0.5)), 30_000);        assert_eq!(wait_ms_for(None, 1, &Fixed(0.5)), 60_000);        assert_eq!(wait_ms_for(None, 20, &Fixed(0.5)), 300_000);        assert_eq!(wait_ms_for(None, 0, &Fixed(0.0)), 24_000);    }    #[test]    fn the_jitter_stays_inside_the_unit_interval_whatever_the_source_answered() {        let random = SeededRandom::new(11);        for _ in 0..64 {            let value = unit_random(&random);            assert!((0.0..1.0).contains(&value), "{value}");        }    }}