Skip to content

Commit 4aa7354

Browse files
committed
feat(channel): Make channel feature additive
1 parent 18a2b30 commit 4aa7354

File tree

7 files changed

+66
-27
lines changed

7 files changed

+66
-27
lines changed

examples/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,13 @@ hyper-warp-multiplex = ["hyper-warp"]
298298
uds = ["tokio-stream/net", "dep:tower", "dep:hyper"]
299299
streaming = ["tokio-stream", "dep:h2"]
300300
mock = ["tokio-stream", "dep:tower"]
301-
tower = ["dep:hyper", "dep:tower", "dep:http"]
301+
tower = ["dep:hyper", "tower/timeout", "dep:http"]
302302
json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"]
303303
compression = ["tonic/gzip"]
304304
tls = ["tonic/tls"]
305305
tls-rustls = ["dep:hyper", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pemfile", "dep:tokio-rustls"]
306306
dynamic-load-balance = ["dep:tower"]
307-
timeout = ["tokio/time", "dep:tower"]
307+
timeout = ["tokio/time", "tower/timeout"]
308308
tls-client-auth = ["tonic/tls"]
309309
types = ["dep:tonic-types"]
310310
h2c = ["dep:hyper", "dep:tower", "dep:http"]

tonic/Cargo.toml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@ version = "0.11.0"
2626
codegen = ["dep:async-trait"]
2727
gzip = ["dep:flate2"]
2828
zstd = ["dep:zstd"]
29-
default = ["transport", "codegen", "prost"]
29+
default = ["channel", "codegen", "prost"]
3030
prost = ["dep:prost"]
3131
tls = ["dep:rustls-pki-types", "dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"]
3232
tls-roots = ["tls-roots-common", "dep:rustls-native-certs"]
33-
tls-roots-common = ["tls"]
33+
tls-roots-common = ["tls", "channel"]
3434
tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"]
3535
transport = [
3636
"dep:async-stream",
3737
"dep:axum",
38-
"channel",
3938
"dep:h2",
40-
"dep:hyper",
39+
"dep:hyper", "hyper?/server",
4140
"dep:tokio", "tokio?/net", "tokio?/time",
42-
"dep:tower",
41+
"dep:tower", "tower?/util", "tower?/limit",
42+
]
43+
channel = [
44+
"transport",
45+
"dep:hyper", "hyper?/client",
46+
"dep:tower", "tower?/balance", "tower?/buffer", "tower?/discover", "tower?/load", "tower?/make",
4347
"dep:hyper-timeout",
4448
]
45-
channel = []
4649

4750
# [[bench]]
4851
# name = "bench_main"
@@ -68,13 +71,15 @@ async-trait = {version = "0.1.13", optional = true}
6871

6972
# transport
7073
h2 = {version = "0.3.24", optional = true}
71-
hyper = {version = "0.14.26", features = ["full"], optional = true}
72-
hyper-timeout = {version = "0.4", optional = true}
74+
hyper = {version = "0.14.26", features = ["http1", "http2", "runtime", "stream"], optional = true}
7375
tokio = {version = "1.0.1", optional = true}
7476
tokio-stream = "0.1"
75-
tower = {version = "0.4.7", default-features = false, features = ["balance", "buffer", "discover", "limit", "load", "make", "timeout", "util"], optional = true}
77+
tower = {version = "0.4.7", default-features = false, optional = true}
7678
axum = {version = "0.6.9", default_features = false, optional = true}
7779

80+
# channel
81+
hyper-timeout = {version = "0.4", optional = true}
82+
7883
# rustls
7984
async-stream = { version = "0.3", optional = true }
8085
rustls-pki-types = { version = "1.0", optional = true }

tonic/src/transport/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ struct ErrorImpl {
1515
#[derive(Debug)]
1616
pub(crate) enum Kind {
1717
Transport,
18+
#[cfg(feature = "channel")]
1819
InvalidUri,
20+
#[cfg(feature = "channel")]
1921
InvalidUserAgent,
2022
}
2123

@@ -35,18 +37,22 @@ impl Error {
3537
Error::new(Kind::Transport).with(source)
3638
}
3739

40+
#[cfg(feature = "channel")]
3841
pub(crate) fn new_invalid_uri() -> Self {
3942
Error::new(Kind::InvalidUri)
4043
}
4144

45+
#[cfg(feature = "channel")]
4246
pub(crate) fn new_invalid_user_agent() -> Self {
4347
Error::new(Kind::InvalidUserAgent)
4448
}
4549

4650
fn description(&self) -> &str {
4751
match &self.inner.kind {
4852
Kind::Transport => "transport error",
53+
#[cfg(feature = "channel")]
4954
Kind::InvalidUri => "invalid URI",
55+
#[cfg(feature = "channel")]
5056
Kind::InvalidUserAgent => "user agent is not a valid header value",
5157
}
5258
}

tonic/src/transport/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
//!
8888
//! [rustls]: https://docs.rs/rustls/0.16.0/rustls/
8989
90+
#[cfg(feature = "channel")]
9091
pub mod channel;
9192
pub mod server;
9293

@@ -110,10 +111,11 @@ pub use self::tls::Certificate;
110111
pub use axum::{body::BoxBody as AxumBoxBody, Router as AxumRouter};
111112
pub use hyper::{Body, Uri};
112113

114+
#[cfg(feature = "channel")]
113115
pub(crate) use self::service::executor::Executor;
114116

115-
#[cfg(feature = "tls")]
116-
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
117+
#[cfg(all(feature = "channel", feature = "tls"))]
118+
#[cfg_attr(docsrs, doc(cfg(all(feature = "channel", feature = "tls"))))]
117119
pub use self::channel::ClientTlsConfig;
118120
#[cfg(feature = "tls")]
119121
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
@@ -122,4 +124,5 @@ pub use self::server::ServerTlsConfig;
122124
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
123125
pub use self::tls::Identity;
124126

127+
#[cfg(feature = "channel")]
125128
type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;

tonic/src/transport/service/io.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::transport::server::Connected;
2+
#[cfg(feature = "channel")]
23
use hyper::client::connect::{Connected as HyperConnected, Connection};
34
use std::io;
45
use std::io::IoSlice;
@@ -15,20 +16,24 @@ pub(in crate::transport) trait Io:
1516

1617
impl<T> Io for T where T: AsyncRead + AsyncWrite + Send + 'static {}
1718

19+
#[cfg(feature = "channel")]
1820
pub(crate) struct BoxedIo(Pin<Box<dyn Io>>);
1921

22+
#[cfg(feature = "channel")]
2023
impl BoxedIo {
2124
pub(in crate::transport) fn new<I: Io>(io: I) -> Self {
2225
BoxedIo(Box::pin(io))
2326
}
2427
}
2528

29+
#[cfg(feature = "channel")]
2630
impl Connection for BoxedIo {
2731
fn connected(&self) -> HyperConnected {
2832
HyperConnected::new()
2933
}
3034
}
3135

36+
#[cfg(feature = "channel")]
3237
impl Connected for BoxedIo {
3338
type ConnectInfo = NoneConnectInfo;
3439

@@ -37,9 +42,11 @@ impl Connected for BoxedIo {
3742
}
3843
}
3944

45+
#[cfg(feature = "channel")]
4046
#[derive(Copy, Clone)]
4147
pub(crate) struct NoneConnectInfo;
4248

49+
#[cfg(feature = "channel")]
4350
impl AsyncRead for BoxedIo {
4451
fn poll_read(
4552
mut self: Pin<&mut Self>,
@@ -50,6 +57,7 @@ impl AsyncRead for BoxedIo {
5057
}
5158
}
5259

60+
#[cfg(feature = "channel")]
5361
impl AsyncWrite for BoxedIo {
5462
fn poll_write(
5563
mut self: Pin<&mut Self>,

tonic/src/transport/service/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1+
#[cfg(feature = "channel")]
12
mod add_origin;
3+
#[cfg(feature = "channel")]
24
mod connection;
5+
#[cfg(feature = "channel")]
36
mod connector;
7+
#[cfg(feature = "channel")]
48
mod discover;
9+
#[cfg(feature = "channel")]
510
pub(crate) mod executor;
611
pub(crate) mod grpc_timeout;
712
mod io;
13+
#[cfg(feature = "channel")]
814
mod reconnect;
915
mod router;
1016
#[cfg(feature = "tls")]
1117
mod tls;
18+
#[cfg(feature = "channel")]
1219
mod user_agent;
1320

14-
pub(crate) use self::add_origin::AddOrigin;
15-
pub(crate) use self::connection::Connection;
16-
pub(crate) use self::connector::Connector;
17-
pub(crate) use self::discover::DynamicServiceStream;
18-
pub(crate) use self::executor::SharedExec;
1921
pub(crate) use self::grpc_timeout::GrpcTimeout;
2022
pub(crate) use self::io::ServerIo;
2123
#[cfg(feature = "tls")]
22-
pub(crate) use self::tls::{TlsAcceptor, TlsConnector};
23-
pub(crate) use self::user_agent::UserAgent;
24+
pub(crate) use self::tls::TlsAcceptor;
25+
#[cfg(all(feature = "channel", feature = "tls"))]
26+
pub(crate) use self::tls::TlsConnector;
27+
#[cfg(feature = "channel")]
28+
pub(crate) use self::{
29+
add_origin::AddOrigin, connection::Connection, connector::Connector,
30+
discover::DynamicServiceStream, executor::SharedExec, user_agent::UserAgent,
31+
};
2432

2533
pub use self::router::Routes;
2634
pub use self::router::RoutesBuilder;

tonic/src/transport/service/tls.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
use std::{
2-
io::Cursor,
3-
{fmt, sync::Arc},
4-
};
1+
use std::io::Cursor;
2+
use std::{fmt, sync::Arc};
53

6-
use rustls_pki_types::{CertificateDer, PrivateKeyDer, ServerName};
4+
#[cfg(feature = "channel")]
5+
use rustls_pki_types::ServerName;
6+
use rustls_pki_types::{CertificateDer, PrivateKeyDer};
77
use tokio::io::{AsyncRead, AsyncWrite};
8+
#[cfg(feature = "channel")]
9+
use tokio_rustls::{rustls::ClientConfig, TlsConnector as RustlsConnector};
810
use tokio_rustls::{
9-
rustls::{server::WebPkiClientVerifier, ClientConfig, RootCertStore, ServerConfig},
10-
TlsAcceptor as RustlsAcceptor, TlsConnector as RustlsConnector,
11+
rustls::{server::WebPkiClientVerifier, RootCertStore, ServerConfig},
12+
TlsAcceptor as RustlsAcceptor,
1113
};
1214

15+
#[cfg(feature = "channel")]
1316
use super::io::BoxedIo;
1417
use crate::transport::{
1518
server::{Connected, TlsStream},
@@ -21,17 +24,20 @@ const ALPN_H2: &[u8] = b"h2";
2124

2225
#[derive(Debug)]
2326
enum TlsError {
27+
#[cfg(feature = "channel")]
2428
H2NotNegotiated,
2529
CertificateParseError,
2630
PrivateKeyParseError,
2731
}
2832

33+
#[cfg(feature = "channel")]
2934
#[derive(Clone)]
3035
pub(crate) struct TlsConnector {
3136
config: Arc<ClientConfig>,
3237
domain: Arc<ServerName<'static>>,
3338
}
3439

40+
#[cfg(feature = "channel")]
3541
impl TlsConnector {
3642
pub(crate) fn new(
3743
ca_cert: Option<Certificate>,
@@ -67,6 +73,7 @@ impl TlsConnector {
6773
})
6874
}
6975

76+
#[cfg(feature = "channel")]
7077
pub(crate) async fn connect<I>(&self, io: I) -> Result<BoxedIo, crate::Error>
7178
where
7279
I: AsyncRead + AsyncWrite + Send + Unpin + 'static,
@@ -84,6 +91,7 @@ impl TlsConnector {
8491
}
8592
}
8693

94+
#[cfg(feature = "channel")]
8795
impl fmt::Debug for TlsConnector {
8896
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8997
f.debug_struct("TlsConnector").finish()
@@ -145,6 +153,7 @@ impl fmt::Debug for TlsAcceptor {
145153
impl fmt::Display for TlsError {
146154
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147155
match self {
156+
#[cfg(feature = "channel")]
148157
TlsError::H2NotNegotiated => write!(f, "HTTP/2 was not negotiated."),
149158
TlsError::CertificateParseError => write!(f, "Error parsing TLS certificate."),
150159
TlsError::PrivateKeyParseError => write!(

0 commit comments

Comments
 (0)