From 005f635fab3f456b1d1a9c93b3c6e85c89dd5dcf Mon Sep 17 00:00:00 2001 From: nate Date: Sat, 28 Mar 2026 17:15:12 +0400 Subject: [PATCH] fix: improve monitor --- apps/monitor/src/runner.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/apps/monitor/src/runner.rs b/apps/monitor/src/runner.rs index 27640a5..d83aa7a 100644 --- a/apps/monitor/src/runner.rs +++ b/apps/monitor/src/runner.rs @@ -142,7 +142,6 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op let method = monitor.method.as_deref().unwrap_or("GET").to_uppercase(); let timeout = std::time::Duration::from_millis(monitor.timeout_ms.unwrap_or(30000)); let is_https = monitor.url.starts_with("https://"); - let url_for_cert = monitor.url.clone(); // Run the check in a real OS thread using ureq (blocking, synchronous HTTP). // ureq sets SO_RCVTIMEO/SO_SNDTIMEO at the socket level, which reliably @@ -151,12 +150,11 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op let url = monitor.url.clone(); let req_headers = monitor.request_headers.clone(); let req_body = monitor.request_body.clone(); - let method_clone = method.clone(); let (tx, rx) = tokio::sync::oneshot::channel::, String), String>>(); std::thread::spawn(move || { let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - run_check_blocking(&url, &method_clone, req_headers.as_ref(), req_body.as_deref(), timeout) + run_check_blocking(&url, &method, req_headers.as_ref(), req_body.as_deref(), timeout) })); let _ = tx.send(match result { Ok(r) => r, @@ -164,7 +162,7 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op }); }); - let curl_result = tokio::time::timeout(timeout + std::time::Duration::from_secs(2), rx) + let result = tokio::time::timeout(timeout + std::time::Duration::from_secs(2), rx) .await .map_err(|_| format!("timed out after {}ms", timeout.as_millis())) .and_then(|r| r.map_err(|_| "check thread dropped".to_string())) @@ -172,8 +170,6 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op let latency_ms = start.elapsed().as_millis() as u64; - let result = curl_result; - match result { Err(ref e) => { debug!("{} check error: {e}", monitor.url); @@ -192,17 +188,17 @@ async fn run_check(client: &reqwest::Client, monitor: &Monitor, scheduled_at: Op run_id: Some(run_id.to_string()), } }, - Ok((status_code, headers, body)) => { - let status = status_code; + Ok((status, headers, body)) => { // Start cert expiry check in background — don't block result posting. // We'll use None for cert_expiry_days in query evaluation since it // shouldn't delay the main result by seconds of extra TLS handshake. let cert_handle = if is_https { + let cert_url = monitor.url.clone(); Some(tokio::spawn(async move { match tokio::time::timeout( std::time::Duration::from_secs(5), - check_cert_expiry(&url_for_cert), + check_cert_expiry(&cert_url), ).await { Ok(Ok(days)) => days, _ => None,