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

chore: update to axum 0.6.0 #1155

Merged
merged 3 commits into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tonic/Cargo.toml
Expand Up @@ -81,7 +81,7 @@ tokio = {version = "1.0.1", features = ["net"], optional = true}
tokio-stream = "0.1"
tower = {version = "0.4.7", default-features = false, features = ["balance", "buffer", "discover", "limit", "load", "make", "timeout", "util"], optional = true}
tracing-futures = {version = "0.2", optional = true}
axum = {version = "0.5.16", default_features = false, optional = true}
axum = {version = "0.6", default_features = false, optional = true}

# rustls
rustls-pemfile = { version = "1.0", optional = true }
Expand Down
10 changes: 5 additions & 5 deletions tonic/src/transport/server/mod.rs
Expand Up @@ -589,7 +589,7 @@ impl<L> Router<L> {
.map_err(super::Error::from_source)?;
self.server
.serve_with_shutdown::<_, _, future::Ready<()>, _, _, ResBody>(
self.routes,
self.routes.prepare(),
incoming,
None,
)
Expand Down Expand Up @@ -618,7 +618,7 @@ impl<L> Router<L> {
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
.map_err(super::Error::from_source)?;
self.server
.serve_with_shutdown(self.routes, incoming, Some(signal))
.serve_with_shutdown(self.routes.prepare(), incoming, Some(signal))
.await
}

Expand All @@ -644,7 +644,7 @@ impl<L> Router<L> {
{
self.server
.serve_with_shutdown::<_, _, future::Ready<()>, _, _, ResBody>(
self.routes,
self.routes.prepare(),
incoming,
None,
)
Expand Down Expand Up @@ -676,7 +676,7 @@ impl<L> Router<L> {
ResBody::Error: Into<crate::Error>,
{
self.server
.serve_with_shutdown(self.routes, incoming, Some(signal))
.serve_with_shutdown(self.routes.prepare(), incoming, Some(signal))
.await
}

Expand All @@ -690,7 +690,7 @@ impl<L> Router<L> {
ResBody: http_body::Body<Data = Bytes> + Send + 'static,
ResBody::Error: Into<crate::Error>,
{
self.server.service_builder.service(self.routes)
self.server.service_builder.service(self.routes.prepare())
}
}

Expand Down
15 changes: 12 additions & 3 deletions tonic/src/transport/service/router.rs
Expand Up @@ -2,7 +2,6 @@ use crate::{
body::{boxed, BoxBody},
transport::NamedService,
};
use axum::handler::Handler;
use http::{Request, Response};
use hyper::Body;
use pin_project::pin_project;
Expand Down Expand Up @@ -33,7 +32,7 @@ impl Routes {
S::Future: Send + 'static,
S::Error: Into<crate::Error> + Send,
{
let router = axum::Router::new().fallback(unimplemented.into_service());
let router = axum::Router::new().fallback(unimplemented);
Self { router }.add_service(svc)
}

Expand All @@ -48,9 +47,19 @@ impl Routes {
S::Error: Into<crate::Error> + Send,
{
let svc = svc.map_response(|res| res.map(axum::body::boxed));
self.router = self.router.route(&format!("/{}/*rest", S::NAME), svc);
self.router = self
.router
.route_service(&format!("/{}/*rest", S::NAME), svc);
self
}

pub(crate) fn prepare(self) -> Self {
Self {
// this makes axum perform update some internals of the router that improves perf
// see https://docs.rs/axum/latest/axum/routing/struct.Router.html#a-note-about-performance
router: self.router.with_state(()),
}
}
}

async fn unimplemented() -> impl axum::response::IntoResponse {
Expand Down