Skip to main content

serenade_http/ops/
shutdown.rs

1//! Readiness flag for probes and graceful drain.
2
3use std::sync::Arc;
4use std::sync::atomic::{AtomicBool, Ordering};
5
6/// Shared readiness bit flipped false before HTTP drain / process exit.
7///
8/// Wire into [`super::HealthMiddleware`] / [`super::AsyncHealthMiddleware`]. Typical
9/// stop order: `mark_not_ready`, stop the HTTP server (Actix `ServerHandle` /
10/// Axum shutdown handle), then run application kernel shutdown.
11#[derive(Clone, Debug, Default)]
12pub struct Readiness {
13    ready: Arc<AtomicBool>,
14}
15
16impl Readiness {
17    /// Starts ready (`true`).
18    #[must_use]
19    pub fn new() -> Self {
20        Self {
21            ready: Arc::new(AtomicBool::new(true)),
22        }
23    }
24
25    /// Whether probes should report ready.
26    #[must_use]
27    pub fn is_ready(&self) -> bool {
28        self.ready.load(Ordering::SeqCst)
29    }
30
31    /// Marks the process not ready (fail readiness probes while draining).
32    pub fn mark_not_ready(&self) {
33        self.ready.store(false, Ordering::SeqCst);
34    }
35
36    /// Marks the process ready again (tests / rare re-arm).
37    pub fn mark_ready(&self) {
38        self.ready.store(true, Ordering::SeqCst);
39    }
40}