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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Health checking for containers
use crate::error::{AgentError, Result};
use crate::runtime::ContainerId;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::timeout;
use zlayer_spec::HealthCheck;
/// Callback type for health state changes.
/// Called with (`container_id`, `is_healthy`) when health state transitions.
pub type HealthCallback = Arc<dyn Fn(ContainerId, bool) + Send + Sync>;
/// Health checker for containers
pub struct HealthChecker {
pub check: HealthCheck,
/// Optional target IP address for health checks (e.g., container overlay IP).
/// When set, TCP and HTTP checks connect to this address instead of 127.0.0.1/localhost.
target_addr: Option<std::net::IpAddr>,
}
impl HealthChecker {
/// Create a new health checker
///
/// `target_addr` is the IP address to connect to for TCP/HTTP checks.
/// Pass `Some(ip)` when the container has an overlay IP, or `None` to
/// fall back to `127.0.0.1` / localhost.
#[must_use]
pub fn new(check: HealthCheck, target_addr: Option<std::net::IpAddr>) -> Self {
Self { check, target_addr }
}
/// Perform the health check
///
/// # Errors
/// Returns an error if the health check fails or times out.
pub async fn check(&self, id: &ContainerId, timeout: Duration) -> Result<()> {
match &self.check {
HealthCheck::Tcp { port } => self.check_tcp(id, *port, timeout).await,
HealthCheck::Http { url, expect_status } => {
self.check_http(id, url, *expect_status, timeout).await
}
HealthCheck::Command { command } => self.check_command(id, command, timeout).await,
}
}
async fn check_tcp(&self, id: &ContainerId, port: u16, timeout_dur: Duration) -> Result<()> {
// Connect to the target address (overlay IP if set, otherwise localhost)
let host = self
.target_addr
.map_or_else(|| "127.0.0.1".to_string(), |ip| ip.to_string());
let addr = format!("{host}:{port}");
match timeout(timeout_dur, tokio::net::TcpStream::connect(&addr)).await {
Ok(Ok(_)) => Ok(()),
Ok(Err(e)) => Err(AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!("TCP connection failed: {e}"),
}),
Err(_) => Err(AgentError::Timeout {
timeout: timeout_dur,
}),
}
}
async fn check_http(
&self,
id: &ContainerId,
url: &str,
expect_status: u16,
timeout_dur: Duration,
) -> Result<()> {
// If a target address is set, replace localhost / 127.0.0.1 in the URL
// so the health check actually reaches the container's overlay IP.
let url = if let Some(ip) = self.target_addr {
let ip_str = ip.to_string();
url.replace("localhost", &ip_str)
.replace("127.0.0.1", &ip_str)
} else {
url.to_string()
};
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.map_err(|e| AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!("failed to create HTTP client: {e}"),
})?;
match timeout(timeout_dur, client.get(&url).send()).await {
Ok(Ok(resp)) => {
let status = resp.status().as_u16();
if status == expect_status {
Ok(())
} else {
Err(AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!("unexpected status: {status} (expected {expect_status})"),
})
}
}
Ok(Err(e)) => Err(AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!("HTTP request failed: {e}"),
}),
Err(_) => Err(AgentError::Timeout {
timeout: timeout_dur,
}),
}
}
async fn check_command(
&self,
id: &ContainerId,
command: &str,
timeout_dur: Duration,
) -> Result<()> {
match timeout(
timeout_dur,
tokio::process::Command::new("sh")
.arg("-c")
.arg(command)
.output(),
)
.await
{
Ok(Ok(output)) => {
if output.status.success() {
Ok(())
} else {
Err(AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!(
"command failed with code {}: {}",
output.status.code().unwrap_or(-1),
String::from_utf8_lossy(&output.stderr)
),
})
}
}
Ok(Err(e)) => Err(AgentError::HealthCheckFailed {
id: id.to_string(),
reason: format!("command execution failed: {e}"),
}),
Err(_) => Err(AgentError::Timeout {
timeout: timeout_dur,
}),
}
}
}
/// Maximum backoff interval when retries are exhausted (60 seconds).
const MAX_BACKOFF: Duration = Duration::from_secs(60);
/// Continuous health monitor
pub struct HealthMonitor {
id: ContainerId,
checker: HealthChecker,
interval: Duration,
retries: u32,
check_timeout: Duration,
start_grace: Duration,
state: tokio::sync::RwLock<HealthState>,
on_health_change: Option<HealthCallback>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HealthState {
Unknown,
Checking,
Healthy,
Unhealthy { failures: u32, reason: String },
}
impl HealthMonitor {
#[must_use]
pub fn new(id: ContainerId, checker: HealthChecker, interval: Duration, retries: u32) -> Self {
Self {
id,
checker,
interval,
retries,
check_timeout: Duration::from_secs(5),
start_grace: Duration::ZERO,
state: tokio::sync::RwLock::new(HealthState::Unknown),
on_health_change: None,
}
}
/// Set a callback to be invoked when health state changes (healthy <-> unhealthy).
#[must_use]
pub fn with_callback(mut self, callback: HealthCallback) -> Self {
self.on_health_change = Some(callback);
self
}
/// Set a startup grace period. The monitor will sleep for this duration
/// before performing the first health check, giving the container time
/// to initialize.
#[must_use]
pub fn with_start_grace(mut self, grace: Duration) -> Self {
self.start_grace = grace;
self
}
/// Set the timeout applied to each individual health check. Defaults to 5 seconds.
#[must_use]
pub fn with_check_timeout(mut self, timeout: Duration) -> Self {
self.check_timeout = timeout;
self
}
/// Start monitoring (spawns background task)
pub fn start(self) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
// Startup grace period — let the container initialize before checking
if !self.start_grace.is_zero() {
tokio::time::sleep(self.start_grace).await;
}
let mut failures = 0u32;
let mut was_healthy: Option<bool> = None;
let mut current_interval = self.interval;
loop {
// Update state to checking
*self.state.write().await = HealthState::Checking;
match self.checker.check(&self.id, self.check_timeout).await {
Ok(()) => {
failures = 0;
current_interval = self.interval;
*self.state.write().await = HealthState::Healthy;
// Check for state transition to healthy
if was_healthy != Some(true) {
if let Some(ref callback) = self.on_health_change {
callback(self.id.clone(), true);
}
was_healthy = Some(true);
}
}
Err(e) => {
failures += 1;
*self.state.write().await = HealthState::Unhealthy {
failures,
reason: e.to_string(),
};
// Check for state transition to unhealthy
if was_healthy != Some(false) {
if let Some(ref callback) = self.on_health_change {
callback(self.id.clone(), false);
}
was_healthy = Some(false);
}
// After exhausting retries, apply exponential backoff
// instead of terminating the monitor
if failures >= self.retries {
current_interval = (current_interval * 2).min(MAX_BACKOFF);
}
}
}
tokio::time::sleep(current_interval).await;
}
})
}
/// Get current health state
pub async fn state(&self) -> HealthState {
self.state.read().await.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_health_state() {
let state = HealthState::Unhealthy {
failures: 3,
reason: "connection refused".to_string(),
};
assert_eq!(
state,
HealthState::Unhealthy {
failures: 3,
reason: "connection refused".to_string()
}
);
}
}