refactor(lib): convert to futures 0.2.0-beta (#1470)

This commit is contained in:
Sam Rijs
2018-03-30 07:32:44 +11:00
committed by Sean McArthur
parent 5db85316a1
commit a12f7beed9
34 changed files with 1366 additions and 1347 deletions

View File

@@ -2,13 +2,15 @@ use std::io;
use bytes::Bytes;
use futures::{Async, Future, Poll, Stream};
use futures::task;
use futures::io::{AsyncRead, AsyncWrite};
use http::{Request, Response, StatusCode};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_service::Service;
use proto::body::Entity;
use proto::{Body, BodyLength, Conn, Http1Transaction, MessageHead, RequestHead, RequestLine, ResponseHead};
use ::service::Service;
pub struct Dispatcher<D, Bs, I, B, T> {
conn: Conn<I, B, T>,
dispatch: D,
@@ -21,9 +23,9 @@ pub trait Dispatch {
type PollItem;
type PollBody;
type RecvItem;
fn poll_msg(&mut self) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error>;
fn recv_msg(&mut self, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()>;
fn poll_ready(&mut self) -> Poll<(), ()>;
fn poll_msg(&mut self, cx: &mut task::Context) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error>;
fn recv_msg(&mut self, cx: &mut task::Context, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()>;
fn poll_ready(&mut self, cx: &mut task::Context) -> Poll<(), ()>;
fn should_poll(&self) -> bool;
}
@@ -69,57 +71,57 @@ where
/// The "Future" poll function. Runs this dispatcher until the
/// connection is shutdown, or an error occurs.
pub fn poll_until_shutdown(&mut self) -> Poll<(), ::Error> {
self.poll_catch(true)
pub fn poll_until_shutdown(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
self.poll_catch(cx, true)
}
/// Run this dispatcher until HTTP says this connection is done,
/// but don't call `AsyncWrite::shutdown` on the underlying IO.
///
/// This is useful for HTTP upgrades.
pub fn poll_without_shutdown(&mut self) -> Poll<(), ::Error> {
self.poll_catch(false)
pub fn poll_without_shutdown(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
self.poll_catch(cx, false)
}
fn poll_catch(&mut self, should_shutdown: bool) -> Poll<(), ::Error> {
self.poll_inner(should_shutdown).or_else(|e| {
fn poll_catch(&mut self, cx: &mut task::Context, should_shutdown: bool) -> Poll<(), ::Error> {
self.poll_inner(cx, should_shutdown).or_else(|e| {
// An error means we're shutting down either way.
// We just try to give the error to the user,
// and close the connection with an Ok. If we
// cannot give it to the user, then return the Err.
self.dispatch.recv_msg(Err(e)).map(Async::Ready)
self.dispatch.recv_msg(cx, Err(e)).map(Async::Ready)
})
}
fn poll_inner(&mut self, should_shutdown: bool) -> Poll<(), ::Error> {
self.poll_read()?;
self.poll_write()?;
self.poll_flush()?;
fn poll_inner(&mut self, cx: &mut task::Context, should_shutdown: bool) -> Poll<(), ::Error> {
self.poll_read(cx)?;
self.poll_write(cx)?;
self.poll_flush(cx)?;
if self.is_done() {
if should_shutdown {
try_ready!(self.conn.shutdown());
try_ready!(self.conn.shutdown(cx));
}
self.conn.take_error()?;
Ok(Async::Ready(()))
} else {
Ok(Async::NotReady)
Ok(Async::Pending)
}
}
fn poll_read(&mut self) -> Poll<(), ::Error> {
fn poll_read(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
loop {
if self.is_closing {
return Ok(Async::Ready(()));
} else if self.conn.can_read_head() {
try_ready!(self.poll_read_head());
try_ready!(self.poll_read_head(cx));
} else if let Some(mut body) = self.body_tx.take() {
if self.conn.can_read_body() {
match body.poll_ready() {
match body.poll_ready(cx) {
Ok(Async::Ready(())) => (),
Ok(Async::NotReady) => {
Ok(Async::Pending) => {
self.body_tx = Some(body);
return Ok(Async::NotReady);
return Ok(Async::Pending);
},
Err(_canceled) => {
// user doesn't care about the body
@@ -129,7 +131,7 @@ where
return Ok(Async::Ready(()));
}
}
match self.conn.read_body() {
match self.conn.read_body(cx) {
Ok(Async::Ready(Some(chunk))) => {
match body.send_data(chunk) {
Ok(()) => {
@@ -147,9 +149,9 @@ where
Ok(Async::Ready(None)) => {
// just drop, the body will close automatically
},
Ok(Async::NotReady) => {
Ok(Async::Pending) => {
self.body_tx = Some(body);
return Ok(Async::NotReady);
return Ok(Async::Pending);
}
Err(e) => {
body.send_error(::Error::Io(e));
@@ -159,16 +161,16 @@ where
// just drop, the body will close automatically
}
} else {
return self.conn.read_keep_alive().map(Async::Ready);
return self.conn.read_keep_alive(cx).map(Async::Ready);
}
}
}
fn poll_read_head(&mut self) -> Poll<(), ::Error> {
fn poll_read_head(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
// can dispatch receive, or does it still care about, an incoming message?
match self.dispatch.poll_ready() {
match self.dispatch.poll_ready(cx) {
Ok(Async::Ready(())) => (),
Ok(Async::NotReady) => unreachable!("dispatch not ready when conn is"),
Ok(Async::Pending) => unreachable!("dispatch not ready when conn is"),
Err(()) => {
trace!("dispatch no longer receiving messages");
self.close();
@@ -176,27 +178,27 @@ where
}
}
// dispatch is ready for a message, try to read one
match self.conn.read_head() {
match self.conn.read_head(cx) {
Ok(Async::Ready(Some((head, has_body)))) => {
let body = if has_body {
let (mut tx, rx) = Body::channel();
let _ = tx.poll_ready(); // register this task if rx is dropped
let _ = tx.poll_ready(cx); // register this task if rx is dropped
self.body_tx = Some(tx);
rx
} else {
Body::empty()
};
self.dispatch.recv_msg(Ok((head, body)))?;
self.dispatch.recv_msg(cx, Ok((head, body)))?;
Ok(Async::Ready(()))
},
Ok(Async::Ready(None)) => {
// read eof, conn will start to shutdown automatically
Ok(Async::Ready(()))
}
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Pending) => Ok(Async::Pending),
Err(err) => {
debug!("read_head error: {}", err);
self.dispatch.recv_msg(Err(err))?;
self.dispatch.recv_msg(cx, Err(err))?;
// if here, the dispatcher gave the user the error
// somewhere else. we still need to shutdown, but
// not as a second error.
@@ -205,12 +207,12 @@ where
}
}
fn poll_write(&mut self) -> Poll<(), ::Error> {
fn poll_write(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
loop {
if self.is_closing {
return Ok(Async::Ready(()));
} else if self.body_rx.is_none() && self.conn.can_write_head() && self.dispatch.should_poll() {
if let Some((head, body)) = try_ready!(self.dispatch.poll_msg()) {
if let Some((head, body)) = try_ready!(self.dispatch.poll_msg(cx)) {
let body_type = body.as_ref().map(|body| {
body.content_length()
.map(BodyLength::Known)
@@ -223,27 +225,27 @@ where
return Ok(Async::Ready(()));
}
} else if !self.conn.can_buffer_body() {
try_ready!(self.poll_flush());
try_ready!(self.poll_flush(cx));
} else if let Some(mut body) = self.body_rx.take() {
let chunk = match body.poll_data()? {
let chunk = match body.poll_data(cx)? {
Async::Ready(Some(chunk)) => {
self.body_rx = Some(body);
chunk
},
Async::Ready(None) => {
if self.conn.can_write_body() {
self.conn.write_body(None)?;
self.conn.write_body(cx, None)?;
}
continue;
},
Async::NotReady => {
Async::Pending => {
self.body_rx = Some(body);
return Ok(Async::NotReady);
return Ok(Async::Pending);
}
};
if self.conn.can_write_body() {
assert!(self.conn.write_body(Some(chunk))?.is_ready());
self.conn.write_body(cx, Some(chunk))?;
// This allows when chunk is `None`, or `Some([])`.
} else if chunk.as_ref().len() == 0 {
// ok
@@ -251,13 +253,13 @@ where
warn!("unexpected chunk when body cannot write");
}
} else {
return Ok(Async::NotReady);
return Ok(Async::Pending);
}
}
}
fn poll_flush(&mut self) -> Poll<(), ::Error> {
self.conn.flush().map_err(|err| {
fn poll_flush(&mut self, cx: &mut task::Context) -> Poll<(), ::Error> {
self.conn.flush(cx).map_err(|err| {
debug!("error writing: {}", err);
err.into()
})
@@ -300,8 +302,8 @@ where
type Error = ::Error;
#[inline]
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.poll_until_shutdown()
fn poll(&mut self, cx: &mut task::Context) -> Poll<Self::Item, Self::Error> {
self.poll_until_shutdown(cx)
}
}
@@ -325,13 +327,13 @@ where
type PollBody = Bs;
type RecvItem = RequestHead;
fn poll_msg(&mut self) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error> {
fn poll_msg(&mut self, cx: &mut task::Context) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error> {
if let Some(mut fut) = self.in_flight.take() {
let resp = match fut.poll()? {
let resp = match fut.poll(cx)? {
Async::Ready(res) => res,
Async::NotReady => {
Async::Pending => {
self.in_flight = Some(fut);
return Ok(Async::NotReady);
return Ok(Async::Pending);
}
};
let (parts, body) = resp.into_parts();
@@ -351,7 +353,7 @@ where
}
}
fn recv_msg(&mut self, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()> {
fn recv_msg(&mut self, _cx: &mut task::Context, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()> {
let (msg, body) = msg?;
let mut req = Request::new(body);
*req.method_mut() = msg.subject.0;
@@ -362,9 +364,9 @@ where
Ok(())
}
fn poll_ready(&mut self) -> Poll<(), ()> {
fn poll_ready(&mut self, _cx: &mut task::Context) -> Poll<(), ()> {
if self.in_flight.is_some() {
Ok(Async::NotReady)
Ok(Async::Pending)
} else {
Ok(Async::Ready(()))
}
@@ -395,16 +397,16 @@ where
type PollBody = B;
type RecvItem = ResponseHead;
fn poll_msg(&mut self) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error> {
match self.rx.poll() {
fn poll_msg(&mut self, cx: &mut task::Context) -> Poll<Option<(Self::PollItem, Option<Self::PollBody>)>, ::Error> {
match self.rx.poll_next(cx) {
Ok(Async::Ready(Some((req, mut cb)))) => {
// check that future hasn't been canceled already
match cb.poll_cancel().expect("poll_cancel cannot error") {
match cb.poll_cancel(cx).expect("poll_cancel cannot error") {
Async::Ready(()) => {
trace!("request canceled");
Ok(Async::Ready(None))
},
Async::NotReady => {
Async::Pending => {
let (parts, body) = req.into_parts();
let head = RequestHead {
version: parts.version,
@@ -427,12 +429,12 @@ where
// user has dropped sender handle
Ok(Async::Ready(None))
},
Ok(Async::NotReady) => return Ok(Async::NotReady),
Ok(Async::Pending) => return Ok(Async::Pending),
Err(_) => unreachable!("receiver cannot error"),
}
}
fn recv_msg(&mut self, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()> {
fn recv_msg(&mut self, cx: &mut task::Context, msg: ::Result<(Self::RecvItem, Body)>) -> ::Result<()> {
match msg {
Ok((msg, body)) => {
if let Some(cb) = self.callback.take() {
@@ -450,7 +452,7 @@ where
if let Some(cb) = self.callback.take() {
let _ = cb.send(Err((err, None)));
Ok(())
} else if let Ok(Async::Ready(Some((req, cb)))) = self.rx.poll() {
} else if let Ok(Async::Ready(Some((req, cb)))) = self.rx.poll_next(cx) {
trace!("canceling queued request with connection error: {}", err);
// in this case, the message was never even started, so it's safe to tell
// the user that the request was completely canceled
@@ -463,14 +465,14 @@ where
}
}
fn poll_ready(&mut self) -> Poll<(), ()> {
fn poll_ready(&mut self, cx: &mut task::Context) -> Poll<(), ()> {
match self.callback {
Some(ref mut cb) => match cb.poll_cancel() {
Some(ref mut cb) => match cb.poll_cancel(cx) {
Ok(Async::Ready(())) => {
trace!("callback receiver has dropped");
Err(())
},
Ok(Async::NotReady) => Ok(Async::Ready(())),
Ok(Async::Pending) => Ok(Async::Ready(())),
Err(_) => unreachable!("oneshot poll_cancel cannot error"),
},
None => Err(()),
@@ -487,31 +489,32 @@ mod tests {
extern crate pretty_env_logger;
use super::*;
use futures::executor::block_on;
use futures::future::lazy;
use mock::AsyncIo;
use proto::ClientTransaction;
#[test]
fn client_read_bytes_before_writing_request() {
let _ = pretty_env_logger::try_init();
::futures::lazy(|| {
block_on(lazy(|cx| {
let io = AsyncIo::new_buf(b"HTTP/1.1 200 OK\r\n\r\n".to_vec(), 100);
let (mut tx, rx) = ::client::dispatch::channel();
let conn = Conn::<_, ::Chunk, ClientTransaction>::new(io);
let mut dispatcher = Dispatcher::new(Client::new(rx), conn);
let res_rx = tx.try_send(::Request::new(::Body::empty())).unwrap();
let mut res_rx = tx.try_send(::Request::new(::Body::empty())).unwrap();
let a1 = dispatcher.poll().expect("error should be sent on channel");
let a1 = dispatcher.poll(cx).expect("error should be sent on channel");
assert!(a1.is_ready(), "dispatcher should be closed");
let err = res_rx.wait()
.expect("callback poll")
.expect_err("callback response");
let result = res_rx.poll(cx)
.expect("callback poll");
match err {
(::Error::Cancel(_), Some(_)) => (),
other => panic!("expected Canceled, got {:?}", other),
match result {
Async::Ready(Err((::Error::Cancel(_), Some(_)))) => (),
other => panic!("expected Err(Canceled), got {:?}", other),
}
Ok::<(), ()>(())
}).wait().unwrap();
Ok::<_, ()>(())
})).unwrap();
}
}