From f6008c173c358f87bc814d9cac25fcc5aa1b5208 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 26 Apr 2025 23:21:15 +0200 Subject: [PATCH] serve: Extract prep_serve_connection out of handle_connection --- axum/CHANGELOG.md | 4 ++++ axum/src/serve/mod.rs | 38 +++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 9d4f5d78..7d653677 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **breaking:** `serve` now requires the listener IO type to be `Sync` + +# Unreleased + - **added:** `Router::reset_fallback` ([#3320]) [#3320]: https://github.com/tokio-rs/axum/pull/3320 diff --git a/axum/src/serve/mod.rs b/axum/src/serve/mod.rs index ca6c39af..ec7ab49a 100644 --- a/axum/src/serve/mod.rs +++ b/axum/src/serve/mod.rs @@ -222,6 +222,7 @@ impl IntoFuture for Serve where L: Listener, L::Addr: Debug, + L::Io: Sync, M: for<'a> Service, Error = Infallible, Response = S> + Send + 'static, for<'a> >>::Future: Send, S: Service + Clone + Send + 'static, @@ -336,6 +337,7 @@ impl IntoFuture for WithGracefulShutdown where L: Listener, L::Addr: Debug, + L::Io: Sync, M: for<'a> Service, Error = Infallible, Response = S> + Send + 'static, for<'a> >>::Future: Send, S: Service + Clone + Send + 'static, @@ -366,6 +368,26 @@ async fn handle_connection( { let io = TokioIo::new(io); + let conn_service = prep_serve_connection(make_service, remote_addr, &io).await; + tokio::spawn(serve_connection( + io, + conn_service, + signal_tx.clone(), + close_rx.clone(), + )); +} + +async fn prep_serve_connection( + make_service: &mut M, + remote_addr: ::Addr, + io: &TokioIo<::Io>, +) -> S +where + L: Listener, + L::Addr: Debug, + M: for<'a> Service, Error = Infallible, Response = S> + Send + 'static, + for<'a> >>::Future: Send, +{ trace!("connection {remote_addr:?} accepted"); make_service @@ -373,20 +395,10 @@ async fn handle_connection( .await .unwrap_or_else(|err| match err {}); - let conn_service = make_service - .call(IncomingStream { - io: &io, - remote_addr, - }) + make_service + .call(IncomingStream { io, remote_addr }) .await - .unwrap_or_else(|err| match err {}); - - tokio::spawn(serve_connection( - io, - conn_service, - signal_tx.clone(), - close_rx.clone(), - )); + .unwrap_or_else(|err| match err {}) } async fn serve_connection(