Skip to content

Commit 32dbd14

Browse files
committed
feat(channel): Make channel feature additive
1 parent 177c1f3 commit 32dbd14

File tree

7 files changed

+65
-24
lines changed

7 files changed

+65
-24
lines changed

examples/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ hyper-warp-multiplex = ["hyper-warp"]
290290
uds = ["tokio-stream/net", "dep:tower", "dep:hyper"]
291291
streaming = ["tokio-stream", "dep:h2"]
292292
mock = ["tokio-stream", "dep:tower"]
293-
tower = ["dep:hyper", "dep:tower", "dep:http"]
293+
tower = ["dep:hyper", "tower/timeout", "dep:http"]
294294
json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"]
295295
compression = ["tonic/gzip"]
296296
tls = ["tonic/tls"]
297297
tls-rustls = ["dep:hyper", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pemfile", "dep:tokio-rustls"]
298298
dynamic-load-balance = ["dep:tower"]
299-
timeout = ["tokio/time", "dep:tower"]
299+
timeout = ["tokio/time", "tower/timeout"]
300300
tls-client-auth = ["tonic/tls"]
301301
types = ["dep:tonic-types"]
302302
h2c = ["dep:hyper", "dep:tower", "dep:http"]

tonic/Cargo.toml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ version = "0.10.2"
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-pemfile", "transport", "dep:tokio-rustls", "dep:rustls", "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",
41-
"tokio/net",
42-
"tokio/time",
43-
"dep:tower",
39+
"dep:hyper", "hyper?/server",
40+
"tokio/net", "tokio/time",
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",
4447
"dep:hyper-timeout",
4548
]
46-
channel = []
4749

4850
# [[bench]]
4951
# name = "bench_main"
@@ -70,12 +72,14 @@ async-trait = {version = "0.1.13", optional = true}
7072

7173
# transport
7274
h2 = {version = "0.3.17", optional = true}
73-
hyper = {version = "0.14.26", features = ["full"], optional = true}
74-
hyper-timeout = {version = "0.4", optional = true}
75+
hyper = {version = "0.14.26", features = ["http1", "http2", "runtime", "stream"], optional = true}
7576
tokio-stream = "0.1"
76-
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}
7778
axum = {version = "0.6.9", default_features = false, optional = true}
7879

80+
# channel
81+
hyper-timeout = {version = "0.4", optional = true}
82+
7983
# rustls
8084
async-stream = { version = "0.3", optional = true }
8185
rustls-pemfile = { 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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1+
#[cfg(feature = "channel")]
12
use super::io::BoxedIo;
23
use crate::transport::{
34
server::{Connected, TlsStream},
45
Certificate, Identity,
56
};
67
use std::{fmt, sync::Arc};
78
use tokio::io::{AsyncRead, AsyncWrite};
9+
#[cfg(feature = "channel")]
810
use tokio_rustls::{
9-
rustls::{ClientConfig, RootCertStore, ServerConfig, ServerName},
10-
TlsAcceptor as RustlsAcceptor, TlsConnector as RustlsConnector,
11+
rustls::{ClientConfig, ServerName},
12+
TlsConnector as RustlsConnector,
13+
};
14+
use tokio_rustls::{
15+
rustls::{RootCertStore, ServerConfig},
16+
TlsAcceptor as RustlsAcceptor,
1117
};
1218

1319
/// h2 alpn in plain format for rustls.
1420
const ALPN_H2: &str = "h2";
1521

1622
#[derive(Debug)]
1723
enum TlsError {
24+
#[cfg(feature = "channel")]
1825
H2NotNegotiated,
1926
CertificateParseError,
2027
PrivateKeyParseError,
2128
}
2229

30+
#[cfg(feature = "channel")]
2331
#[derive(Clone)]
2432
pub(crate) struct TlsConnector {
2533
config: Arc<ClientConfig>,
2634
domain: Arc<ServerName>,
2735
}
2836

37+
#[cfg(feature = "channel")]
2938
impl TlsConnector {
3039
pub(crate) fn new(
3140
ca_cert: Option<Certificate>,
@@ -67,6 +76,7 @@ impl TlsConnector {
6776
})
6877
}
6978

79+
#[cfg(feature = "channel")]
7080
pub(crate) async fn connect<I>(&self, io: I) -> Result<BoxedIo, crate::Error>
7181
where
7282
I: AsyncRead + AsyncWrite + Send + Unpin + 'static,
@@ -90,6 +100,7 @@ impl TlsConnector {
90100
}
91101
}
92102

103+
#[cfg(feature = "channel")]
93104
impl fmt::Debug for TlsConnector {
94105
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95106
f.debug_struct("TlsConnector").finish()
@@ -154,6 +165,7 @@ impl fmt::Debug for TlsAcceptor {
154165
impl fmt::Display for TlsError {
155166
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156167
match self {
168+
#[cfg(feature = "channel")]
157169
TlsError::H2NotNegotiated => write!(f, "HTTP/2 was not negotiated."),
158170
TlsError::CertificateParseError => write!(f, "Error parsing TLS certificate."),
159171
TlsError::PrivateKeyParseError => write!(

0 commit comments

Comments
 (0)