Skip to content
Myr

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

3 files+51 added−1 removed

modified:axum/src/routing/tests/merge.rs+46 added
1use super::*;2use crate::extract::OriginalUri;3use serde_json::{json, Value};4use std::sync::Arc;5use tower::limit::ConcurrencyLimitLayer;67#[crate::test]8async fn basic() {
384 unmodified lines
393 StatusCode::NOT_FOUND394 );395 assert_eq!(client.get("/public").await.status(), StatusCode::OK);396}397398#[crate::test]399async fn merged_service_routes_are_not_wrapped_again() {400 struct Svc(Arc<AtomicUsize>);401402 impl Clone for Svc {403 fn clone(&self) -> Self {404 self.0.fetch_add(1, Ordering::SeqCst);405 Self(self.0.clone())406 }407 }408409 impl Service<Request> for Svc {410 type Response = Response;411 type Error = Infallible;412 type Future = Ready<Result<Response, Infallible>>;413414 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {415 Poll::Ready(Ok(()))416 }417418 fn call(&mut self, _req: Request) -> Self::Future {419 ready(Ok("hello".into_response()))420 }421 }422423 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 }432433 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())));436437 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 lines
123 .expect("no path for route id. This is a bug in axum. Please file an issue");124125 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 }131132 Ok(())
294 unmodified lines
modified:axum/CHANGELOG.md+3 added
33 unmodified lines
34- **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])4243[#3158]: https://github.com/tokio-rs/axum/pull/3158
7 unmodified lines
51[#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/3867
2415 unmodified lines