Skip to content

Commit 0846e6e

Browse files
authored
feat(transport): Make tower internal dependency (#1947)
* feat(channel): Add Change type to make tower internal dependency * feat(server): Remove NamedService implementation for Either * feat(web): Add CorsGrpcWebResponseFuture to make tower-http internal dependency
1 parent 6dac819 commit 0846e6e

File tree

9 files changed

+53
-29
lines changed

9 files changed

+53
-29
lines changed

examples/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ path = "src/load_balance/server.rs"
5151
[[bin]]
5252
name = "dynamic-load-balance-client"
5353
path = "src/dynamic_load_balance/client.rs"
54-
required-features = ["dynamic-load-balance"]
5554

5655
[[bin]]
5756
name = "dynamic-load-balance-server"
5857
path = "src/dynamic_load_balance/server.rs"
59-
required-features = ["dynamic-load-balance"]
6058

6159
[[bin]]
6260
name = "tls-client"
@@ -267,13 +265,12 @@ json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"]
267265
compression = ["tonic/gzip"]
268266
tls = ["tonic/tls"]
269267
tls-rustls = ["dep:http", "dep:hyper", "dep:hyper-util", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pki-types", "dep:tokio-rustls", "dep:pin-project", "dep:http-body-util"]
270-
dynamic-load-balance = ["dep:tower"]
271268
tls-client-auth = ["tonic/tls"]
272269
types = ["dep:tonic-types"]
273270
h2c = ["dep:hyper", "dep:tower", "dep:http", "dep:hyper-util"]
274271
cancellation = ["dep:tokio-util"]
275272

276-
full = ["gcp", "routeguide", "reflection", "autoreload", "health", "grpc-web", "tracing", "uds", "streaming", "mock", "tower", "json-codec", "compression", "tls", "tls-rustls", "dynamic-load-balance", "tls-client-auth", "types", "cancellation", "h2c"]
273+
full = ["gcp", "routeguide", "reflection", "autoreload", "health", "grpc-web", "tracing", "uds", "streaming", "mock", "tower", "json-codec", "compression", "tls", "tls-rustls", "tls-client-auth", "types", "cancellation", "h2c"]
277274
default = ["full"]
278275

279276
[dependencies]

examples/src/dynamic_load_balance/client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ pub mod pb {
33
}
44

55
use pb::{echo_client::EchoClient, EchoRequest};
6+
use tonic::transport::channel::Change;
67
use tonic::transport::Channel;
7-
88
use tonic::transport::Endpoint;
99

1010
use std::sync::Arc;
1111

1212
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
1313
use tokio::time::timeout;
14-
use tower::discover::Change;
1514

1615
#[tokio::main]
1716
async fn main() -> Result<(), Box<dyn std::error::Error>> {

tonic-web/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ allowed_external_types = [
4343
# not major released
4444
"futures_core::stream::Stream",
4545
"http_body_util::combinators::box_body::UnsyncBoxBody",
46-
"tower_http::cors::Cors",
4746
"tower_layer::Layer",
4847
"tower_service::Service",
4948
]

tonic-web/src/lib.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ mod layer;
108108
mod service;
109109

110110
use http::header::HeaderName;
111-
use std::time::Duration;
111+
use pin_project::pin_project;
112+
use std::{
113+
fmt,
114+
future::Future,
115+
pin::Pin,
116+
task::{Context, Poll},
117+
time::Duration,
118+
};
112119
use tonic::{body::BoxBody, server::NamedService, Status};
113120
use tower_http::cors::{AllowOrigin, CorsLayer};
114121
use tower_layer::Layer;
@@ -159,18 +166,37 @@ where
159166
{
160167
type Response = S::Response;
161168
type Error = S::Error;
162-
type Future =
163-
<tower_http::cors::Cors<GrpcWebService<S>> as Service<http::Request<BoxBody>>>::Future;
169+
type Future = CorsGrpcWebResponseFuture<S::Future>;
164170

165-
fn poll_ready(
166-
&mut self,
167-
cx: &mut std::task::Context<'_>,
168-
) -> std::task::Poll<Result<(), Self::Error>> {
171+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
169172
self.0.poll_ready(cx)
170173
}
171174

172175
fn call(&mut self, req: http::Request<BoxBody>) -> Self::Future {
173-
self.0.call(req)
176+
CorsGrpcWebResponseFuture(self.0.call(req))
177+
}
178+
}
179+
180+
/// Response Future for the [`CorsGrpcWeb`].
181+
#[pin_project]
182+
pub struct CorsGrpcWebResponseFuture<F>(
183+
#[pin] tower_http::cors::ResponseFuture<service::ResponseFuture<F>>,
184+
);
185+
186+
impl<F, E> Future for CorsGrpcWebResponseFuture<F>
187+
where
188+
F: Future<Output = Result<http::Response<BoxBody>, E>>,
189+
{
190+
type Output = Result<http::Response<BoxBody>, E>;
191+
192+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
193+
self.project().0.poll(cx)
194+
}
195+
}
196+
197+
impl<F> fmt::Debug for CorsGrpcWebResponseFuture<F> {
198+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199+
f.debug_tuple("CorsGrpcWebResponseFuture").finish()
174200
}
175201
}
176202

tonic/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ allowed_external_types = [
137137
"futures_core::stream::Stream",
138138
"h2::error::Error",
139139
"http_body_util::combinators::box_body::UnsyncBoxBody",
140-
"tower::discover::Change",
141140
"tower_service::Service",
142141
"tower_layer::Layer",
143142
"tower_layer::stack::Stack",

tonic/src/transport/channel/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub(crate) mod service;
55
#[cfg(feature = "_tls-any")]
66
mod tls;
77

8+
pub use self::service::Change;
89
pub use endpoint::Endpoint;
910
#[cfg(feature = "_tls-any")]
1011
pub use tls::ClientTlsConfig;
@@ -29,7 +30,7 @@ use hyper::rt;
2930
use tower::balance::p2c::Balance;
3031
use tower::{
3132
buffer::{future::ResponseFuture as BufferResponseFuture, Buffer},
32-
discover::{Change, Discover},
33+
discover::Discover,
3334
util::BoxService,
3435
Service,
3536
};

tonic/src/transport/channel/service/discover.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ use std::{
77
task::{Context, Poll},
88
};
99
use tokio::sync::mpsc::Receiver;
10-
1110
use tokio_stream::Stream;
12-
use tower::discover::Change;
13-
14-
type DiscoverResult<K, S, E> = Result<Change<K, S>, E>;
11+
use tower::discover::Change as TowerChange;
12+
13+
/// A change in the service set.
14+
#[derive(Debug, Clone)]
15+
pub enum Change<K, V> {
16+
/// A new service identified by key `K` was identified.
17+
Insert(K, V),
18+
/// The service identified by key `K` disappeared.
19+
Remove(K),
20+
}
1521

1622
pub(crate) struct DynamicServiceStream<K: Hash + Eq + Clone> {
1723
changes: Receiver<Change<K, Endpoint>>,
@@ -24,7 +30,7 @@ impl<K: Hash + Eq + Clone> DynamicServiceStream<K> {
2430
}
2531

2632
impl<K: Hash + Eq + Clone> Stream for DynamicServiceStream<K> {
27-
type Item = DiscoverResult<K, Connection, crate::BoxError>;
33+
type Item = Result<TowerChange<K, Connection>, crate::BoxError>;
2834

2935
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
3036
let c = &mut self.changes;
@@ -39,10 +45,10 @@ impl<K: Hash + Eq + Clone> Stream for DynamicServiceStream<K> {
3945
http.enforce_http(false);
4046

4147
let connection = Connection::lazy(endpoint.connector(http), endpoint);
42-
let change = Ok(Change::Insert(k, connection));
48+
let change = Ok(TowerChange::Insert(k, connection));
4349
Poll::Ready(Some(change))
4450
}
45-
Change::Remove(k) => Poll::Ready(Some(Ok(Change::Remove(k)))),
51+
Change::Remove(k) => Poll::Ready(Some(Ok(TowerChange::Remove(k)))),
4652
},
4753
}
4854
}

tonic/src/transport/channel/service/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod connection;
1111
pub(super) use self::connection::Connection;
1212

1313
mod discover;
14+
pub use self::discover::Change;
1415
pub(super) use self::discover::DynamicServiceStream;
1516

1617
mod io;

tonic/src/transport/server/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use tower::{
6464
layer::util::{Identity, Stack},
6565
layer::Layer,
6666
limit::concurrency::ConcurrencyLimitLayer,
67-
util::{BoxCloneService, Either},
67+
util::BoxCloneService,
6868
Service, ServiceBuilder, ServiceExt,
6969
};
7070

@@ -138,10 +138,6 @@ pub struct Router<L = Identity> {
138138
routes: Routes,
139139
}
140140

141-
impl<S: NamedService, T> NamedService for Either<S, T> {
142-
const NAME: &'static str = S::NAME;
143-
}
144-
145141
impl Server {
146142
/// Create a new server builder that can configure a [`Server`].
147143
pub fn builder() -> Self {

0 commit comments

Comments
 (0)