From b2968bc6993439affa28e4b900784a082f0d3243 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 06:56:41 +0900 Subject: [PATCH 01/14] feat(channel): Make channel feature additive --- examples/Cargo.toml | 4 ++-- tonic/Cargo.toml | 27 +++++++++++++++++---------- tonic/src/status.rs | 2 +- tonic/src/transport/error.rs | 6 ++++++ tonic/src/transport/mod.rs | 8 ++++++-- tonic/src/transport/service/io.rs | 1 + tonic/src/transport/service/mod.rs | 20 +++++++++++++++++--- tonic/src/transport/service/tls.rs | 19 ++++++++++++++++--- 8 files changed, 66 insertions(+), 21 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 89dbc75e0..214a86ebc 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -276,13 +276,13 @@ tracing = ["dep:tracing", "dep:tracing-subscriber"] uds = ["tokio-stream/net", "dep:tower", "dep:hyper", "dep:hyper-util"] streaming = ["tokio-stream", "dep:h2"] mock = ["tokio-stream", "dep:tower", "dep:hyper-util"] -tower = ["dep:hyper", "dep:hyper-util", "dep:tower", "dep:http"] +tower = ["dep:hyper", "dep:hyper-util", "dep:tower", "tower?/timeout", "dep:http"] json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"] compression = ["tonic/gzip"] tls = ["tonic/tls"] tls-rustls = ["dep:hyper", "dep:hyper-util", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pemfile", "dep:tokio-rustls", "dep:pin-project", "dep:http-body-util"] dynamic-load-balance = ["dep:tower"] -timeout = ["tokio/time", "dep:tower"] +timeout = ["tokio/time", "dep:tower", "tower?/timeout"] tls-client-auth = ["tonic/tls"] types = ["dep:tonic-types"] h2c = ["dep:hyper", "dep:tower", "dep:http", "dep:hyper-util"] diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml index 74df3a583..cb77bcaed 100644 --- a/tonic/Cargo.toml +++ b/tonic/Cargo.toml @@ -26,24 +26,29 @@ version = "0.11.0" codegen = ["dep:async-trait"] gzip = ["dep:flate2"] zstd = ["dep:zstd"] -default = ["transport", "codegen", "prost"] +default = ["channel", "codegen", "prost"] prost = ["dep:prost"] tls = ["dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] tls-roots = ["tls-roots-common", "dep:rustls-native-certs"] -tls-roots-common = ["tls"] +tls-roots-common = ["tls", "channel"] tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"] router = ["dep:axum"] transport = [ "router", "dep:async-stream", - "channel", "dep:h2", - "dep:hyper", "dep:hyper-util", "dep:hyper-timeout", + "dep:hyper", "dep:hyper-util", "dep:socket2", "dep:tokio", "tokio?/macros", "tokio?/net", "tokio?/time", - "dep:tower", + "dep:tower", "tower?/util", "tower?/limit", +] +channel = [ + "transport", + "dep:hyper", "hyper?/client", + "dep:hyper-util", "hyper-util?/client-legacy", + "dep:tower", "tower?/balance", "tower?/buffer", "tower?/discover", "tower?/load", "tower?/make", + "dep:hyper-timeout", ] -channel = [] # [[bench]] # name = "bench_main" @@ -71,13 +76,12 @@ async-trait = {version = "0.1.13", optional = true} # transport async-stream = {version = "0.3", optional = true} h2 = {version = "0.4", optional = true} -hyper = {version = "1", features = ["full"], optional = true} -hyper-util = { version = ">=0.1.4, <0.2", features = ["full"], optional = true } -hyper-timeout = {version = "0.5", optional = true} +hyper = {version = "1", features = ["http1", "http2", "server"], optional = true} +hyper-util = { version = ">=0.1.4, <0.2", features = ["service", "server-auto", "tokio"], optional = true } socket2 = { version = ">=0.4.7, <0.6.0", optional = true, features = ["all"] } tokio = {version = "1", default-features = false, optional = true} tokio-stream = { version = "0.1", features = ["net"] } -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.7", default-features = false, optional = true} # rustls @@ -90,6 +94,9 @@ webpki-roots = { version = "0.26", optional = true } flate2 = {version = "1.0", optional = true} zstd = { version = "0.13.0", optional = true } +# channel +hyper-timeout = {version = "0.5", optional = true} + [dev-dependencies] bencher = "0.1.5" quickcheck = "1.0" diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 1b97d22d4..0a22226f1 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -618,7 +618,7 @@ fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option { // matches the spec of: // > The service is currently unavailable. This is most likely a transient condition that // > can be corrected if retried with a backoff. - #[cfg(feature = "transport")] + #[cfg(feature = "channel")] if let Some(connect) = err.downcast_ref::() { return Some(Status::unavailable(connect.to_string())); } diff --git a/tonic/src/transport/error.rs b/tonic/src/transport/error.rs index d2a1c7bb2..92a910498 100644 --- a/tonic/src/transport/error.rs +++ b/tonic/src/transport/error.rs @@ -15,7 +15,9 @@ struct ErrorImpl { #[derive(Debug)] pub(crate) enum Kind { Transport, + #[cfg(feature = "channel")] InvalidUri, + #[cfg(feature = "channel")] InvalidUserAgent, } @@ -35,10 +37,12 @@ impl Error { Error::new(Kind::Transport).with(source) } + #[cfg(feature = "channel")] pub(crate) fn new_invalid_uri() -> Self { Error::new(Kind::InvalidUri) } + #[cfg(feature = "channel")] pub(crate) fn new_invalid_user_agent() -> Self { Error::new(Kind::InvalidUserAgent) } @@ -46,7 +50,9 @@ impl Error { fn description(&self) -> &str { match &self.inner.kind { Kind::Transport => "transport error", + #[cfg(feature = "channel")] Kind::InvalidUri => "invalid URI", + #[cfg(feature = "channel")] Kind::InvalidUserAgent => "user agent is not a valid header value", } } diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index f29fd1c99..3f07a5be2 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -89,6 +89,7 @@ //! //! [rustls]: https://docs.rs/rustls/0.16.0/rustls/ +#[cfg(feature = "channel")] pub mod channel; pub mod server; @@ -106,6 +107,7 @@ pub use self::error::Error; pub use self::server::Server; #[doc(inline)] pub use self::service::grpc_timeout::TimeoutExpired; +#[cfg(feature = "channel")] pub(crate) use self::service::ConnectError; #[cfg(feature = "tls")] @@ -116,10 +118,11 @@ pub use hyper::{body::Body, Uri}; #[cfg(feature = "tls")] pub use tokio_rustls::rustls::pki_types::CertificateDer; +#[cfg(feature = "channel")] pub(crate) use self::service::executor::Executor; -#[cfg(feature = "tls")] -#[cfg_attr(docsrs, doc(cfg(feature = "tls")))] +#[cfg(all(feature = "channel", feature = "tls"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "channel", feature = "tls"))))] pub use self::channel::ClientTlsConfig; #[cfg(feature = "tls")] #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] @@ -128,4 +131,5 @@ pub use self::server::ServerTlsConfig; #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] pub use self::tls::Identity; +#[cfg(feature = "channel")] use crate::service::router::BoxFuture; diff --git a/tonic/src/transport/service/io.rs b/tonic/src/transport/service/io.rs index cb2296cac..694ae4043 100644 --- a/tonic/src/transport/service/io.rs +++ b/tonic/src/transport/service/io.rs @@ -19,6 +19,7 @@ impl Io for T where T: rt::Read + rt::Write + Send + 'static {} pub(crate) struct BoxedIo(Pin>); impl BoxedIo { + #[cfg(feature = "channel")] pub(in crate::transport) fn new(io: I) -> Self { BoxedIo(Box::pin(io)) } diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index eeae37f5d..a07ea74bd 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,23 +1,37 @@ +#[cfg(feature = "channel")] mod add_origin; +#[cfg(feature = "channel")] mod connection; +#[cfg(feature = "channel")] mod connector; +#[cfg(feature = "channel")] mod discover; +#[cfg(feature = "channel")] pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; +#[cfg(feature = "channel")] mod reconnect; #[cfg(feature = "tls")] mod tls; +#[cfg(feature = "channel")] mod user_agent; +#[cfg(feature = "channel")] pub(crate) use self::add_origin::AddOrigin; +#[cfg(feature = "channel")] pub(crate) use self::connection::Connection; -pub(crate) use self::connector::ConnectError; -pub(crate) use self::connector::Connector; +#[cfg(feature = "channel")] +pub(crate) use self::connector::{ConnectError, Connector}; +#[cfg(feature = "channel")] pub(crate) use self::discover::DynamicServiceStream; +#[cfg(feature = "channel")] pub(crate) use self::executor::SharedExec; pub(crate) use self::grpc_timeout::GrpcTimeout; pub(crate) use self::io::ServerIo; #[cfg(feature = "tls")] -pub(crate) use self::tls::{TlsAcceptor, TlsConnector}; +pub(crate) use self::tls::TlsAcceptor; +#[cfg(all(feature = "channel", feature = "tls"))] +pub(crate) use self::tls::TlsConnector; +#[cfg(feature = "channel")] pub(crate) use self::user_agent::UserAgent; diff --git a/tonic/src/transport/service/tls.rs b/tonic/src/transport/service/tls.rs index 0e38d87ee..6d80abf10 100644 --- a/tonic/src/transport/service/tls.rs +++ b/tonic/src/transport/service/tls.rs @@ -4,20 +4,27 @@ use std::{ }; use tokio::io::{AsyncRead, AsyncWrite}; +#[cfg(feature = "channel")] +use tokio_rustls::{ + rustls::{pki_types::ServerName, ClientConfig}, + TlsConnector as RustlsConnector, +}; use tokio_rustls::{ rustls::{ - pki_types::{CertificateDer, PrivateKeyDer, ServerName}, + pki_types::{CertificateDer, PrivateKeyDer}, server::WebPkiClientVerifier, - ClientConfig, RootCertStore, ServerConfig, + RootCertStore, ServerConfig, }, - TlsAcceptor as RustlsAcceptor, TlsConnector as RustlsConnector, + TlsAcceptor as RustlsAcceptor, }; +#[cfg(feature = "channel")] use super::io::BoxedIo; use crate::transport::{ server::{Connected, TlsStream}, Certificate, Identity, }; +#[cfg(feature = "channel")] use hyper_util::rt::TokioIo; /// h2 alpn in plain format for rustls. @@ -25,11 +32,13 @@ const ALPN_H2: &[u8] = b"h2"; #[derive(Debug)] enum TlsError { + #[cfg(feature = "channel")] H2NotNegotiated, CertificateParseError, PrivateKeyParseError, } +#[cfg(feature = "channel")] #[derive(Clone)] pub(crate) struct TlsConnector { config: Arc, @@ -37,6 +46,7 @@ pub(crate) struct TlsConnector { assume_http2: bool, } +#[cfg(feature = "channel")] impl TlsConnector { pub(crate) fn new( ca_certs: Vec, @@ -74,6 +84,7 @@ impl TlsConnector { }) } + #[cfg(feature = "channel")] pub(crate) async fn connect(&self, io: I) -> Result where I: AsyncRead + AsyncWrite + Send + Unpin + 'static, @@ -93,6 +104,7 @@ impl TlsConnector { } } +#[cfg(feature = "channel")] impl fmt::Debug for TlsConnector { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("TlsConnector").finish() @@ -154,6 +166,7 @@ impl fmt::Debug for TlsAcceptor { impl fmt::Display for TlsError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + #[cfg(feature = "channel")] TlsError::H2NotNegotiated => write!(f, "HTTP/2 was not negotiated."), TlsError::CertificateParseError => write!(f, "Error parsing TLS certificate."), TlsError::PrivateKeyParseError => write!( From 418725e4e4a85ef3645da6d66e440544a4fd1f5c Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 19:35:38 +0900 Subject: [PATCH 02/14] chore(channel): Move add_origin service module to channel module --- tonic/src/transport/channel/mod.rs | 1 + tonic/src/transport/{ => channel}/service/add_origin.rs | 0 tonic/src/transport/channel/service/mod.rs | 2 ++ tonic/src/transport/service/connection.rs | 3 ++- tonic/src/transport/service/mod.rs | 4 ---- 5 files changed, 5 insertions(+), 5 deletions(-) rename tonic/src/transport/{ => channel}/service/add_origin.rs (100%) create mode 100644 tonic/src/transport/channel/service/mod.rs diff --git a/tonic/src/transport/channel/mod.rs b/tonic/src/transport/channel/mod.rs index 0983725f8..16643d5cc 100644 --- a/tonic/src/transport/channel/mod.rs +++ b/tonic/src/transport/channel/mod.rs @@ -1,6 +1,7 @@ //! Client implementation and builder. mod endpoint; +pub(crate) mod service; #[cfg(feature = "tls")] #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] mod tls; diff --git a/tonic/src/transport/service/add_origin.rs b/tonic/src/transport/channel/service/add_origin.rs similarity index 100% rename from tonic/src/transport/service/add_origin.rs rename to tonic/src/transport/channel/service/add_origin.rs diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs new file mode 100644 index 000000000..a480c94e2 --- /dev/null +++ b/tonic/src/transport/channel/service/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod add_origin; +pub(crate) use self::add_origin::AddOrigin; diff --git a/tonic/src/transport/service/connection.rs b/tonic/src/transport/service/connection.rs index 47a505f48..7b9d45b0f 100644 --- a/tonic/src/transport/service/connection.rs +++ b/tonic/src/transport/service/connection.rs @@ -1,7 +1,8 @@ use super::SharedExec; -use super::{grpc_timeout::GrpcTimeout, reconnect::Reconnect, AddOrigin, UserAgent}; +use super::{grpc_timeout::GrpcTimeout, reconnect::Reconnect, UserAgent}; use crate::{ body::{boxed, BoxBody}, + transport::channel::service::AddOrigin, transport::{BoxFuture, Endpoint}, }; use http::Uri; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index a07ea74bd..96278c58f 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,6 +1,4 @@ #[cfg(feature = "channel")] -mod add_origin; -#[cfg(feature = "channel")] mod connection; #[cfg(feature = "channel")] mod connector; @@ -17,8 +15,6 @@ mod tls; #[cfg(feature = "channel")] mod user_agent; -#[cfg(feature = "channel")] -pub(crate) use self::add_origin::AddOrigin; #[cfg(feature = "channel")] pub(crate) use self::connection::Connection; #[cfg(feature = "channel")] From 9b218953fe375508717fc7eccfa09b0bd2d7e04e Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 20:02:25 +0900 Subject: [PATCH 03/14] chore(channel): Move user_agent service to channel module --- tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/{ => channel}/service/user_agent.rs | 0 tonic/src/transport/service/connection.rs | 4 ++-- tonic/src/transport/service/mod.rs | 4 ---- 4 files changed, 5 insertions(+), 6 deletions(-) rename tonic/src/transport/{ => channel}/service/user_agent.rs (100%) diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index a480c94e2..7e0593667 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -1,2 +1,5 @@ pub(crate) mod add_origin; pub(crate) use self::add_origin::AddOrigin; + +pub(crate) mod user_agent; +pub(crate) use self::user_agent::UserAgent; diff --git a/tonic/src/transport/service/user_agent.rs b/tonic/src/transport/channel/service/user_agent.rs similarity index 100% rename from tonic/src/transport/service/user_agent.rs rename to tonic/src/transport/channel/service/user_agent.rs diff --git a/tonic/src/transport/service/connection.rs b/tonic/src/transport/service/connection.rs index 7b9d45b0f..2b69e4e3f 100644 --- a/tonic/src/transport/service/connection.rs +++ b/tonic/src/transport/service/connection.rs @@ -1,8 +1,8 @@ use super::SharedExec; -use super::{grpc_timeout::GrpcTimeout, reconnect::Reconnect, UserAgent}; +use super::{grpc_timeout::GrpcTimeout, reconnect::Reconnect}; use crate::{ body::{boxed, BoxBody}, - transport::channel::service::AddOrigin, + transport::channel::service::{AddOrigin, UserAgent}, transport::{BoxFuture, Endpoint}, }; use http::Uri; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 96278c58f..35c3e8afd 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -12,8 +12,6 @@ mod io; mod reconnect; #[cfg(feature = "tls")] mod tls; -#[cfg(feature = "channel")] -mod user_agent; #[cfg(feature = "channel")] pub(crate) use self::connection::Connection; @@ -29,5 +27,3 @@ pub(crate) use self::io::ServerIo; pub(crate) use self::tls::TlsAcceptor; #[cfg(all(feature = "channel", feature = "tls"))] pub(crate) use self::tls::TlsConnector; -#[cfg(feature = "channel")] -pub(crate) use self::user_agent::UserAgent; From a1a9f379084c9fd19f5dd10fb71ddbd4ad9dbf5c Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 20:35:20 +0900 Subject: [PATCH 04/14] chore(channel): Move reconnect service to channel module --- tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/{ => channel}/service/reconnect.rs | 0 tonic/src/transport/service/connection.rs | 4 ++-- tonic/src/transport/service/mod.rs | 2 -- 4 files changed, 5 insertions(+), 4 deletions(-) rename tonic/src/transport/{ => channel}/service/reconnect.rs (100%) diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 7e0593667..1b91c274e 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -3,3 +3,6 @@ pub(crate) use self::add_origin::AddOrigin; pub(crate) mod user_agent; pub(crate) use self::user_agent::UserAgent; + +pub(crate) mod reconnect; +pub(crate) use self::reconnect::Reconnect; diff --git a/tonic/src/transport/service/reconnect.rs b/tonic/src/transport/channel/service/reconnect.rs similarity index 100% rename from tonic/src/transport/service/reconnect.rs rename to tonic/src/transport/channel/service/reconnect.rs diff --git a/tonic/src/transport/service/connection.rs b/tonic/src/transport/service/connection.rs index 2b69e4e3f..e42cd3356 100644 --- a/tonic/src/transport/service/connection.rs +++ b/tonic/src/transport/service/connection.rs @@ -1,8 +1,8 @@ +use super::grpc_timeout::GrpcTimeout; use super::SharedExec; -use super::{grpc_timeout::GrpcTimeout, reconnect::Reconnect}; use crate::{ body::{boxed, BoxBody}, - transport::channel::service::{AddOrigin, UserAgent}, + transport::channel::service::{AddOrigin, Reconnect, UserAgent}, transport::{BoxFuture, Endpoint}, }; use http::Uri; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 35c3e8afd..fc0fb27eb 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -8,8 +8,6 @@ mod discover; pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; -#[cfg(feature = "channel")] -mod reconnect; #[cfg(feature = "tls")] mod tls; From 02b3d116ea018c62f97295efa5dfd3182fd43a85 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 20:45:19 +0900 Subject: [PATCH 05/14] chore(channel): Move connection service to channel module --- tonic/src/transport/channel/mod.rs | 3 ++- .../transport/{ => channel}/service/connection.rs | 15 +++++++-------- tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/service/discover.rs | 3 +-- tonic/src/transport/service/mod.rs | 4 ---- 5 files changed, 13 insertions(+), 15 deletions(-) rename tonic/src/transport/{ => channel}/service/connection.rs (94%) diff --git a/tonic/src/transport/channel/mod.rs b/tonic/src/transport/channel/mod.rs index 16643d5cc..883153591 100644 --- a/tonic/src/transport/channel/mod.rs +++ b/tonic/src/transport/channel/mod.rs @@ -10,7 +10,8 @@ pub use endpoint::Endpoint; #[cfg(feature = "tls")] pub use tls::ClientTlsConfig; -use super::service::{Connection, DynamicServiceStream, SharedExec}; +use self::service::Connection; +use super::service::{DynamicServiceStream, SharedExec}; use crate::body::BoxBody; use crate::transport::Executor; use bytes::Bytes; diff --git a/tonic/src/transport/service/connection.rs b/tonic/src/transport/channel/service/connection.rs similarity index 94% rename from tonic/src/transport/service/connection.rs rename to tonic/src/transport/channel/service/connection.rs index e42cd3356..1e84e9b5d 100644 --- a/tonic/src/transport/service/connection.rs +++ b/tonic/src/transport/channel/service/connection.rs @@ -1,9 +1,8 @@ -use super::grpc_timeout::GrpcTimeout; -use super::SharedExec; +use super::{AddOrigin, Reconnect, UserAgent}; +use crate::transport::service::SharedExec; use crate::{ body::{boxed, BoxBody}, - transport::channel::service::{AddOrigin, Reconnect, UserAgent}, - transport::{BoxFuture, Endpoint}, + transport::{service::GrpcTimeout, BoxFuture, Endpoint}, }; use http::Uri; use hyper::rt; @@ -37,7 +36,7 @@ impl Connection { C::Future: Unpin + Send, C::Response: rt::Read + rt::Write + Unpin + Send + 'static, { - let mut settings: Builder = Builder::new(endpoint.executor.clone()) + let mut settings: Builder = Builder::new(endpoint.executor.clone()) .initial_stream_window_size(endpoint.init_stream_window_size) .initial_connection_window_size(endpoint.init_connection_window_size) .keep_alive_interval(endpoint.http2_keep_alive_interval) @@ -159,12 +158,12 @@ impl tower::Service> for SendRequest { struct MakeSendRequestService { connector: C, - executor: super::SharedExec, - settings: Builder, + executor: SharedExec, + settings: Builder, } impl MakeSendRequestService { - fn new(connector: C, executor: SharedExec, settings: Builder) -> Self { + fn new(connector: C, executor: SharedExec, settings: Builder) -> Self { Self { connector, executor, diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 1b91c274e..483661eb4 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -6,3 +6,6 @@ pub(crate) use self::user_agent::UserAgent; pub(crate) mod reconnect; pub(crate) use self::reconnect::Reconnect; + +pub(crate) mod connection; +pub(crate) use self::connection::Connection; diff --git a/tonic/src/transport/service/discover.rs b/tonic/src/transport/service/discover.rs index b9356110e..5c15d3365 100644 --- a/tonic/src/transport/service/discover.rs +++ b/tonic/src/transport/service/discover.rs @@ -1,5 +1,4 @@ -use super::connection::Connection; -use crate::transport::Endpoint; +use crate::transport::{channel::service::Connection, Endpoint}; use hyper_util::client::legacy::connect::HttpConnector; use std::{ diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index fc0fb27eb..b4d38f745 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,6 +1,4 @@ #[cfg(feature = "channel")] -mod connection; -#[cfg(feature = "channel")] mod connector; #[cfg(feature = "channel")] mod discover; @@ -11,8 +9,6 @@ mod io; #[cfg(feature = "tls")] mod tls; -#[cfg(feature = "channel")] -pub(crate) use self::connection::Connection; #[cfg(feature = "channel")] pub(crate) use self::connector::{ConnectError, Connector}; #[cfg(feature = "channel")] From 48b009b47a2ec2b1bc9bd1621a7d830ac11b00bc Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 20:53:12 +0900 Subject: [PATCH 06/14] chore(channel): Move discover service to channel module --- tonic/src/transport/channel/mod.rs | 4 ++-- tonic/src/transport/{ => channel}/service/discover.rs | 0 tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/service/mod.rs | 4 ---- 4 files changed, 5 insertions(+), 6 deletions(-) rename tonic/src/transport/{ => channel}/service/discover.rs (100%) diff --git a/tonic/src/transport/channel/mod.rs b/tonic/src/transport/channel/mod.rs index 883153591..b273c3015 100644 --- a/tonic/src/transport/channel/mod.rs +++ b/tonic/src/transport/channel/mod.rs @@ -10,8 +10,8 @@ pub use endpoint::Endpoint; #[cfg(feature = "tls")] pub use tls::ClientTlsConfig; -use self::service::Connection; -use super::service::{DynamicServiceStream, SharedExec}; +use self::service::{Connection, DynamicServiceStream}; +use super::service::SharedExec; use crate::body::BoxBody; use crate::transport::Executor; use bytes::Bytes; diff --git a/tonic/src/transport/service/discover.rs b/tonic/src/transport/channel/service/discover.rs similarity index 100% rename from tonic/src/transport/service/discover.rs rename to tonic/src/transport/channel/service/discover.rs diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 483661eb4..72138de74 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -9,3 +9,6 @@ pub(crate) use self::reconnect::Reconnect; pub(crate) mod connection; pub(crate) use self::connection::Connection; + +pub(crate) mod discover; +pub(crate) use self::discover::DynamicServiceStream; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index b4d38f745..98b8dcf20 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,8 +1,6 @@ #[cfg(feature = "channel")] mod connector; #[cfg(feature = "channel")] -mod discover; -#[cfg(feature = "channel")] pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; @@ -12,8 +10,6 @@ mod tls; #[cfg(feature = "channel")] pub(crate) use self::connector::{ConnectError, Connector}; #[cfg(feature = "channel")] -pub(crate) use self::discover::DynamicServiceStream; -#[cfg(feature = "channel")] pub(crate) use self::executor::SharedExec; pub(crate) use self::grpc_timeout::GrpcTimeout; pub(crate) use self::io::ServerIo; From 1cc3f9f5db20e8dcd106e59fe8983cd24cfcf1c3 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 21:13:12 +0900 Subject: [PATCH 07/14] feat(channel): Remove unused Connected implement for BoxedIo --- tonic/src/transport/service/io.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tonic/src/transport/service/io.rs b/tonic/src/transport/service/io.rs index 694ae4043..d5eaaeb22 100644 --- a/tonic/src/transport/service/io.rs +++ b/tonic/src/transport/service/io.rs @@ -31,17 +31,6 @@ impl Connection for BoxedIo { } } -impl Connected for BoxedIo { - type ConnectInfo = NoneConnectInfo; - - fn connect_info(&self) -> Self::ConnectInfo { - NoneConnectInfo - } -} - -#[derive(Copy, Clone)] -pub(crate) struct NoneConnectInfo; - impl rt::Read for BoxedIo { fn poll_read( mut self: Pin<&mut Self>, From b9ed61b12bfe291a290f8cf5c5eee8f5badf2cb8 Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 23 May 2024 21:13:58 +0900 Subject: [PATCH 08/14] chore(channel): Move channel part of io service to channel module --- tonic/src/transport/channel/service/io.rs | 67 ++++++++++++++++++++++ tonic/src/transport/channel/service/mod.rs | 3 + tonic/src/transport/service/connector.rs | 2 +- tonic/src/transport/service/io.rs | 64 --------------------- tonic/src/transport/service/tls.rs | 2 +- 5 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 tonic/src/transport/channel/service/io.rs diff --git a/tonic/src/transport/channel/service/io.rs b/tonic/src/transport/channel/service/io.rs new file mode 100644 index 000000000..084195ad6 --- /dev/null +++ b/tonic/src/transport/channel/service/io.rs @@ -0,0 +1,67 @@ +use std::io::{self, IoSlice}; +use std::pin::Pin; +use std::task::{Context, Poll}; + +use hyper::rt; +use hyper_util::client::legacy::connect::{Connected as HyperConnected, Connection}; + +pub(in crate::transport) trait Io: + rt::Read + rt::Write + Send + 'static +{ +} + +impl Io for T where T: rt::Read + rt::Write + Send + 'static {} + +pub(crate) struct BoxedIo(Pin>); + +impl BoxedIo { + pub(in crate::transport) fn new(io: I) -> Self { + BoxedIo(Box::pin(io)) + } +} + +impl Connection for BoxedIo { + fn connected(&self) -> HyperConnected { + HyperConnected::new() + } +} + +impl rt::Read for BoxedIo { + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: rt::ReadBufCursor<'_>, + ) -> Poll> { + Pin::new(&mut self.0).poll_read(cx, buf) + } +} + +impl rt::Write for BoxedIo { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + Pin::new(&mut self.0).poll_write(cx, buf) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.0).poll_flush(cx) + } + + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.0).poll_shutdown(cx) + } + + fn poll_write_vectored( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + bufs: &[IoSlice<'_>], + ) -> Poll> { + Pin::new(&mut self.0).poll_write_vectored(cx, bufs) + } + + fn is_write_vectored(&self) -> bool { + self.0.is_write_vectored() + } +} diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 72138de74..0f6f8d619 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -12,3 +12,6 @@ pub(crate) use self::connection::Connection; pub(crate) mod discover; pub(crate) use self::discover::DynamicServiceStream; + +pub(crate) mod io; +pub(crate) use self::io::BoxedIo; diff --git a/tonic/src/transport/service/connector.rs b/tonic/src/transport/service/connector.rs index 4c73d13f2..c9e04f5ed 100644 --- a/tonic/src/transport/service/connector.rs +++ b/tonic/src/transport/service/connector.rs @@ -1,7 +1,7 @@ use super::super::BoxFuture; -use super::io::BoxedIo; #[cfg(feature = "tls")] use super::tls::TlsConnector; +use crate::transport::channel::service::BoxedIo; use http::Uri; use std::fmt; use std::task::{Context, Poll}; diff --git a/tonic/src/transport/service/io.rs b/tonic/src/transport/service/io.rs index d5eaaeb22..7821f691c 100644 --- a/tonic/src/transport/service/io.rs +++ b/tonic/src/transport/service/io.rs @@ -1,6 +1,4 @@ use crate::transport::server::Connected; -use hyper::rt; -use hyper_util::client::legacy::connect::{Connected as HyperConnected, Connection}; use std::io; use std::io::IoSlice; use std::pin::Pin; @@ -9,68 +7,6 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; #[cfg(feature = "tls")] use tokio_rustls::server::TlsStream; -pub(in crate::transport) trait Io: - rt::Read + rt::Write + Send + 'static -{ -} - -impl Io for T where T: rt::Read + rt::Write + Send + 'static {} - -pub(crate) struct BoxedIo(Pin>); - -impl BoxedIo { - #[cfg(feature = "channel")] - pub(in crate::transport) fn new(io: I) -> Self { - BoxedIo(Box::pin(io)) - } -} - -impl Connection for BoxedIo { - fn connected(&self) -> HyperConnected { - HyperConnected::new() - } -} - -impl rt::Read for BoxedIo { - fn poll_read( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: rt::ReadBufCursor<'_>, - ) -> Poll> { - Pin::new(&mut self.0).poll_read(cx, buf) - } -} - -impl rt::Write for BoxedIo { - fn poll_write( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll> { - Pin::new(&mut self.0).poll_write(cx, buf) - } - - fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_flush(cx) - } - - fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.0).poll_shutdown(cx) - } - - fn poll_write_vectored( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - bufs: &[IoSlice<'_>], - ) -> Poll> { - Pin::new(&mut self.0).poll_write_vectored(cx, bufs) - } - - fn is_write_vectored(&self) -> bool { - self.0.is_write_vectored() - } -} - pub(crate) enum ServerIo { Io(IO), #[cfg(feature = "tls")] diff --git a/tonic/src/transport/service/tls.rs b/tonic/src/transport/service/tls.rs index 6d80abf10..83b46053c 100644 --- a/tonic/src/transport/service/tls.rs +++ b/tonic/src/transport/service/tls.rs @@ -19,7 +19,7 @@ use tokio_rustls::{ }; #[cfg(feature = "channel")] -use super::io::BoxedIo; +use crate::transport::channel::service::BoxedIo; use crate::transport::{ server::{Connected, TlsStream}, Certificate, Identity, From 3d54b576b4d75cd4d2aa2a843d89f13d7d5c350e Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 24 May 2024 06:43:34 +0900 Subject: [PATCH 09/14] chore(channel): Move channel part of tls service to channel module --- tonic/src/transport/channel/endpoint.rs | 4 +- tonic/src/transport/channel/service/mod.rs | 5 ++ tonic/src/transport/channel/service/tls.rs | 83 ++++++++++++++++++++ tonic/src/transport/channel/tls.rs | 2 +- tonic/src/transport/service/connector.rs | 4 +- tonic/src/transport/service/mod.rs | 4 +- tonic/src/transport/service/tls.rs | 90 +--------------------- 7 files changed, 98 insertions(+), 94 deletions(-) create mode 100644 tonic/src/transport/channel/service/tls.rs diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs index 6014960a8..4fe6d228b 100644 --- a/tonic/src/transport/channel/endpoint.rs +++ b/tonic/src/transport/channel/endpoint.rs @@ -1,9 +1,9 @@ use super::super::service; +#[cfg(feature = "tls")] +use super::service::TlsConnector; use super::Channel; #[cfg(feature = "tls")] use super::ClientTlsConfig; -#[cfg(feature = "tls")] -use crate::transport::service::TlsConnector; use crate::transport::{service::SharedExec, Error, Executor}; use bytes::Bytes; use http::{uri::Uri, HeaderValue}; diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 0f6f8d619..3ce7e35b8 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -15,3 +15,8 @@ pub(crate) use self::discover::DynamicServiceStream; pub(crate) mod io; pub(crate) use self::io::BoxedIo; + +#[cfg(feature = "tls")] +pub(crate) mod tls; +#[cfg(feature = "tls")] +pub(crate) use self::tls::TlsConnector; diff --git a/tonic/src/transport/channel/service/tls.rs b/tonic/src/transport/channel/service/tls.rs new file mode 100644 index 000000000..c63396a70 --- /dev/null +++ b/tonic/src/transport/channel/service/tls.rs @@ -0,0 +1,83 @@ +use std::fmt; +use std::io::Cursor; +use std::sync::Arc; + +use hyper_util::rt::TokioIo; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio_rustls::{ + rustls::{pki_types::ServerName, ClientConfig, RootCertStore}, + TlsConnector as RustlsConnector, +}; + +use super::io::BoxedIo; +use crate::transport::service::tls::{add_certs_from_pem, load_identity, TlsError, ALPN_H2}; +use crate::transport::tls::{Certificate, Identity}; + +#[derive(Clone)] +pub(crate) struct TlsConnector { + config: Arc, + domain: Arc>, + assume_http2: bool, +} + +impl TlsConnector { + pub(crate) fn new( + ca_certs: Vec, + identity: Option, + domain: &str, + assume_http2: bool, + ) -> Result { + let builder = ClientConfig::builder(); + let mut roots = RootCertStore::empty(); + + #[cfg(feature = "tls-roots")] + roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?); + + #[cfg(feature = "tls-webpki-roots")] + roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + + for cert in ca_certs { + add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?; + } + + let builder = builder.with_root_certificates(roots); + let mut config = match identity { + Some(identity) => { + let (client_cert, client_key) = load_identity(identity)?; + builder.with_client_auth_cert(client_cert, client_key)? + } + None => builder.with_no_client_auth(), + }; + + config.alpn_protocols.push(ALPN_H2.into()); + Ok(Self { + config: Arc::new(config), + domain: Arc::new(ServerName::try_from(domain)?.to_owned()), + assume_http2, + }) + } + + pub(crate) async fn connect(&self, io: I) -> Result + where + I: AsyncRead + AsyncWrite + Send + Unpin + 'static, + { + let io = RustlsConnector::from(self.config.clone()) + .connect(self.domain.as_ref().to_owned(), io) + .await?; + + // Generally we require ALPN to be negotiated, but if the user has + // explicitly set `assume_http2` to true, we'll allow it to be missing. + let (_, session) = io.get_ref(); + let alpn_protocol = session.alpn_protocol(); + if !(alpn_protocol == Some(ALPN_H2) || self.assume_http2) { + return Err(TlsError::H2NotNegotiated.into()); + } + Ok(BoxedIo::new(TokioIo::new(io))) + } +} + +impl fmt::Debug for TlsConnector { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TlsConnector").finish() + } +} diff --git a/tonic/src/transport/channel/tls.rs b/tonic/src/transport/channel/tls.rs index a3c64a65c..31f5a7a46 100644 --- a/tonic/src/transport/channel/tls.rs +++ b/tonic/src/transport/channel/tls.rs @@ -1,5 +1,5 @@ +use super::service::TlsConnector; use crate::transport::{ - service::TlsConnector, tls::{Certificate, Identity}, Error, }; diff --git a/tonic/src/transport/service/connector.rs b/tonic/src/transport/service/connector.rs index c9e04f5ed..d9f1e468a 100644 --- a/tonic/src/transport/service/connector.rs +++ b/tonic/src/transport/service/connector.rs @@ -1,7 +1,7 @@ use super::super::BoxFuture; -#[cfg(feature = "tls")] -use super::tls::TlsConnector; use crate::transport::channel::service::BoxedIo; +#[cfg(feature = "tls")] +use crate::transport::channel::service::TlsConnector; use http::Uri; use std::fmt; use std::task::{Context, Poll}; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 98b8dcf20..8bd57402c 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -5,7 +5,7 @@ pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; #[cfg(feature = "tls")] -mod tls; +pub(crate) mod tls; #[cfg(feature = "channel")] pub(crate) use self::connector::{ConnectError, Connector}; @@ -15,5 +15,3 @@ pub(crate) use self::grpc_timeout::GrpcTimeout; pub(crate) use self::io::ServerIo; #[cfg(feature = "tls")] pub(crate) use self::tls::TlsAcceptor; -#[cfg(all(feature = "channel", feature = "tls"))] -pub(crate) use self::tls::TlsConnector; diff --git a/tonic/src/transport/service/tls.rs b/tonic/src/transport/service/tls.rs index 83b46053c..7bbf210b3 100644 --- a/tonic/src/transport/service/tls.rs +++ b/tonic/src/transport/service/tls.rs @@ -4,11 +4,6 @@ use std::{ }; use tokio::io::{AsyncRead, AsyncWrite}; -#[cfg(feature = "channel")] -use tokio_rustls::{ - rustls::{pki_types::ServerName, ClientConfig}, - TlsConnector as RustlsConnector, -}; use tokio_rustls::{ rustls::{ pki_types::{CertificateDer, PrivateKeyDer}, @@ -18,99 +13,22 @@ use tokio_rustls::{ TlsAcceptor as RustlsAcceptor, }; -#[cfg(feature = "channel")] -use crate::transport::channel::service::BoxedIo; use crate::transport::{ server::{Connected, TlsStream}, Certificate, Identity, }; -#[cfg(feature = "channel")] -use hyper_util::rt::TokioIo; /// h2 alpn in plain format for rustls. -const ALPN_H2: &[u8] = b"h2"; +pub(crate) const ALPN_H2: &[u8] = b"h2"; #[derive(Debug)] -enum TlsError { +pub(crate) enum TlsError { #[cfg(feature = "channel")] H2NotNegotiated, CertificateParseError, PrivateKeyParseError, } -#[cfg(feature = "channel")] -#[derive(Clone)] -pub(crate) struct TlsConnector { - config: Arc, - domain: Arc>, - assume_http2: bool, -} - -#[cfg(feature = "channel")] -impl TlsConnector { - pub(crate) fn new( - ca_certs: Vec, - identity: Option, - domain: &str, - assume_http2: bool, - ) -> Result { - let builder = ClientConfig::builder(); - let mut roots = RootCertStore::empty(); - - #[cfg(feature = "tls-roots")] - roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?); - - #[cfg(feature = "tls-webpki-roots")] - roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); - - for cert in ca_certs { - add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?; - } - - let builder = builder.with_root_certificates(roots); - let mut config = match identity { - Some(identity) => { - let (client_cert, client_key) = load_identity(identity)?; - builder.with_client_auth_cert(client_cert, client_key)? - } - None => builder.with_no_client_auth(), - }; - - config.alpn_protocols.push(ALPN_H2.into()); - Ok(Self { - config: Arc::new(config), - domain: Arc::new(ServerName::try_from(domain)?.to_owned()), - assume_http2, - }) - } - - #[cfg(feature = "channel")] - pub(crate) async fn connect(&self, io: I) -> Result - where - I: AsyncRead + AsyncWrite + Send + Unpin + 'static, - { - let io = RustlsConnector::from(self.config.clone()) - .connect(self.domain.as_ref().to_owned(), io) - .await?; - - // Generally we require ALPN to be negotiated, but if the user has - // explicitly set `assume_http2` to true, we'll allow it to be missing. - let (_, session) = io.get_ref(); - let alpn_protocol = session.alpn_protocol(); - if !(alpn_protocol == Some(ALPN_H2) || self.assume_http2) { - return Err(TlsError::H2NotNegotiated.into()); - } - Ok(BoxedIo::new(TokioIo::new(io))) - } -} - -#[cfg(feature = "channel")] -impl fmt::Debug for TlsConnector { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("TlsConnector").finish() - } -} - #[derive(Clone)] pub(crate) struct TlsAcceptor { inner: Arc, @@ -179,7 +97,7 @@ impl fmt::Display for TlsError { impl std::error::Error for TlsError {} -fn load_identity( +pub(crate) fn load_identity( identity: Identity, ) -> Result<(Vec>, PrivateKeyDer<'static>), TlsError> { let cert = rustls_pemfile::certs(&mut Cursor::new(identity.cert)) @@ -193,7 +111,7 @@ fn load_identity( Ok((cert, key)) } -fn add_certs_from_pem( +pub(crate) fn add_certs_from_pem( mut certs: &mut dyn std::io::BufRead, roots: &mut RootCertStore, ) -> Result<(), crate::Error> { From e77359092b875530dfe342283165d290b7cec49c Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 24 May 2024 06:49:22 +0900 Subject: [PATCH 10/14] chore(channel): Move connector service to channel module --- tonic/src/transport/channel/endpoint.rs | 3 +-- tonic/src/transport/{ => channel}/service/connector.rs | 6 +++--- tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/mod.rs | 4 ++-- tonic/src/transport/service/mod.rs | 4 ---- 5 files changed, 9 insertions(+), 11 deletions(-) rename tonic/src/transport/{ => channel}/service/connector.rs (96%) diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs index 4fe6d228b..054cbe78f 100644 --- a/tonic/src/transport/channel/endpoint.rs +++ b/tonic/src/transport/channel/endpoint.rs @@ -1,9 +1,8 @@ -use super::super::service; #[cfg(feature = "tls")] use super::service::TlsConnector; -use super::Channel; #[cfg(feature = "tls")] use super::ClientTlsConfig; +use super::{service, Channel}; use crate::transport::{service::SharedExec, Error, Executor}; use bytes::Bytes; use http::{uri::Uri, HeaderValue}; diff --git a/tonic/src/transport/service/connector.rs b/tonic/src/transport/channel/service/connector.rs similarity index 96% rename from tonic/src/transport/service/connector.rs rename to tonic/src/transport/channel/service/connector.rs index d9f1e468a..0bfe0a518 100644 --- a/tonic/src/transport/service/connector.rs +++ b/tonic/src/transport/channel/service/connector.rs @@ -1,7 +1,7 @@ -use super::super::BoxFuture; -use crate::transport::channel::service::BoxedIo; +use super::BoxedIo; #[cfg(feature = "tls")] -use crate::transport::channel::service::TlsConnector; +use super::TlsConnector; +use crate::transport::BoxFuture; use http::Uri; use std::fmt; use std::task::{Context, Poll}; diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 3ce7e35b8..81b095aa6 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -16,6 +16,9 @@ pub(crate) use self::discover::DynamicServiceStream; pub(crate) mod io; pub(crate) use self::io::BoxedIo; +pub(crate) mod connector; +pub(crate) use self::connector::{ConnectError, Connector}; + #[cfg(feature = "tls")] pub(crate) mod tls; #[cfg(feature = "tls")] diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index 3f07a5be2..57ed36dfe 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -98,6 +98,8 @@ mod service; #[cfg(feature = "tls")] mod tls; +#[cfg(feature = "channel")] +pub(crate) use self::channel::service::ConnectError; #[doc(inline)] #[cfg(feature = "channel")] #[cfg_attr(docsrs, doc(cfg(feature = "channel")))] @@ -107,8 +109,6 @@ pub use self::error::Error; pub use self::server::Server; #[doc(inline)] pub use self::service::grpc_timeout::TimeoutExpired; -#[cfg(feature = "channel")] -pub(crate) use self::service::ConnectError; #[cfg(feature = "tls")] #[cfg_attr(docsrs, doc(cfg(feature = "tls")))] diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 8bd57402c..0f97c15ce 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,14 +1,10 @@ #[cfg(feature = "channel")] -mod connector; -#[cfg(feature = "channel")] pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; #[cfg(feature = "tls")] pub(crate) mod tls; -#[cfg(feature = "channel")] -pub(crate) use self::connector::{ConnectError, Connector}; #[cfg(feature = "channel")] pub(crate) use self::executor::SharedExec; pub(crate) use self::grpc_timeout::GrpcTimeout; From cf8e302c3cf12342d9a038e221c0a4d71b6bfa8f Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 24 May 2024 06:54:33 +0900 Subject: [PATCH 11/14] chore(channel): Move executor service to channel module --- tonic/src/transport/channel/endpoint.rs | 5 +++-- tonic/src/transport/channel/mod.rs | 4 +--- tonic/src/transport/{ => channel}/service/executor.rs | 0 tonic/src/transport/channel/service/mod.rs | 3 +++ tonic/src/transport/mod.rs | 3 --- tonic/src/transport/service/mod.rs | 4 ---- 6 files changed, 7 insertions(+), 12 deletions(-) rename tonic/src/transport/{ => channel}/service/executor.rs (100%) diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs index 054cbe78f..4961e03b6 100644 --- a/tonic/src/transport/channel/endpoint.rs +++ b/tonic/src/transport/channel/endpoint.rs @@ -1,9 +1,10 @@ #[cfg(feature = "tls")] use super::service::TlsConnector; +use super::service::{self, Executor, SharedExec}; +use super::Channel; #[cfg(feature = "tls")] use super::ClientTlsConfig; -use super::{service, Channel}; -use crate::transport::{service::SharedExec, Error, Executor}; +use crate::transport::Error; use bytes::Bytes; use http::{uri::Uri, HeaderValue}; use hyper::rt; diff --git a/tonic/src/transport/channel/mod.rs b/tonic/src/transport/channel/mod.rs index b273c3015..16a6ec160 100644 --- a/tonic/src/transport/channel/mod.rs +++ b/tonic/src/transport/channel/mod.rs @@ -10,10 +10,8 @@ pub use endpoint::Endpoint; #[cfg(feature = "tls")] pub use tls::ClientTlsConfig; -use self::service::{Connection, DynamicServiceStream}; -use super::service::SharedExec; +use self::service::{Connection, DynamicServiceStream, Executor, SharedExec}; use crate::body::BoxBody; -use crate::transport::Executor; use bytes::Bytes; use http::{ uri::{InvalidUri, Uri}, diff --git a/tonic/src/transport/service/executor.rs b/tonic/src/transport/channel/service/executor.rs similarity index 100% rename from tonic/src/transport/service/executor.rs rename to tonic/src/transport/channel/service/executor.rs diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 81b095aa6..39e40735e 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -19,6 +19,9 @@ pub(crate) use self::io::BoxedIo; pub(crate) mod connector; pub(crate) use self::connector::{ConnectError, Connector}; +pub(crate) mod executor; +pub(crate) use self::executor::{Executor, SharedExec}; + #[cfg(feature = "tls")] pub(crate) mod tls; #[cfg(feature = "tls")] diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index 57ed36dfe..aa9998a3f 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -118,9 +118,6 @@ pub use hyper::{body::Body, Uri}; #[cfg(feature = "tls")] pub use tokio_rustls::rustls::pki_types::CertificateDer; -#[cfg(feature = "channel")] -pub(crate) use self::service::executor::Executor; - #[cfg(all(feature = "channel", feature = "tls"))] #[cfg_attr(docsrs, doc(cfg(all(feature = "channel", feature = "tls"))))] pub use self::channel::ClientTlsConfig; diff --git a/tonic/src/transport/service/mod.rs b/tonic/src/transport/service/mod.rs index 0f97c15ce..b5904aa07 100644 --- a/tonic/src/transport/service/mod.rs +++ b/tonic/src/transport/service/mod.rs @@ -1,12 +1,8 @@ -#[cfg(feature = "channel")] -pub(crate) mod executor; pub(crate) mod grpc_timeout; mod io; #[cfg(feature = "tls")] pub(crate) mod tls; -#[cfg(feature = "channel")] -pub(crate) use self::executor::SharedExec; pub(crate) use self::grpc_timeout::GrpcTimeout; pub(crate) use self::io::ServerIo; #[cfg(feature = "tls")] From 76128a42b0b90a29ea8169d7627d10412a24f45e Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 24 May 2024 07:14:40 +0900 Subject: [PATCH 12/14] chore(channel): Clean up scope of channel service module --- tonic/src/status.rs | 4 ++- .../transport/channel/service/connection.rs | 3 +- tonic/src/transport/channel/service/mod.rs | 34 +++++++++---------- tonic/src/transport/mod.rs | 2 -- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 0a22226f1..a1c2eb016 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -619,7 +619,9 @@ fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option { // > The service is currently unavailable. This is most likely a transient condition that // > can be corrected if retried with a backoff. #[cfg(feature = "channel")] - if let Some(connect) = err.downcast_ref::() { + if let Some(connect) = + err.downcast_ref::() + { return Some(Status::unavailable(connect.to_string())); } diff --git a/tonic/src/transport/channel/service/connection.rs b/tonic/src/transport/channel/service/connection.rs index 1e84e9b5d..2c34f3ed7 100644 --- a/tonic/src/transport/channel/service/connection.rs +++ b/tonic/src/transport/channel/service/connection.rs @@ -1,5 +1,4 @@ -use super::{AddOrigin, Reconnect, UserAgent}; -use crate::transport::service::SharedExec; +use super::{AddOrigin, Reconnect, SharedExec, UserAgent}; use crate::{ body::{boxed, BoxBody}, transport::{service::GrpcTimeout, BoxFuture, Endpoint}, diff --git a/tonic/src/transport/channel/service/mod.rs b/tonic/src/transport/channel/service/mod.rs index 39e40735e..6ac7603a9 100644 --- a/tonic/src/transport/channel/service/mod.rs +++ b/tonic/src/transport/channel/service/mod.rs @@ -1,28 +1,28 @@ -pub(crate) mod add_origin; -pub(crate) use self::add_origin::AddOrigin; +mod add_origin; +use self::add_origin::AddOrigin; -pub(crate) mod user_agent; -pub(crate) use self::user_agent::UserAgent; +mod user_agent; +use self::user_agent::UserAgent; -pub(crate) mod reconnect; -pub(crate) use self::reconnect::Reconnect; +mod reconnect; +use self::reconnect::Reconnect; -pub(crate) mod connection; -pub(crate) use self::connection::Connection; +mod connection; +pub(super) use self::connection::Connection; -pub(crate) mod discover; -pub(crate) use self::discover::DynamicServiceStream; +mod discover; +pub(super) use self::discover::DynamicServiceStream; -pub(crate) mod io; -pub(crate) use self::io::BoxedIo; +mod io; +use self::io::BoxedIo; -pub(crate) mod connector; +mod connector; pub(crate) use self::connector::{ConnectError, Connector}; -pub(crate) mod executor; -pub(crate) use self::executor::{Executor, SharedExec}; +mod executor; +pub(super) use self::executor::{Executor, SharedExec}; #[cfg(feature = "tls")] -pub(crate) mod tls; +mod tls; #[cfg(feature = "tls")] -pub(crate) use self::tls::TlsConnector; +pub(super) use self::tls::TlsConnector; diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index aa9998a3f..9ee24a557 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -98,8 +98,6 @@ mod service; #[cfg(feature = "tls")] mod tls; -#[cfg(feature = "channel")] -pub(crate) use self::channel::service::ConnectError; #[doc(inline)] #[cfg(feature = "channel")] #[cfg_attr(docsrs, doc(cfg(feature = "channel")))] From 2724a967a3723af79f36ad7431efd9e9ff6826a7 Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 24 May 2024 07:19:07 +0900 Subject: [PATCH 13/14] chore(channel): Clean up importing in channel service --- tonic/src/transport/channel/service/discover.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tonic/src/transport/channel/service/discover.rs b/tonic/src/transport/channel/service/discover.rs index 5c15d3365..b1d3c3331 100644 --- a/tonic/src/transport/channel/service/discover.rs +++ b/tonic/src/transport/channel/service/discover.rs @@ -1,4 +1,4 @@ -use crate::transport::{channel::service::Connection, Endpoint}; +use super::super::{Connection, Endpoint}; use hyper_util::client::legacy::connect::HttpConnector; use std::{ From 3b54baefa5f7d25805592673a6ad1688ba0fbbef Mon Sep 17 00:00:00 2001 From: tottoto Date: Thu, 13 Jun 2024 23:49:51 +0900 Subject: [PATCH 14/14] chore(doc): Update feature flag document about transport and channel --- tonic/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index 29137536f..1ca537e52 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -16,10 +16,9 @@ //! //! # Feature Flags //! -//! - `transport`: Enables the fully featured, batteries included client and server -//! implementation based on [`hyper`], [`tower`] and [`tokio`]. Enabled by default. -//! - `channel`: Enables just the full featured channel/client portion of the `transport` -//! feature. +//! - `transport`: Enables just the full featured server portion of the `channel` feature. +//! - `channel`: Enables the fully featured, batteries included client and server +//! implementation based on [`hyper`], [`tower`] and [`tokio`]. Enabled by default. //! - `codegen`: Enables all the required exports and optional dependencies required //! for [`tonic-build`]. Enabled by default. //! - `tls`: Enables the `rustls` based TLS options for the `transport` feature. Not