Skip to content

Commit d258fb8

Browse files
committed
feat(transport): Make transport server and channel independent
1 parent 1fe8cb0 commit d258fb8

File tree

15 files changed

+136
-110
lines changed

15 files changed

+136
-110
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ jobs:
6060
with:
6161
tool: protoc@${{ env.PROTOC_VERSION }}
6262
- uses: Swatinem/rust-cache@v2
63-
- run: cargo hack udeps --workspace --each-feature ${{ matrix.option }}
63+
- run: cargo hack udeps --workspace --exclude-features tls --each-feature ${{ matrix.option }}
64+
- run: cargo udeps --package tonic --features tls,transport
65+
- run: cargo udeps --package tonic --features tls,server
66+
- run: cargo udeps --package tonic --features tls,channel
6467

6568
check:
6669
runs-on: ${{ matrix.os }}

tonic/Cargo.toml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,29 @@ version = "0.11.0"
2626
codegen = ["dep:async-trait"]
2727
gzip = ["dep:flate2"]
2828
zstd = ["dep:zstd"]
29-
default = ["channel", "codegen", "prost"]
29+
default = ["channel", "codegen", "prost", "server"]
3030
prost = ["dep:prost"]
31-
tls = ["dep:rustls-pki-types", "dep:rustls-pemfile", "transport", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"]
31+
tls = ["dep:rustls-pki-types", "dep:rustls-pemfile", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"]
3232
tls-roots = ["tls-roots-common", "dep:rustls-native-certs"]
3333
tls-roots-common = ["tls", "channel"]
3434
tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"]
3535
transport = [
36+
"dep:tower", "tower?/util", "tower?/limit",
37+
"dep:tokio", "tokio?/time",
38+
"dep:hyper",
39+
]
40+
server = [
41+
"transport",
3642
"dep:async-stream",
43+
"tokio?/net",
3744
"dep:axum",
3845
"dep:h2",
39-
"dep:hyper", "hyper?/server",
40-
"dep:tokio", "tokio?/net", "tokio?/time",
41-
"dep:tower", "tower?/util", "tower?/limit",
46+
"hyper?/server",
4247
]
4348
channel = [
4449
"transport",
45-
"dep:hyper", "hyper?/client",
46-
"dep:tower", "tower?/balance", "tower?/buffer", "tower?/discover", "tower?/load", "tower?/make",
50+
"hyper?/client",
51+
"tower?/balance", "tower?/buffer", "tower?/discover", "tower?/load", "tower?/make",
4752
"dep:hyper-timeout",
4853
]
4954

tonic/src/request.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::metadata::{MetadataMap, MetadataValue};
2-
#[cfg(feature = "transport")]
2+
#[cfg(feature = "server")]
33
use crate::transport::server::TcpConnectInfo;
4-
#[cfg(feature = "tls")]
4+
#[cfg(all(feature = "server", feature = "tls"))]
55
use crate::transport::{server::TlsConnectInfo, Certificate};
66
use crate::Extensions;
7-
#[cfg(feature = "transport")]
7+
#[cfg(feature = "server")]
88
use std::net::SocketAddr;
9-
#[cfg(feature = "tls")]
9+
#[cfg(all(feature = "server", feature = "tls"))]
1010
use std::sync::Arc;
1111
use std::time::Duration;
1212
use tokio_stream::Stream;
@@ -209,8 +209,8 @@ impl<T> Request<T> {
209209
/// This will return `None` if the `IO` type used
210210
/// does not implement `Connected` or when using a unix domain socket.
211211
/// This currently only works on the server side.
212-
#[cfg(feature = "transport")]
213-
#[cfg_attr(docsrs, doc(cfg(feature = "transport")))]
212+
#[cfg(feature = "server")]
213+
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
214214
pub fn local_addr(&self) -> Option<SocketAddr> {
215215
let addr = self
216216
.extensions()
@@ -232,8 +232,8 @@ impl<T> Request<T> {
232232
/// This will return `None` if the `IO` type used
233233
/// does not implement `Connected` or when using a unix domain socket.
234234
/// This currently only works on the server side.
235-
#[cfg(feature = "transport")]
236-
#[cfg_attr(docsrs, doc(cfg(feature = "transport")))]
235+
#[cfg(feature = "server")]
236+
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
237237
pub fn remote_addr(&self) -> Option<SocketAddr> {
238238
let addr = self
239239
.extensions()
@@ -256,8 +256,8 @@ impl<T> Request<T> {
256256
/// and is mostly used for mTLS. This currently only returns
257257
/// `Some` on the server side of the `transport` server with
258258
/// TLS enabled connections.
259-
#[cfg(feature = "tls")]
260-
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
259+
#[cfg(all(feature = "server", feature = "tls"))]
260+
#[cfg_attr(docsrs, doc(cfg(all(feature = "server", feature = "tls"))))]
261261
pub fn peer_certs(&self) -> Option<Arc<Vec<Certificate>>> {
262262
self.extensions()
263263
.get::<TlsConnectInfo<TcpConnectInfo>>()

tonic/src/status.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl Status {
342342
Err(err) => err,
343343
};
344344

345-
#[cfg(feature = "transport")]
345+
#[cfg(feature = "server")]
346346
let err = match err.downcast::<h2::Error>() {
347347
Ok(h2) => {
348348
return Ok(Status::from_h2_error(h2));
@@ -359,7 +359,7 @@ impl Status {
359359
}
360360

361361
// FIXME: bubble this into `transport` and expose generic http2 reasons.
362-
#[cfg(feature = "transport")]
362+
#[cfg(feature = "server")]
363363
fn from_h2_error(err: Box<h2::Error>) -> Status {
364364
let code = Self::code_from_h2(&err);
365365

@@ -368,7 +368,7 @@ impl Status {
368368
status
369369
}
370370

371-
#[cfg(feature = "transport")]
371+
#[cfg(feature = "server")]
372372
fn code_from_h2(err: &h2::Error) -> Code {
373373
// See https://github.com/grpc/grpc/blob/3977c30/doc/PROTOCOL-HTTP2.md#errors
374374
match err.reason() {
@@ -388,7 +388,7 @@ impl Status {
388388
}
389389
}
390390

391-
#[cfg(feature = "transport")]
391+
#[cfg(feature = "server")]
392392
fn to_h2_error(&self) -> h2::Error {
393393
// conservatively transform to h2 error codes...
394394
let reason = match self.code {
@@ -404,7 +404,7 @@ impl Status {
404404
///
405405
/// Returns Some if there's a way to handle the error, or None if the information from this
406406
/// hyper error, but perhaps not its source, should be ignored.
407-
#[cfg(feature = "transport")]
407+
#[cfg(feature = "server")]
408408
fn from_hyper_error(err: &hyper::Error) -> Option<Status> {
409409
// is_timeout results from hyper's keep-alive logic
410410
// (https://docs.rs/hyper/0.14.11/src/hyper/error.rs.html#192-194). Per the grpc spec
@@ -614,12 +614,12 @@ fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option<Status> {
614614
});
615615
}
616616

617-
#[cfg(feature = "transport")]
617+
#[cfg(any(feature = "server", feature = "channel"))]
618618
if let Some(timeout) = err.downcast_ref::<crate::transport::TimeoutExpired>() {
619619
return Some(Status::cancelled(timeout.to_string()));
620620
}
621621

622-
#[cfg(feature = "transport")]
622+
#[cfg(feature = "server")]
623623
if let Some(hyper) = err
624624
.downcast_ref::<hyper::Error>()
625625
.and_then(Status::from_hyper_error)
@@ -666,14 +666,14 @@ fn invalid_header_value_byte<Error: fmt::Display>(err: Error) -> Status {
666666
)
667667
}
668668

669-
#[cfg(feature = "transport")]
669+
#[cfg(feature = "server")]
670670
impl From<h2::Error> for Status {
671671
fn from(err: h2::Error) -> Self {
672672
Status::from_h2_error(Box::new(err))
673673
}
674674
}
675675

676-
#[cfg(feature = "transport")]
676+
#[cfg(feature = "server")]
677677
impl From<Status> for h2::Error {
678678
fn from(status: Status) -> Self {
679679
status.to_h2_error()

tonic/src/transport/error.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct ErrorImpl {
1414

1515
#[derive(Debug)]
1616
pub(crate) enum Kind {
17+
#[allow(unused)]
1718
Transport,
1819
#[cfg(feature = "channel")]
1920
InvalidUri,
@@ -22,17 +23,20 @@ pub(crate) enum Kind {
2223
}
2324

2425
impl Error {
25-
pub(crate) fn new(kind: Kind) -> Self {
26+
#[cfg(any(feature = "server", feature = "channel"))]
27+
fn new(kind: Kind) -> Self {
2628
Self {
2729
inner: ErrorImpl { kind, source: None },
2830
}
2931
}
3032

33+
#[cfg(any(feature = "server", feature = "channel"))]
3134
pub(crate) fn with(mut self, source: impl Into<Source>) -> Self {
3235
self.inner.source = Some(source.into());
3336
self
3437
}
3538

39+
#[cfg(any(feature = "server", feature = "channel"))]
3640
pub(crate) fn from_source(source: impl Into<crate::Error>) -> Self {
3741
Error::new(Kind::Transport).with(source)
3842
}

tonic/src/transport/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
9090
#[cfg(feature = "channel")]
9191
pub mod channel;
92+
#[cfg(feature = "server")]
9293
pub mod server;
9394

9495
mod error;
@@ -102,12 +103,16 @@ mod tls;
102103
pub use self::channel::{Channel, Endpoint};
103104
pub use self::error::Error;
104105
#[doc(inline)]
106+
#[cfg(feature = "server")]
105107
pub use self::server::Server;
106108
#[doc(inline)]
109+
#[cfg(any(feature = "server", feature = "channel"))]
107110
pub use self::service::grpc_timeout::TimeoutExpired;
108111
#[cfg(feature = "tls")]
109112
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
110113
pub use self::tls::Certificate;
114+
#[cfg(feature = "server")]
115+
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
111116
pub use axum::{body::BoxBody as AxumBoxBody, Router as AxumRouter};
112117
pub use hyper::{Body, Uri};
113118

@@ -117,8 +122,8 @@ pub(crate) use self::channel::service::executor::Executor;
117122
#[cfg(all(feature = "channel", feature = "tls"))]
118123
#[cfg_attr(docsrs, doc(cfg(all(feature = "channel", feature = "tls"))))]
119124
pub use self::channel::ClientTlsConfig;
120-
#[cfg(feature = "tls")]
121-
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
125+
#[cfg(all(feature = "server", feature = "tls"))]
126+
#[cfg_attr(docsrs, doc(cfg(all(feature = "server", feature = "tls"))))]
122127
pub use self::server::ServerTlsConfig;
123128
#[cfg(feature = "tls")]
124129
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]

tonic/src/transport/server/incoming.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use super::service::ServerIo;
12
use super::{Connected, Server};
2-
use crate::transport::service::ServerIo;
33
use hyper::server::{
44
accept::Accept,
55
conn::{AddrIncoming, AddrStream},

tonic/src/transport/server/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
mod conn;
44
mod incoming;
55
mod recover_error;
6+
mod service;
67
#[cfg(feature = "tls")]
78
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
89
mod tls;
910
#[cfg(unix)]
1011
mod unix;
1112

12-
pub use super::service::Routes;
13-
pub use super::service::RoutesBuilder;
13+
pub use self::service::{Routes, RoutesBuilder};
1414

1515
pub use conn::{Connected, TcpConnectInfo};
1616
#[cfg(feature = "tls")]
@@ -20,7 +20,7 @@ pub use tls::ServerTlsConfig;
2020
pub use conn::TlsConnectInfo;
2121

2222
#[cfg(feature = "tls")]
23-
use super::service::TlsAcceptor;
23+
use self::service::tls::TlsAcceptor;
2424

2525
#[cfg(unix)]
2626
pub use unix::UdsConnectInfo;
@@ -34,7 +34,8 @@ pub(crate) use tokio_rustls::server::TlsStream;
3434
use crate::transport::Error;
3535

3636
use self::recover_error::RecoverError;
37-
use super::service::{GrpcTimeout, ServerIo};
37+
use self::service::ServerIo;
38+
use super::service::GrpcTimeout;
3839
use crate::body::BoxBody;
3940
use crate::server::NamedService;
4041
use bytes::Bytes;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::transport::server::Connected;
21
use std::io;
32
use std::io::IoSlice;
43
use std::pin::Pin;
@@ -7,6 +6,8 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
76
#[cfg(feature = "tls")]
87
use tokio_rustls::server::TlsStream;
98

9+
use super::super::Connected;
10+
1011
pub(crate) enum ServerIo<IO> {
1112
Io(IO),
1213
#[cfg(feature = "tls")]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub(crate) mod io;
2+
pub(crate) use self::io::ServerIo;
3+
4+
mod router;
5+
pub use self::router::{Routes, RoutesBuilder};
6+
7+
#[cfg(feature = "tls")]
8+
pub(crate) mod tls;

0 commit comments

Comments
 (0)