use std::time::Duration;
use axum::response::Response;
use crate::web::error::{GatewayTimeout, HttpException};
#[doc(hidden)]
pub async fn run_with_timeout<F>(millis: u64, route: &'static str, fut: F) -> Response
where
F: std::future::Future<Output = Response>,
{
match tokio::time::timeout(Duration::from_millis(millis), fut).await {
Ok(resp) => resp,
Err(_elapsed) => {
metrics::counter!("handler_timeouts_total", "route" => route).increment(1);
crate::http::IntoResponse::into_response(HttpException::from(GatewayTimeout::new(
"handler exceeded its deadline",
)))
}
}
}
pub fn parse_duration_millis(s: &str) -> Option<u64> {
let s = s.trim();
if let Some(v) = s.strip_suffix("ms") {
return v.trim().parse().ok();
}
if let Some(v) = s.strip_suffix('s') {
return v.trim().parse::<u64>().ok().map(|n| n * 1_000);
}
if let Some(v) = s.strip_suffix('m') {
return v.trim().parse::<u64>().ok().map(|n| n * 60_000);
}
s.parse().ok()
}