From 0945556a991e5f67e060d3f170ae05a42e32df8e Mon Sep 17 00:00:00 2001 From: tottoto Date: Sat, 18 Nov 2023 08:10:14 +0900 Subject: [PATCH] feat(transport): Make service router independent from transport --- tonic/Cargo.toml | 13 +++++++------ tonic/src/service/mod.rs | 5 +++++ tonic/src/{transport => }/service/router.rs | 1 + tonic/src/transport/server/mod.rs | 4 ++-- tonic/src/transport/service/mod.rs | 5 ++--- 5 files changed, 17 insertions(+), 11 deletions(-) rename tonic/src/{transport => }/service/router.rs (99%) diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml index 99388e357..89c442d9d 100644 --- a/tonic/Cargo.toml +++ b/tonic/Cargo.toml @@ -32,14 +32,15 @@ tls = ["dep:rustls-pki-types", "dep:rustls-pemfile", "transport", "dep:tokio-rus tls-roots = ["tls-roots-common", "dep:rustls-native-certs"] tls-roots-common = ["tls"] tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"] +router = ["dep:hyper", "dep:axum", "dep:tower"] transport = [ - "dep:async-stream", - "dep:axum", + "router", "channel", + "dep:async-stream", "dep:h2", - "dep:hyper", + "dep:hyper", "hyper?/full", "dep:tokio", "tokio?/net", "tokio?/time", - "dep:tower", + "dep:tower", "tower?/balance", "tower?/buffer", "tower?/discover", "tower?/limit", "tower?/load", "tower?/make", "tower?/timeout", "tower?/util", "dep:hyper-timeout", ] channel = [] @@ -68,11 +69,11 @@ async-trait = {version = "0.1.13", optional = true} # transport h2 = {version = "0.3.24", optional = true} -hyper = {version = "0.14.26", features = ["full"], optional = true} +hyper = {version = "0.14.26", optional = true} hyper-timeout = {version = "0.4", optional = true} tokio = {version = "1.0.1", 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} +tower = {version = "0.4.7", default-features = false, optional = true} axum = {version = "0.6.9", default_features = false, optional = true} # rustls diff --git a/tonic/src/service/mod.rs b/tonic/src/service/mod.rs index c9aad52be..d23f9cfee 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")] +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 99% rename from tonic/src/transport/service/router.rs rename to tonic/src/service/router.rs index 85636c4d4..1aca4758b 100644 --- a/tonic/src/transport/service/router.rs +++ b/tonic/src/service/router.rs @@ -83,6 +83,7 @@ impl Routes { self } + #[cfg(feature = "transport")] pub(crate) fn prepare(self) -> Self { Self { // this makes axum perform update some internals of the router that improves perf diff --git a/tonic/src/transport/server/mod.rs b/tonic/src/transport/server/mod.rs index 7f2ffde2b..7bddf40a8 100644 --- a/tonic/src/transport/server/mod.rs +++ b/tonic/src/transport/server/mod.rs @@ -9,8 +9,8 @@ mod tls; #[cfg(unix)] mod unix; -pub use super::service::Routes; -pub use super::service::RoutesBuilder; +/// A deprecated re-export. Please use `tonic::service::{Routes, RoutesBuilder}` directly. +pub use super::service::{Routes, RoutesBuilder}; pub use conn::{Connected, TcpConnectInfo}; #[cfg(feature = "tls")] diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 69d850f10..06ade78ac 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,5 +21,5 @@ pub(crate) use self::io::ServerIo; pub(crate) use self::tls::{TlsAcceptor, TlsConnector}; pub(crate) use self::user_agent::UserAgent; -pub use self::router::Routes; -pub use self::router::RoutesBuilder; +/// A deprecated re-export. Please use `tonic::service::{Routes, RoutesBuilder}` directly. +pub use crate::service::{Routes, RoutesBuilder};