Skip to content
This repository was archived by the owner on Mar 13, 2021. It is now read-only.

Commit 1b90a5a

Browse files
authored
Merge pull request #2 from ebkalderon/fix-example-code
Fix broken examples, remove use of RetryPolicy
2 parents f7aab6c + 8c66555 commit 1b90a5a

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ hyper = "0.12.27"
1818
tokio = "0.1.19"
1919
tower = { git = "https://github.com/tower-rs/tower" }
2020
tower-hyper = { git = "https://github.com/tower-rs/tower-hyper" }
21+
tower-reconnect = { git = "https://github.com/tower-rs/tower" }

src/http-client.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ use hyper::{
44
Request, Response, Uri,
55
};
66
use std::time::Duration;
7-
use tower::{builder::ServiceBuilder, reconnect::Reconnect, Service, ServiceExt};
8-
use tower_hyper::{
9-
client::{Builder, Connect},
10-
retry::{Body, RetryPolicy},
11-
util::Connector,
12-
};
7+
use tower::{builder::ServiceBuilder, Service, ServiceExt};
8+
use tower_hyper::{client::Connect, util::Connector, Body};
9+
use tower_reconnect::Reconnect;
1310

1411
fn main() {
1512
let fut = futures::lazy(|| {
@@ -22,29 +19,24 @@ fn main() {
2219

2320
fn request() -> impl Future<Item = Response<hyper::Body>, Error = ()> {
2421
let connector = Connector::new(HttpConnector::new(1));
25-
let hyper = Connect::new(connector, Builder::new());
22+
let hyper = Connect::new(connector);
2623

27-
// RetryPolicy is a very simple policy that retries `n` times
28-
// if the response has a 500 status code. Here, `n` is 5.
29-
let policy = RetryPolicy::new(5);
3024
// We're calling the tower/examples/server.rs.
3125
let dst = Destination::try_from_uri(Uri::from_static("http://127.0.0.1:3000")).unwrap();
3226

3327
// Now, to build the service! We use two BufferLayers in order to:
3428
// - provide backpressure for the RateLimitLayer, and ConcurrencyLimitLayer
35-
// - meet `RetryLayer`'s requirement that our service implement `Service + Clone`
3629
// - ..and to provide cheap clones on the service.
37-
let maker = ServiceBuilder::new()
30+
let service = ServiceBuilder::new()
3831
.buffer(5)
3932
.rate_limit(5, Duration::from_secs(1))
4033
.concurrency_limit(5)
41-
.retry(policy)
4234
.buffer(5)
43-
.make_service(hyper);
35+
.service(hyper);
4436

45-
// `Reconnect` accepts a destination and a MakeService, creating a new service
37+
// `Reconnect` accepts a destination and a Service, creating a new service
4638
// any time the connection encounters an error.
47-
let client = Reconnect::new(maker, dst);
39+
let client = Reconnect::new(service, dst);
4840

4941
let request = Request::builder()
5042
.method("GET")
@@ -55,9 +47,5 @@ fn request() -> impl Future<Item = Response<hyper::Body>, Error = ()> {
5547
client
5648
.ready()
5749
.map_err(|e| panic!("Service is not ready: {:?}", e))
58-
.and_then(|mut c| {
59-
c.call(request)
60-
.map(|res| res.map(|b| b.into_inner()))
61-
.map_err(|e| panic!("{:?}", e))
62-
})
50+
.and_then(|mut c| c.call(request).map_err(|e| panic!("{:?}", e)))
6351
}

src/http-server.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use futures::{future, Future, Poll, Stream};
22
use hyper::{self, Body, Request, Response};
33
use tokio::net::TcpListener;
44
use tower::{builder::ServiceBuilder, Service};
5-
use tower_hyper::{body::LiftBody, server::Server};
5+
use tower_hyper::server::Server;
66

77
fn main() {
88
hyper::rt::run(future::lazy(|| {
@@ -11,10 +11,7 @@ fn main() {
1111

1212
println!("Listening on http://{}", addr);
1313

14-
let maker = ServiceBuilder::new()
15-
.concurrency_limit(5)
16-
.make_service(MakeSvc);
17-
14+
let maker = ServiceBuilder::new().concurrency_limit(5).service(MakeSvc);
1815
let server = Server::new(maker);
1916

2017
bind.incoming()
@@ -37,17 +34,17 @@ fn main() {
3734
}
3835

3936
struct Svc;
40-
impl Service<Request<LiftBody<Body>>> for Svc {
41-
type Response = Response<&'static str>;
37+
impl Service<Request<Body>> for Svc {
38+
type Response = Response<Body>;
4239
type Error = hyper::Error;
4340
type Future = future::FutureResult<Self::Response, Self::Error>;
4441

4542
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
4643
Ok(().into())
4744
}
4845

49-
fn call(&mut self, _req: Request<LiftBody<Body>>) -> Self::Future {
50-
let res = Response::new("Hello World!");
46+
fn call(&mut self, _req: Request<Body>) -> Self::Future {
47+
let res = Response::new(Body::from("Hello World!"));
5148
future::ok(res)
5249
}
5350
}

0 commit comments

Comments
 (0)