Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally Arc Router, without breaking changes #2483

Merged
merged 13 commits into from
Jan 13, 2024
3 changes: 3 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased

- **fixed:** Improve `debug_handler` on tuple response types ([#2201])
- **fixed:** Fix performance regression present since axum 0.7.0 ([#2483])
- **added:** Add `must_use` attribute to `Serve` and `WithGracefulShutdown` ([#2484])
- **added:** Re-export `axum_core::body::BodyDataStream` from axum

[#2201]: https://github.com/tokio-rs/axum/pull/2201
[#2483]: https://github.com/tokio-rs/axum/pull/2483
[#2201]: https://github.com/tokio-rs/axum/pull/2201
[#2484]: https://github.com/tokio-rs/axum/pull/2484

Expand Down
3 changes: 3 additions & 0 deletions axum/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disallowed-types = [
{ path = "std::sync::Mutex", reason = "Use our internal AxumMutex instead" },
]
13 changes: 7 additions & 6 deletions axum/src/boxed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{convert::Infallible, fmt, sync::Mutex};
use std::{convert::Infallible, fmt};

use crate::extract::Request;
use crate::util::AxumMutex;
use tower::Service;

use crate::{
Expand All @@ -9,7 +10,7 @@ use crate::{
Router,
};

pub(crate) struct BoxedIntoRoute<S, E>(Mutex<Box<dyn ErasedIntoRoute<S, E>>>);
pub(crate) struct BoxedIntoRoute<S, E>(AxumMutex<Box<dyn ErasedIntoRoute<S, E>>>);

impl<S> BoxedIntoRoute<S, Infallible>
where
Expand All @@ -20,7 +21,7 @@ where
H: Handler<T, S>,
T: 'static,
{
Self(Mutex::new(Box::new(MakeErasedHandler {
Self(AxumMutex::new(Box::new(MakeErasedHandler {
handler,
into_route: |handler, state| Route::new(Handler::with_state(handler, state)),
})))
Expand All @@ -35,7 +36,7 @@ impl<S, E> BoxedIntoRoute<S, E> {
F: FnOnce(Route<E>) -> Route<E2> + Clone + Send + 'static,
E2: 'static,
{
BoxedIntoRoute(Mutex::new(Box::new(Map {
BoxedIntoRoute(AxumMutex::new(Box::new(Map {
inner: self.0.into_inner().unwrap(),
layer: Box::new(f),
})))
Expand All @@ -48,7 +49,7 @@ impl<S, E> BoxedIntoRoute<S, E> {

impl<S, E> Clone for BoxedIntoRoute<S, E> {
fn clone(&self) -> Self {
Self(Mutex::new(self.0.lock().unwrap().clone_box()))
Self(AxumMutex::new(self.0.lock().unwrap().clone_box()))
}
}

Expand Down Expand Up @@ -118,7 +119,7 @@ where
(self.into_route)(self.router, state)
}

fn call_with_state(mut self: Box<Self>, request: Request, state: S) -> RouteFuture<Infallible> {
fn call_with_state(self: Box<Self>, request: Request, state: S) -> RouteFuture<Infallible> {
self.router.call_with_state(request, state)
}
}
Expand Down
12 changes: 6 additions & 6 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ where
self
}

pub(crate) fn call_with_state(&mut self, req: Request, state: S) -> RouteFuture<E> {
pub(crate) fn call_with_state(&self, req: Request, state: S) -> RouteFuture<E> {
macro_rules! call {
(
$req:expr,
Expand All @@ -1034,12 +1034,12 @@ where
match $svc {
MethodEndpoint::None => {}
MethodEndpoint::Route(route) => {
return RouteFuture::from_future(route.oneshot_inner($req))
return RouteFuture::from_future(route.clone().oneshot_inner($req))
.strip_body($method == Method::HEAD);
}
MethodEndpoint::BoxedHandler(handler) => {
let mut route = handler.clone().into_route(state);
return RouteFuture::from_future(route.oneshot_inner($req))
let route = handler.clone().into_route(state);
return RouteFuture::from_future(route.clone().oneshot_inner($req))
.strip_body($method == Method::HEAD);
}
}
Expand Down Expand Up @@ -1073,7 +1073,7 @@ where
call!(req, method, DELETE, delete);
call!(req, method, TRACE, trace);

let future = fallback.call_with_state(req, state);
let future = fallback.clone().call_with_state(req, state);

match allow_header {
AllowHeader::None => future.allow_header(Bytes::new()),
Expand Down Expand Up @@ -1219,7 +1219,7 @@ where
{
type Future = InfallibleRouteFuture;

fn call(mut self, req: Request, state: S) -> Self::Future {
fn call(self, req: Request, state: S) -> Self::Future {
InfallibleRouteFuture::new(self.call_with_state(req, state))
}
}
Expand Down
Loading