From 012418407e4ad2587d9f3ba3521b516c84c2051a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Thu, 25 Apr 2019 15:44:01 +0100 Subject: [PATCH] update to latest tower and tower-hyper updates --- src/http-client.rs | 31 +++++++++++-------------------- src/http-server.rs | 14 +++++++------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/http-client.rs b/src/http-client.rs index 52f4f64..8fae1c4 100644 --- a/src/http-client.rs +++ b/src/http-client.rs @@ -4,10 +4,11 @@ use hyper::{ Request, Response, Uri, }; use std::time::Duration; -use tower::{builder::ServiceBuilder, reconnect::Reconnect, Service, ServiceExt}; +use tower::{MakeService, Service}; +use tower::builder::ServiceBuilder; use tower_hyper::{ + Body, client::{Builder, Connect}, - retry::{Body, RetryPolicy}, util::Connector, }; @@ -24,40 +25,30 @@ fn request() -> impl Future, Error = ()> { let connector = Connector::new(HttpConnector::new(1)); let hyper = Connect::new(connector, Builder::new()); - // RetryPolicy is a very simple policy that retries `n` times - // if the response has a 500 status code. Here, `n` is 5. - let policy = RetryPolicy::new(5); // We're calling the tower/examples/server.rs. let dst = Destination::try_from_uri(Uri::from_static("http://127.0.0.1:3000")).unwrap(); - // Now, to build the service! We use two BufferLayers in order to: - // - provide backpressure for the RateLimitLayer, and ConcurrencyLimitLayer - // - meet `RetryLayer`'s requirement that our service implement `Service + Clone` - // - ..and to provide cheap clones on the service. - let maker = ServiceBuilder::new() + // Now, to build the service! + let mut maker = ServiceBuilder::new() .buffer(5) .rate_limit(5, Duration::from_secs(1)) .concurrency_limit(5) - .retry(policy) - .buffer(5) - .make_service(hyper); + .service(hyper); - // `Reconnect` accepts a destination and a MakeService, creating a new service - // any time the connection encounters an error. - let client = Reconnect::new(maker, dst); + let client = maker + .make_service(dst) + .map_err(|err| eprintln!("Connect Error {:?}", err)); let request = Request::builder() .method("GET") - .body(Body::from(Vec::new())) + .body(Vec::new()) .unwrap(); - // we check to see if the client is ready to accept requests. client - .ready() .map_err(|e| panic!("Service is not ready: {:?}", e)) .and_then(|mut c| { c.call(request) - .map(|res| res.map(|b| b.into_inner())) + .map(|res| res.map(Body::into_inner)) .map_err(|e| panic!("{:?}", e)) }) } diff --git a/src/http-server.rs b/src/http-server.rs index c6e0f41..0f4b48d 100644 --- a/src/http-server.rs +++ b/src/http-server.rs @@ -1,8 +1,8 @@ use futures::{future, Future, Poll, Stream}; -use hyper::{self, Body, Request, Response}; +use hyper::{self, Request, Response}; use tokio::net::TcpListener; use tower::{builder::ServiceBuilder, Service}; -use tower_hyper::{body::LiftBody, server::Server}; +use tower_hyper::{Body, server::Server}; fn main() { hyper::rt::run(future::lazy(|| { @@ -13,7 +13,7 @@ fn main() { let maker = ServiceBuilder::new() .concurrency_limit(5) - .make_service(MakeSvc); + .service(MakeSvc); let server = Server::new(maker); @@ -37,8 +37,8 @@ fn main() { } struct Svc; -impl Service>> for Svc { - type Response = Response<&'static str>; +impl Service> for Svc { + type Response = Response; type Error = hyper::Error; type Future = future::FutureResult; @@ -46,8 +46,8 @@ impl Service>> for Svc { Ok(().into()) } - fn call(&mut self, _req: Request>) -> Self::Future { - let res = Response::new("Hello World!"); + fn call(&mut self, _req: Request) -> Self::Future { + let res = Response::new(Body::from(hyper::Body::from("Hello World!"))); future::ok(res) } }