Skip to content

Commit a41a417

Browse files
committed
feat(transport): Make service router independent from transport
1 parent 3e5cd1e commit a41a417

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

tonic/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ tls = ["dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "toki
3232
tls-roots = ["tls-roots-common", "dep:rustls-native-certs"]
3333
tls-roots-common = ["tls"]
3434
tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"]
35+
router = ["dep:axum"]
3536
transport = [
37+
"router",
3638
"dep:async-stream",
37-
"dep:axum",
3839
"channel",
3940
"dep:h2",
4041
"dep:hyper", "dep:hyper-util", "dep:hyper-timeout",

tonic/src/service/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! Utilities for using Tower services with Tonic.
22
33
pub mod interceptor;
4+
#[cfg(feature = "router")]
5+
pub(crate) mod router;
46

57
#[doc(inline)]
68
pub use self::interceptor::{interceptor, Interceptor};
9+
#[doc(inline)]
10+
#[cfg(feature = "router")]
11+
pub use self::router::{Routes, RoutesBuilder};

tonic/src/transport/service/router.rs renamed to tonic/src/service/router.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{
22
body::{boxed, BoxBody},
33
server::NamedService,
4-
transport::BoxFuture,
54
};
65
use http::{Request, Response};
76
use pin_project::pin_project;
@@ -82,10 +81,11 @@ impl Routes {
8281
self
8382
}
8483

85-
pub(crate) fn prepare(self) -> Self {
84+
/// This makes axum perform update some internals of the router that improves perf.
85+
///
86+
/// See <https://docs.rs/axum/latest/axum/routing/struct.Router.html#a-note-about-performance>
87+
pub fn prepare(self) -> Self {
8688
Self {
87-
// this makes axum perform update some internals of the router that improves perf
88-
// see https://docs.rs/axum/latest/axum/routing/struct.Router.html#a-note-about-performance
8989
router: self.router.with_state(()),
9090
}
9191
}
@@ -142,6 +142,8 @@ struct AxumBodyService<S> {
142142
service: S,
143143
}
144144

145+
pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
146+
145147
impl<S> Service<Request<axum::body::Body>> for AxumBodyService<S>
146148
where
147149
S: Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>

tonic/src/transport/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ pub use self::server::ServerTlsConfig;
128128
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
129129
pub use self::tls::Identity;
130130

131-
type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
131+
use crate::service::router::BoxFuture;

tonic/src/transport/server/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ mod unix;
1212
use tokio_stream::StreamExt as _;
1313
use tracing::{debug, trace};
1414

15-
pub use super::service::{Routes, RoutesBuilder};
15+
/// A deprecated re-export. Please use `tonic::service::{Routes, RoutesBuilder}` directly.
16+
pub use crate::service::{Routes, RoutesBuilder};
1617

1718
pub use conn::{Connected, TcpConnectInfo};
1819
use hyper_util::rt::{TokioExecutor, TokioIo};

tonic/src/transport/service/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub(crate) mod executor;
66
pub(crate) mod grpc_timeout;
77
mod io;
88
mod reconnect;
9-
mod router;
109
#[cfg(feature = "tls")]
1110
mod tls;
1211
mod user_agent;
@@ -22,6 +21,3 @@ pub(crate) use self::io::ServerIo;
2221
#[cfg(feature = "tls")]
2322
pub(crate) use self::tls::{TlsAcceptor, TlsConnector};
2423
pub(crate) use self::user_agent::UserAgent;
25-
26-
pub use self::router::Routes;
27-
pub use self::router::RoutesBuilder;

0 commit comments

Comments
 (0)