From a41a4170b6ff0f27aabde0f7c771b5cb1676dff0 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 13 Jun 2024 05:59:15 +0900 Subject: [PATCH] feat(transport): Make service router independent from transport --- tonic/Cargo.toml | 3 ++- tonic/src/service/mod.rs | 5 +++++ tonic/src/{transport => }/service/router.rs | 10 ++++++---- tonic/src/transport/mod.rs | 2 +- tonic/src/transport/server/mod.rs | 3 ++- tonic/src/transport/service/mod.rs | 4 ---- 6 files changed, 16 insertions(+), 11 deletions(-) rename tonic/src/{transport => }/service/router.rs (93%) diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml index b795cc5f9..74df3a583 100644 --- a/tonic/Cargo.toml +++ b/tonic/Cargo.toml @@ -32,9 +32,10 @@ tls = ["dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "toki tls-roots = ["tls-roots-common", "dep:rustls-native-certs"] tls-roots-common = ["tls"] tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"] +router = ["dep:axum"] transport = [ + "router", "dep:async-stream", - "dep:axum", "channel", "dep:h2", "dep:hyper", "dep:hyper-util", "dep:hyper-timeout", diff --git a/tonic/src/service/mod.rs b/tonic/src/service/mod.rs index c9aad52be..36407885a 100644 --- a/tonic/src/service/mod.rs +++ b/tonic/src/service/mod.rs @@ -1,6 +1,11 @@ //! Utilities for using Tower services with Tonic. pub mod interceptor; +#[cfg(feature = "router")] +pub(crate) mod router; #[doc(inline)] pub use self::interceptor::{interceptor, Interceptor}; +#[doc(inline)] +#[cfg(feature = "router")] +pub use self::router::{Routes, RoutesBuilder}; diff --git a/tonic/src/transport/service/router.rs b/tonic/src/service/router.rs similarity index 93% rename from tonic/src/transport/service/router.rs rename to tonic/src/service/router.rs index c43782ba9..bc4f78553 100644 --- a/tonic/src/transport/service/router.rs +++ b/tonic/src/service/router.rs @@ -1,7 +1,6 @@ use crate::{ body::{boxed, BoxBody}, server::NamedService, - transport::BoxFuture, }; use http::{Request, Response}; use pin_project::pin_project; @@ -82,10 +81,11 @@ impl Routes { self } - pub(crate) fn prepare(self) -> Self { + /// This makes axum perform update some internals of the router that improves perf. + /// + /// See + pub 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(()), } } @@ -142,6 +142,8 @@ struct AxumBodyService { service: S, } +pub(crate) type BoxFuture<'a, T> = Pin + Send + 'a>>; + impl Service> for AxumBodyService where S: Service, Response = Response, Error = Infallible> diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index 767534748..f29fd1c99 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -128,4 +128,4 @@ pub use self::server::ServerTlsConfig; #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] pub use self::tls::Identity; -type BoxFuture<'a, T> = std::pin::Pin + Send + 'a>>; +use crate::service::router::BoxFuture; diff --git a/tonic/src/transport/server/mod.rs b/tonic/src/transport/server/mod.rs index 3fa406c77..06a088d51 100644 --- a/tonic/src/transport/server/mod.rs +++ b/tonic/src/transport/server/mod.rs @@ -12,7 +12,8 @@ mod unix; use tokio_stream::StreamExt as _; use tracing::{debug, trace}; -pub use super::service::{Routes, RoutesBuilder}; +/// A deprecated re-export. Please use `tonic::service::{Routes, RoutesBuilder}` directly. +pub use crate::service::{Routes, RoutesBuilder}; pub use conn::{Connected, TcpConnectInfo}; use hyper_util::rt::{TokioExecutor, TokioIo}; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 2b2a84070..eeae37f5d 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -6,7 +6,6 @@ pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; mod reconnect; -mod router; #[cfg(feature = "tls")] mod tls; mod user_agent; @@ -22,6 +21,3 @@ pub(crate) use self::io::ServerIo; #[cfg(feature = "tls")] pub(crate) use self::tls::{TlsAcceptor, TlsConnector}; pub(crate) use self::user_agent::UserAgent; - -pub use self::router::Routes; -pub use self::router::RoutesBuilder;