Skip to content

Commit 1359d7a

Browse files
authored
chore(examples): Refactor tower example (#1999)
* chore(examples): Use http crate directly in tower example * chore(examples): Remove unused hyper-util from tower example * chore(examples): Remove BoxBody from tower example
1 parent cb534a7 commit 1359d7a

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ tracing = ["dep:tracing", "dep:tracing-subscriber"]
272272
uds = ["tokio-stream/net", "dep:tower", "dep:hyper", "dep:hyper-util"]
273273
streaming = ["tokio-stream", "dep:h2"]
274274
mock = ["tokio-stream", "dep:tower", "dep:hyper-util"]
275-
tower = ["dep:hyper", "dep:hyper-util", "dep:tower", "tower?/timeout", "dep:http"]
275+
tower = ["dep:tower", "tower?/timeout", "dep:http"]
276276
json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"]
277277
compression = ["tonic/gzip"]
278278
tls = ["tonic/tls"]

examples/src/tower/server.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
task::{Context, Poll},
44
time::Duration,
55
};
6-
use tonic::{body::BoxBody, transport::Server, Request, Response, Status};
6+
use tonic::{transport::Server, Request, Response, Status};
77
use tower::{Layer, Service};
88

99
use hello_world::greeter_server::{Greeter, GreeterServer};
@@ -83,13 +83,11 @@ struct MyMiddleware<S> {
8383

8484
type BoxFuture<'a, T> = Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
8585

86-
impl<S> Service<hyper::Request<BoxBody>> for MyMiddleware<S>
86+
impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for MyMiddleware<S>
8787
where
88-
S: Service<hyper::Request<BoxBody>, Response = hyper::Response<BoxBody>>
89-
+ Clone
90-
+ Send
91-
+ 'static,
88+
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
9289
S::Future: Send + 'static,
90+
ReqBody: Send + 'static,
9391
{
9492
type Response = S::Response;
9593
type Error = S::Error;
@@ -99,7 +97,7 @@ where
9997
self.inner.poll_ready(cx)
10098
}
10199

102-
fn call(&mut self, req: hyper::Request<BoxBody>) -> Self::Future {
100+
fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
103101
// This is necessary because tonic internally uses `tower::buffer::Buffer`.
104102
// See https://github.com/tower-rs/tower/issues/547#issuecomment-767629149
105103
// for details on why this is necessary

0 commit comments

Comments
 (0)