Files
continuwuity/src/router/request.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
4.3 KiB
Rust
Raw Normal View History

2025-02-01 23:41:05 +00:00
use std::{
fmt::Debug,
sync::{Arc, atomic::Ordering},
2026-09-24 10:44:15 +02:00
time::{Duration, Instant},
2025-02-01 23:41:05 +00:00
};
2024-05-09 15:59:08 -07:00
use axum::{
2026-09-24 10:44:15 +02:00
extract::{MatchedPath, State},
response::{IntoResponse, Response},
};
2026-09-24 10:44:15 +02:00
use conduwuit::{
Result, debug, debug_error, debug_warn, err, error,
metrics::{KeyValue, method_label},
2026-09-24 10:44:15 +02:00
trace,
};
2024-12-14 21:58:01 -05:00
use conduwuit_service::Services;
2025-02-02 10:43:02 +00:00
use futures::FutureExt;
2024-05-09 15:59:08 -07:00
use http::{Method, StatusCode, Uri};
2025-02-02 10:43:02 +00:00
use tokio::time::sleep;
2025-02-02 10:07:00 +00:00
use tracing::Span;
2024-05-09 15:59:08 -07:00
2026-09-24 10:44:15 +02:00
use crate::layers::truncated_matched_path;
2025-02-02 23:19:35 +00:00
#[tracing::instrument(name = "request", level = "debug", skip_all)]
2024-05-09 15:59:08 -07:00
pub(crate) async fn handle(
State(services): State<Arc<Services>>,
req: http::Request<axum::body::Body>,
next: axum::middleware::Next,
) -> Result<Response, StatusCode> {
2025-01-30 09:28:34 +00:00
if !services.server.running() {
2024-05-09 15:59:08 -07:00
debug_warn!(
method = %req.method(),
uri = %req.uri(),
"unavailable pending shutdown"
);
return Err(StatusCode::SERVICE_UNAVAILABLE);
}
let instruments = &services.server.metrics.instruments;
let method_attr = KeyValue::new("http.request.method", method_label(req.method()));
instruments
.http_server_active_requests
.add(1, std::slice::from_ref(&method_attr));
conduwuit::defer! {{
instruments
.http_server_active_requests
.add(-1, std::slice::from_ref(&method_attr));
}};
2026-09-24 10:44:15 +02:00
let start = Instant::now();
let route = req
.extensions()
.get::<MatchedPath>()
.map(|path| KeyValue::new("http.route", truncated_matched_path(path).to_owned()));
2025-01-30 09:28:34 +00:00
let uri = req.uri().clone();
2025-02-01 23:41:05 +00:00
let method = req.method().clone();
let services_ = services.clone();
2025-02-02 10:07:00 +00:00
let parent = Span::current();
let task = services.server.runtime().spawn(async move {
tokio::select! {
response = execute(&services_, req, next, &parent) => response,
2025-02-02 10:43:02 +00:00
response = services_.server.until_shutdown()
.then(|()| {
let timeout = services_.server.config.client_shutdown_timeout;
let timeout = Duration::from_secs(timeout);
sleep(timeout)
})
.map(|()| StatusCode::SERVICE_UNAVAILABLE)
.map(IntoResponse::into_response) => response,
2025-02-02 10:07:00 +00:00
}
});
2025-02-01 23:41:05 +00:00
2026-09-24 10:44:15 +02:00
let result = task
.await
2025-02-01 23:41:05 +00:00
.map_err(unhandled)
2026-09-24 10:44:15 +02:00
.and_then(|result| handle_result(&method, &uri, result));
let status = result
.as_ref()
.map_or_else(|status| *status, Response::status);
let mut attributes = vec![
method_attr.clone(),
KeyValue::new("http.response.status_code", i64::from(status.as_u16())),
];
attributes.extend(route);
if status.is_server_error() {
attributes.push(KeyValue::new("error.type", status.as_str().to_owned()));
}
instruments
.http_server_request_duration
.record(start.elapsed().as_secs_f64(), &attributes);
2026-09-24 10:44:15 +02:00
result
2025-02-01 23:41:05 +00:00
}
2025-02-02 10:07:00 +00:00
#[tracing::instrument(
name = "handle",
level = "debug",
parent = parent,
skip_all,
2025-02-02 23:19:35 +00:00
fields(
active = %services
.server
.metrics
.requests_handle_active
.fetch_add(1, Ordering::Relaxed),
handled = %services
.server
.metrics
.requests_handle_finished
.load(Ordering::Relaxed),
)
2025-02-02 10:07:00 +00:00
)]
2025-02-01 23:41:05 +00:00
async fn execute(
// we made a safety contract that Services will not go out of scope
// during the request; this ensures a reference is accounted for at
// the base frame of the task regardless of its detachment.
2025-02-02 23:19:35 +00:00
services: &Arc<Services>,
2025-02-01 23:41:05 +00:00
req: http::Request<axum::body::Body>,
next: axum::middleware::Next,
parent: &Span,
2025-02-01 23:41:05 +00:00
) -> Response {
2025-02-02 23:19:35 +00:00
#[cfg(debug_assertions)]
conduwuit::defer! {{
_ = services.server
.metrics
.requests_handle_finished
.fetch_add(1, Ordering::Relaxed);
_ = services.server
.metrics
.requests_handle_active
.fetch_sub(1, Ordering::Relaxed);
}};
2025-02-01 23:41:05 +00:00
next.run(req).await
2024-05-09 15:59:08 -07:00
}
fn handle_result(method: &Method, uri: &Uri, result: Response) -> Result<Response, StatusCode> {
2024-05-09 15:59:08 -07:00
let status = result.status();
let code = status.as_u16();
let reason = status.canonical_reason().unwrap_or("Unknown Reason");
2025-02-01 23:41:05 +00:00
2024-05-09 15:59:08 -07:00
if status.is_server_error() {
2026-01-04 03:04:37 +00:00
error!(%method, %uri, "{code} {reason}");
2024-05-09 15:59:08 -07:00
} else if status.is_client_error() {
2026-01-04 03:04:37 +00:00
debug_error!(%method, %uri, "{code} {reason}");
2024-05-09 15:59:08 -07:00
} else if status.is_redirection() {
2026-01-04 03:04:37 +00:00
debug!(%method, %uri, "{code} {reason}");
2024-05-09 15:59:08 -07:00
} else {
2026-01-04 03:04:37 +00:00
trace!(%method, %uri, "{code} {reason}");
2024-05-09 15:59:08 -07:00
}
if status == StatusCode::METHOD_NOT_ALLOWED {
return Ok(
err!(Request(Unrecognized("Method not allowed"), METHOD_NOT_ALLOWED)).into_response()
);
}
Ok(result)
2024-05-09 15:59:08 -07:00
}
2025-02-01 23:41:05 +00:00
#[cold]
fn unhandled<Error: Debug>(e: Error) -> StatusCode {
error!("unhandled error or panic during request: {e:?}");
StatusCode::INTERNAL_SERVER_ERROR
}