Diffinery

homeflare/celldpublic

celld/crates/logic/alarm.rs
34 lines · 1.3 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! Alarm retry policy, reified sans-IO. After a handler fails, `celld` persists
4//! a backoff in SQLite (`storage.rs`) and abandons the alarm once a bounded
5//! number of limit-counting failures accrue. That schedule is a pure function
6//! of the row's retry counters; storage is its executor.
7
8use crate::Ms;
9
10/// Failures that count toward the ceiling before the alarm is abandoned.
11const RETRY_CEILING: i64 = 6;
12/// Backoff base; the nth retry waits `BACKOFF_BASE_MS << min(retry, MAX_SHIFT)`.
13const BACKOFF_BASE_MS: Ms = 2_000;
14const MAX_BACKOFF_SHIFT: i64 = 6;
15
16/// `retry` is the total failure count (drives the exponential backoff);
17/// `counted_retry` is how many counted against the limit. `counts_against_limit`
18/// is false for failures the caller excuses (e.g. a shed under pressure) — those
19/// back off but never reach the ceiling. Returns the next deadline, or `None`
20/// when the counted failure ceiling is reached.
21pub fn alarm_retry(
22 now_ms: Ms,
23 retry: i64,
24 counted_retry: i64,
25 counts_against_limit: bool,
26) -> Option<Ms> {
27 if counts_against_limit && counted_retry >= RETRY_CEILING {
28 None
29 } else {
30 let shift = retry.clamp(0, MAX_BACKOFF_SHIFT) as u32;
31 let at_ms = now_ms.saturating_add(BACKOFF_BASE_MS * (1_i64 << shift));
32 Some(at_ms)
33 }
34}