Skip to content

Commit

Permalink
Update to axum 0.6.0
Browse files Browse the repository at this point in the history
Draft until axum is out of release candidate mode.
  • Loading branch information
davidpdrsn committed Nov 24, 2022
1 parent 933f560 commit 8295b44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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 = {git = "https://github.com/tokio-rs/axum", 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
14 changes: 11 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,18 @@ 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
router: self.router.with_state(()),
}
}
}

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

0 comments on commit 8295b44

Please sign in to comment.