use axum::{
extract::Request,
http::StatusCode,
middleware::Next,
response::{IntoResponse, Response},
};
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
pub(crate) const API_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
type TimeoutFuture = Pin<Box<dyn Future<Output = Response> + Send>>;
pub(crate) fn request_timeout(
duration: Duration,
) -> impl Fn(Request, Next) -> TimeoutFuture + Clone + Send + Sync + 'static {
move |req: Request, next: Next| {
Box::pin(async move {
match tokio::time::timeout(duration, next.run(req)).await {
Ok(res) => res,
Err(_elapsed) => StatusCode::REQUEST_TIMEOUT.into_response(),
}
})
}
}
#[cfg(test)]
#[path = "timeout_tests.rs"]
mod timeout_tests;