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

update to latest tower and tower-hyper updates #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/http-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -24,40 +25,30 @@ fn request() -> impl Future<Item = Response<hyper::Body>, 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))
})
}
14 changes: 7 additions & 7 deletions src/http-server.rs
Original file line number Diff line number Diff line change
@@ -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(|| {
Expand All @@ -13,7 +13,7 @@ fn main() {

let maker = ServiceBuilder::new()
.concurrency_limit(5)
.make_service(MakeSvc);
.service(MakeSvc);

let server = Server::new(maker);

Expand All @@ -37,17 +37,17 @@ fn main() {
}

struct Svc;
impl Service<Request<LiftBody<Body>>> for Svc {
type Response = Response<&'static str>;
impl Service<Request<Body>> for Svc {
type Response = Response<Body>;
type Error = hyper::Error;
type Future = future::FutureResult<Self::Response, Self::Error>;

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

fn call(&mut self, _req: Request<LiftBody<Body>>) -> Self::Future {
let res = Response::new("Hello World!");
fn call(&mut self, _req: Request<Body>) -> Self::Future {
let res = Response::new(Body::from(hyper::Body::from("Hello World!")));
future::ok(res)
}
}
Expand Down