fix: improve monitor

This commit is contained in:
nate 2026-03-28 17:15:12 +04:00
parent de5f7580a6
commit 005f635fab
1 changed files with 5 additions and 9 deletions

View File

@ -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::<Result<(u16, HashMap<String, String>, 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,