axum: Don't wrap merged service routes in another Route (#3909)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jayakumar Thanakodi · committed by GitHub · parent 83366c2 · Browse files · 3 files·+51 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+51 added−1 removed
- tests:+46 added−0 removed
- other:+5 added−1 removed
Provenance
- Commit
f8b02f2- Tree
ae0a0a3- Author
Jayakumar Thanakodi <grepjava@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
83366c2
3 files changed+51 added−1 removed
modified:axum/src/routing/tests/merge.rs+46 added
1 use super::*;2 use crate::extract::OriginalUri;3 use serde_json::{json, Value};4+use std::sync::Arc;5 use tower::limit::ConcurrencyLimitLayer;6 7 #[crate::test]8 async fn basic() {384 unmodified lines393 StatusCode::NOT_FOUND394 );395 assert_eq!(client.get("/public").await.status(), StatusCode::OK);396 }397+398+#[crate::test]399+async fn merged_service_routes_are_not_wrapped_again() {400+ struct Svc(Arc<AtomicUsize>);401+402+ impl Clone for Svc {403+ fn clone(&self) -> Self {404+ self.0.fetch_add(1, Ordering::SeqCst);405+ Self(self.0.clone())406+ }407+ }408+409+ impl Service<Request> for Svc {410+ type Response = Response;411+ type Error = Infallible;412+ type Future = Ready<Result<Response, Infallible>>;413+414+ fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {415+ Poll::Ready(Ok(()))416+ }417+418+ fn call(&mut self, _req: Request) -> Self::Future {419+ ready(Ok("hello".into_response()))420+ }421+ }422+423+ async fn clones_per_request(app: Router, clones: &AtomicUsize) -> usize {424+ let before = clones.load(Ordering::SeqCst);425+ let res = app426+ .oneshot(Request::get("/svc").body(Body::empty()).unwrap())427+ .await428+ .unwrap();429+ assert_eq!(res.status(), StatusCode::OK);430+ clones.load(Ordering::SeqCst) - before431+ }432+433+ let clones = Arc::new(AtomicUsize::new(0));434+ let direct = Router::new().route_service("/svc", Svc(clones.clone()));435+ let merged = Router::new().merge(Router::new().route_service("/svc", Svc(clones.clone())));436+437+ assert_eq!(438+ clones_per_request(merged, &clones).await,439+ clones_per_request(direct, &clones).await,440+ );441+}modified:axum/src/routing/path_router.rs+2 added-1 removed
122 unmodified lines123 .expect("no path for route id. This is a bug in axum. Please file an issue");124 125 match route {126 Endpoint::MethodRouter(method_router) => self.route(path, method_router)?,127- Endpoint::Route(route) => self.route_service(path, route)?,127+ // `route_service` would wrap this `Route` in another `Route`128+ endpoint @ Endpoint::Route(_) => self.route_endpoint(path, endpoint)?,129 }130 }131 132 Ok(())294 unmodified linesmodified:axum/CHANGELOG.md+3 added
33 unmodified lines34 - **fixed:** Responses to `HEAD` will not accidentally reply with `content-length: 0` anymore ([#3742])35 - **added:** Add `MethodFilter::QUERY`, `routing::query[_service]` and `MethodRouter::query[_service]` ([#3801])36 - **fixed:** `MethodRouter::merge` no longer lists a method twice in the `Allow`37 header after merging `get` and `head` ([#3836])38+- **changed:** `Router::merge` no longer wraps service routes in another `Route`,39+ which saves a clone and a layer of boxing on every request to them ([#3909])40 - **changed:** Update `Router::route_service` docs for `axum::body::Body` instead of41 the removed `BoxBody` type ([#3890])42 43 [#3158]: https://github.com/tokio-rs/axum/pull/31587 unmodified lines51 [#3702]: https://github.com/tokio-rs/axum/pull/370252 [#3721]: https://github.com/tokio-rs/axum/pull/372153 [#3742]: https://github.com/tokio-rs/axum/pull/374254 [#3836]: https://github.com/tokio-rs/axum/pull/383655+[#3909]: https://github.com/tokio-rs/axum/pull/390956 [#3757]: https://github.com/tokio-rs/axum/pull/375757 [#3829]: https://github.com/tokio-rs/axum/pull/382958 [#3801]: https://github.com/tokio-rs/axum/pull/380159 [#3867]: https://github.com/tokio-rs/axum/pull/38672415 unmodified lines