hyperlane_plugin_shutdown/struct.rs
1use super::*;
2
3/// Configuration controlling graceful drain and OS signal handling for [`ShutdownCoordinator`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct ShutdownConfig {
6 /// Maximum duration in milliseconds to wait for active in-flight guards to drain.
7 #[get(type(copy))]
8 pub(crate) drain_timeout_ms: u64,
9 /// Maximum duration in milliseconds allowed for each individual [`ShutdownCallback`].
10 #[get(type(copy))]
11 pub(crate) callback_timeout_ms: u64,
12 /// Whether `arm_signals` listens for `SIGINT` (`Ctrl-C`).
13 #[get(type(copy))]
14 pub(crate) handle_sigint: bool,
15 /// Whether `arm_signals` listens for Unix `SIGTERM`.
16 #[get(type(copy))]
17 pub(crate) handle_sigterm: bool,
18}
19
20/// Summary report returned once a graceful shutdown sequence finishes.
21#[derive(Clone, Data, Debug, Eq, PartialEq)]
22pub struct ShutdownReport {
23 /// `true` when all tracked in-flight work drained before `drain_timeout_ms` elapsed.
24 #[get(type(copy))]
25 pub(crate) drained_cleanly: bool,
26 /// Number of still-active guards remaining if the drain timed out (`0` when clean).
27 #[get(type(copy))]
28 pub(crate) remaining_in_flight: usize,
29 /// Names of callbacks that completed within their timeout in execution order.
30 pub(crate) completed_callbacks: Vec<String>,
31 /// Names of callbacks that timed out.
32 pub(crate) timed_out_callbacks: Vec<String>,
33 /// Total elapsed shutdown duration in milliseconds.
34 #[get(type(copy))]
35 pub(crate) elapsed_ms: u64,
36}
37
38/// Thread-safe lock-free graceful shutdown and request-drain coordinator.
39#[derive(Clone)]
40pub struct ShutdownCoordinator {
41 /// Shared coordinator state.
42 pub(crate) inner: Arc<ShutdownCoordinatorInner>,
43}
44
45/// RAII guard representing one active unit of in-flight work tracked by [`ShutdownCoordinator`].
46pub struct ShutdownGuard {
47 /// Reference to the owning cache-line-isolated shard decremented on drop.
48 pub(crate) shard: Arc<ShutdownShard>,
49}
50
51/// Process-wide [`ShutdownPlugin`] singleton wrapping [`ShutdownCoordinator`].
52#[derive(Clone)]
53pub struct ShutdownPlugin {
54 /// Shared process-wide shutdown coordinator.
55 pub(crate) coordinator: ShutdownCoordinator,
56}
57
58/// Cold coordinator metadata, notifiers, and shutdown callbacks shared across all shards.
59pub(crate) struct ShutdownCore {
60 /// Active timeout and signal configuration.
61 pub(crate) config: ShutdownConfig,
62 /// `true` once [`ShutdownCoordinator::trigger`] has armed shutdown notifications.
63 pub(crate) triggered: AtomicBool,
64 /// `true` once `SHUTDOWN_BIT` has been propagated across all shards.
65 pub(crate) shards_armed: AtomicBool,
66 /// Notifier woken whenever shutdown is triggered.
67 pub(crate) shutdown_notify: Notify,
68 /// Notifier woken when any shard's in-flight count transitions down to `0` during shutdown.
69 pub(crate) drained_notify: Notify,
70 /// Ordered list of cleanup callbacks (executed in reverse LIFO order).
71 pub(crate) callbacks: Vec<Arc<dyn ShutdownCallback>>,
72}
73
74/// Cache-line-isolated in-flight shard eliminating cross-core contention on both state and `Arc` refcount.
75#[repr(align(128))]
76pub(crate) struct ShutdownShard {
77 /// Packed atomic state: high bit = `SHUTDOWN_BIT`, low bits = local active in-flight guard count.
78 pub(crate) state: AtomicUsize,
79 /// Shared cold coordinator core holding `drained_notify`.
80 pub(crate) core: Arc<ShutdownCore>,
81}
82
83/// Shared lock-free interior state of [`ShutdownCoordinator`].
84pub(crate) struct ShutdownCoordinatorInner {
85 /// Shared cold metadata and notifiers.
86 pub(crate) core: Arc<ShutdownCore>,
87 /// 32-way cache-line-isolated in-flight shards.
88 pub(crate) shards: Box<[Arc<ShutdownShard>; SHUTDOWN_SHARD_COUNT]>,
89}