nixfleet_agent/runtime/workers/probe_runners/
tcp.rs

1//! TCP probe runner (RFC-0007 ยง3.1). Pass iff `connect_timeout_secs`
2//! TCP connect succeeds against `host:port`. `host` defaults to
3//! `127.0.0.1` if absent.
4
5use chrono::{DateTime, Utc};
6use std::time::Duration;
7use tokio::net::TcpStream;
8use tokio::time::timeout;
9
10use super::{ProbeDecl, RunnerOutcome};
11
12pub async fn run(decl: &ProbeDecl, now: DateTime<Utc>) -> RunnerOutcome {
13    let Some(port) = decl.port else {
14        return RunnerOutcome::fail(now, "tcp probe: port missing");
15    };
16    let host = decl.host.as_deref().unwrap_or("127.0.0.1");
17    let addr = format!("{host}:{port}");
18    let connect = TcpStream::connect(&addr);
19    match timeout(Duration::from_secs(decl.connect_timeout_secs), connect).await {
20        Ok(Ok(_stream)) => RunnerOutcome::pass(now),
21        Ok(Err(err)) => RunnerOutcome::fail(now, format!("tcp probe: connect {addr}: {err}")),
22        Err(_elapsed) => RunnerOutcome::fail(
23            now,
24            format!(
25                "tcp probe: connect {addr} timed out after {}s",
26                decl.connect_timeout_secs
27            ),
28        ),
29    }
30}