1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Application-registered readiness gates (AP2.1-10).
//!
//! [`ReadinessCheck`] is the seam an application uses to declare "my required
//! dependencies are ready" — beyond the engine's own startup. The engine
//! sets the lifecycle state to [`Ready`](super::state::LifecycleState::Ready)
//! once subsystems connect, but a production app often wants to gate
//! readiness on an additional condition (a warmup query, a cache primed, a
//! feature flag fetched, a migration-compatibility probe). The application
//! registers one or more [`ReadinessCheck`] closures via
//! [`Lifecycle::register_readiness`](super::Lifecycle::register_readiness)
//! inside its `state_fn`; the `/up/ready` endpoint returns 200 only when the
//! state is `Ready` **and** every registered check returns `true`
//! (PROGRAM.md AP2.1-10: "Readiness must NOT become true before required app
//! dependencies are ready").
//!
//! # Design
//!
//! A check is `Arc<dyn Fn() -> bool + Send + Sync>` — a cheap, clonable,
//! synchronous predicate. It is *not* async: the health endpoint is on the
//! hot path and must not block the runtime on a per-request `await`. A check
//! that needs async work (a warmup query) performs that work in a background
//! task and flips an `AtomicBool` the check reads synchronously. This keeps
//! the health endpoint latency bounded and the readiness decision O(1).
//!
//! The checks are stored in a `Vec` behind an [`ArcSwap`]-free design: the
//! list is built during startup (the `state_fn` runs once, before serving)
//! and read concurrently afterward. A [`std::sync::RwLock`] guards the list
//! — writes happen only at startup (a handful of `register_readiness`
//! calls), reads happen on every `/up/ready` request. The read lock is
//! cheap and uncontended after startup. No global mutable state: the
//! [`ReadinessChecks`] instance lives inside the [`Lifecycle`](super::Lifecycle)
//! handle, which is request-owned (passed in `state_fn`, cloned into
//! `AppState`), never a process-global singleton (AGENTS.md §20).
use Arc;
/// A synchronous readiness predicate registered by the application.
///
/// Returns `true` when the dependency the check represents is ready. The
/// `/up/ready` endpoint returns 200 only when the lifecycle state is `Ready`
/// and *every* registered check returns `true`.
///
/// Checks must be cheap and non-blocking (see the module docs): perform any
/// async warmup in a background task and read an `AtomicBool` here.
pub type ReadinessCheck = ;
/// The collection of readiness checks for one [`Lifecycle`](super::Lifecycle).
///
/// Built during startup (the application's `state_fn` calls
/// `register_readiness`); read on every `/up/ready` request. The read path
/// takes a read lock; after startup the lock is uncontended.
pub