Skip to content

Commit

Permalink
feat(transport): Make service router independent from transport (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Jun 13, 2024
1 parent 68bf17d commit da48235
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions tonic/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
body::{boxed, BoxBody},
server::NamedService,
transport::BoxFuture,
};
use http::{Request, Response};
use pin_project::pin_project;
Expand Down Expand Up @@ -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 <https://docs.rs/axum/latest/axum/routing/struct.Router.html#a-note-about-performance>
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(()),
}
}
Expand Down Expand Up @@ -142,6 +142,8 @@ struct AxumBodyService<S> {
service: S,
}

pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

impl<S> Service<Request<axum::body::Body>> for AxumBodyService<S>
where
S: Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
Expand Down
2 changes: 1 addition & 1 deletion tonic/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
use crate::service::router::BoxFuture;
3 changes: 2 additions & 1 deletion tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 0 additions & 4 deletions tonic/src/transport/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

0 comments on commit da48235

Please sign in to comment.