agentmux_srv_core/connectivity/
probe.rs1use std::future::Future;
11use std::pin::Pin;
12use std::sync::Arc;
13use std::time::Duration;
14
15use super::{Outcome, Service, Shared};
16
17pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
19
20pub trait Prober: Send + Sync + 'static {
22 fn probe(&self, service: Service) -> Pin<Box<dyn Future<Output = Outcome> + Send>>;
23}
24
25const PROBE_TIMEOUT: Duration = Duration::from_secs(15);
28const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
29const HEAD_TIMEOUT: Duration = Duration::from_secs(10);
30
31pub(super) async fn drive(shared: Arc<Shared>, prober: Arc<dyn Prober>, clock: Clock) {
35 loop {
36 let due = shared.tracker().take_due_probes(clock());
37 for (service, ticket) in due {
38 let (shared, prober, clock) = (shared.clone(), prober.clone(), clock.clone());
39 tokio::spawn(async move {
40 let outcome = tokio::time::timeout(PROBE_TIMEOUT, prober.probe(service.clone()))
41 .await
42 .unwrap_or_else(|_| Outcome::Unreachable("probe timed out".to_string()));
43 tracing::debug!(service = %service.id(), ?outcome, "connectivity: probed");
44 let changed = shared.tracker().probe_finished(&service, ticket, outcome, clock());
45 shared.publish(changed);
46 shared.wake.notify_one();
48 });
49 }
50 let next = shared.tracker().next_probe_due();
51 match next {
52 Some(due_ms) => {
53 let wait = Duration::from_millis(u64::try_from(due_ms - clock()).unwrap_or(0));
54 tokio::select! {
55 _ = tokio::time::sleep(wait) => {}
56 _ = shared.wake.notified() => {}
57 }
58 }
59 None => shared.wake.notified().await,
60 }
61 }
62}
63
64fn proxy_configured() -> bool {
68 ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy"]
69 .iter()
70 .any(|var| std::env::var(var).is_ok_and(|v| !v.trim().is_empty()))
71}
72
73pub(super) struct NetProber {
76 http: reqwest::Client,
77}
78
79impl NetProber {
80 pub(super) fn new() -> Self {
81 let http = crate::http_client::builder()
82 .user_agent(crate::muxbus::relay::USER_AGENT)
83 .connect_timeout(CONNECT_TIMEOUT)
84 .timeout(HEAD_TIMEOUT)
85 .build()
86 .unwrap_or_else(|_| crate::http_client::client());
87 Self { http }
88 }
89}
90
91async fn probe_url(http: reqwest::Client, url: String) -> Outcome {
92 let parsed = match reqwest::Url::parse(&url) {
93 Ok(u) => u,
94 Err(e) => return Outcome::Unreachable(format!("bad address {url}: {e}")),
95 };
96 if !proxy_configured() {
97 let (Some(host), Some(port)) = (parsed.host_str(), parsed.port_or_known_default()) else {
98 return Outcome::Unreachable(format!("bad address {url}"));
99 };
100 match tokio::time::timeout(CONNECT_TIMEOUT, tokio::net::TcpStream::connect((host, port))).await {
101 Ok(Ok(_)) => {}
102 Ok(Err(e)) => return Outcome::Unreachable(e.to_string()),
103 Err(_) => return Outcome::Unreachable("connect timed out".to_string()),
104 }
105 }
106 super::http_outcome(&http.head(parsed).send().await)
107 .unwrap_or_else(|| Outcome::Unreachable("the probe request couldn't be built".to_string()))
108}
109
110impl Prober for NetProber {
111 fn probe(&self, service: Service) -> Pin<Box<dyn Future<Output = Outcome> + Send>> {
112 Box::pin(probe_url(self.http.clone(), service.probe_url()))
113 }
114}
115
116#[cfg(test)]
117#[path = "probe_tests.rs"]
118mod tests;