feat(lib): update to std::future::Future
BREAKING CHANGE: All usage of async traits (`Future`, `Stream`, `AsyncRead`, `AsyncWrite`, etc) are updated to newer versions.
This commit is contained in:
@@ -16,20 +16,19 @@ use std::net::{
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::future::{Executor, ExecuteError};
|
||||
use futures::sync::oneshot;
|
||||
use futures_cpupool::{Builder as CpuPoolBuilder};
|
||||
use tokio_executor::TypedExecutor;
|
||||
use tokio_sync::oneshot;
|
||||
use tokio_threadpool;
|
||||
|
||||
use crate::common::{Future, Pin, Poll, Unpin, task};
|
||||
use self::sealed::GaiTask;
|
||||
|
||||
/// Resolve a hostname to a set of IP addresses.
|
||||
pub trait Resolve {
|
||||
pub trait Resolve: Unpin {
|
||||
/// The set of IP addresses to try to connect to.
|
||||
type Addrs: Iterator<Item=IpAddr>;
|
||||
/// A Future of the resolved set of addresses.
|
||||
type Future: Future<Item=Self::Addrs, Error=io::Error>;
|
||||
type Future: Future<Output=Result<Self::Addrs, io::Error>> + Unpin;
|
||||
/// Resolve a hostname.
|
||||
fn resolve(&self, name: Name) -> Self::Future;
|
||||
}
|
||||
@@ -53,7 +52,8 @@ pub struct GaiAddrs {
|
||||
|
||||
/// A future to resole a name returned by `GaiResolver`.
|
||||
pub struct GaiFuture {
|
||||
rx: oneshot::SpawnHandle<IpAddrs, io::Error>,
|
||||
//rx: oneshot::SpawnHandle<IpAddrs, io::Error>,
|
||||
blocking: GaiBlocking,
|
||||
}
|
||||
|
||||
impl Name {
|
||||
@@ -112,24 +112,36 @@ impl GaiResolver {
|
||||
///
|
||||
/// Takes number of DNS worker threads.
|
||||
pub fn new(threads: usize) -> Self {
|
||||
let pool = CpuPoolBuilder::new()
|
||||
.name_prefix("hyper-dns")
|
||||
.pool_size(threads)
|
||||
.create();
|
||||
GaiResolver {
|
||||
executor: GaiExecutor,
|
||||
}
|
||||
/*
|
||||
use tokio_threadpool::Builder;
|
||||
|
||||
let pool = Builder::new()
|
||||
.name_prefix("hyper-dns-gai-resolver")
|
||||
// not for CPU tasks, so only spawn workers
|
||||
// in blocking mode
|
||||
.pool_size(1)
|
||||
.max_blocking(threads)
|
||||
.build();
|
||||
GaiResolver::new_with_executor(pool)
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
/// Construct a new `GaiResolver` with a shared thread pool executor.
|
||||
///
|
||||
/// Takes an executor to run blocking `getaddrinfo` tasks on.
|
||||
pub fn new_with_executor<E: 'static>(executor: E) -> Self
|
||||
/*pub */fn new_with_executor<E: 'static>(executor: E) -> Self
|
||||
where
|
||||
E: Executor<GaiTask> + Send + Sync,
|
||||
E: TypedExecutor<GaiTask> + Send + Sync,
|
||||
{
|
||||
GaiResolver {
|
||||
executor: GaiExecutor(Arc::new(executor)),
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl Resolve for GaiResolver {
|
||||
@@ -138,9 +150,10 @@ impl Resolve for GaiResolver {
|
||||
|
||||
fn resolve(&self, name: Name) -> Self::Future {
|
||||
let blocking = GaiBlocking::new(name.host);
|
||||
let rx = oneshot::spawn(blocking, &self.executor);
|
||||
//let rx = oneshot::spawn(blocking, &self.executor);
|
||||
GaiFuture {
|
||||
rx,
|
||||
//rx,
|
||||
blocking,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,14 +165,16 @@ impl fmt::Debug for GaiResolver {
|
||||
}
|
||||
|
||||
impl Future for GaiFuture {
|
||||
type Item = GaiAddrs;
|
||||
type Error = io::Error;
|
||||
type Output = Result<GaiAddrs, io::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
||||
/*
|
||||
let addrs = try_ready!(self.rx.poll());
|
||||
Ok(Async::Ready(GaiAddrs {
|
||||
inner: addrs,
|
||||
}))
|
||||
*/
|
||||
Poll::Ready(self.blocking.block().map(|addrs| GaiAddrs { inner: addrs }))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,14 +199,16 @@ impl fmt::Debug for GaiAddrs {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct GaiExecutor(Arc<dyn Executor<GaiTask> + Send + Sync>);
|
||||
struct GaiExecutor/*(Arc<dyn Executor<GaiTask> + Send + Sync>)*/;
|
||||
|
||||
/*
|
||||
impl Executor<oneshot::Execute<GaiBlocking>> for GaiExecutor {
|
||||
fn execute(&self, future: oneshot::Execute<GaiBlocking>) -> Result<(), ExecuteError<oneshot::Execute<GaiBlocking>>> {
|
||||
self.0.execute(GaiTask { work: future })
|
||||
.map_err(|err| ExecuteError::new(err.kind(), err.into_future().work))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub(super) struct GaiBlocking {
|
||||
host: String,
|
||||
@@ -201,8 +218,16 @@ impl GaiBlocking {
|
||||
pub(super) fn new(host: String) -> GaiBlocking {
|
||||
GaiBlocking { host }
|
||||
}
|
||||
|
||||
fn block(&self) -> io::Result<IpAddrs> {
|
||||
debug!("resolving host={:?}", self.host);
|
||||
(&*self.host, 0).to_socket_addrs()
|
||||
.map(|i| IpAddrs { iter: i })
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl Future for GaiBlocking {
|
||||
type Item = IpAddrs;
|
||||
type Error = io::Error;
|
||||
@@ -213,6 +238,7 @@ impl Future for GaiBlocking {
|
||||
.map(|i| Async::Ready(IpAddrs { iter: i }))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub(super) struct IpAddrs {
|
||||
iter: vec::IntoIter<SocketAddr>,
|
||||
@@ -276,7 +302,7 @@ pub(super) mod sealed {
|
||||
use super::*;
|
||||
// Blocking task to be executed on a thread pool.
|
||||
pub struct GaiTask {
|
||||
pub(super) work: oneshot::Execute<GaiBlocking>
|
||||
//pub(super) work: oneshot::Execute<GaiBlocking>
|
||||
}
|
||||
|
||||
impl fmt::Debug for GaiTask {
|
||||
@@ -285,6 +311,7 @@ pub(super) mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
impl Future for GaiTask {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
@@ -293,6 +320,7 @@ pub(super) mod sealed {
|
||||
self.work.poll()
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@@ -329,15 +357,14 @@ impl Resolve for TokioThreadpoolGaiResolver {
|
||||
}
|
||||
|
||||
impl Future for TokioThreadpoolGaiFuture {
|
||||
type Item = GaiAddrs;
|
||||
type Error = io::Error;
|
||||
type Output = Result<GaiAddrs, io::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<GaiAddrs, io::Error> {
|
||||
match tokio_threadpool::blocking(|| (self.name.as_str(), 0).to_socket_addrs()) {
|
||||
Ok(Async::Ready(Ok(iter))) => Ok(Async::Ready(GaiAddrs { inner: IpAddrs { iter } })),
|
||||
Ok(Async::Ready(Err(e))) => Err(e),
|
||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
||||
match ready!(tokio_threadpool::blocking(|| (self.name.as_str(), 0).to_socket_addrs())) {
|
||||
Ok(Ok(iter)) => Poll::Ready(Ok(GaiAddrs { inner: IpAddrs { iter } })),
|
||||
Ok(Err(e)) => Poll::Ready(Err(e)),
|
||||
// a BlockingError, meaning not on a tokio_threadpool :(
|
||||
Err(e) => Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,19 @@ use std::mem;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
use futures::future::{Executor};
|
||||
use http::uri::Scheme;
|
||||
use net2::TcpBuilder;
|
||||
use tokio_reactor::Handle;
|
||||
use tokio_tcp::{TcpStream, ConnectFuture};
|
||||
use tokio_tcp::{TcpStream/*, ConnectFuture*/};
|
||||
use tokio_timer::Delay;
|
||||
|
||||
use crate::common::{Future, Pin, Poll, task};
|
||||
use super::{Connect, Connected, Destination};
|
||||
use super::dns::{self, GaiResolver, Resolve, TokioThreadpoolGaiResolver};
|
||||
|
||||
// TODO: unbox me?
|
||||
type ConnectFuture = Pin<Box<dyn Future<Output = io::Result<TcpStream>> + Send>>;
|
||||
|
||||
/// A connector for the `http` scheme.
|
||||
///
|
||||
/// Performs DNS resolution in a thread pool, and then connects over TCP.
|
||||
@@ -89,6 +91,7 @@ impl HttpConnector {
|
||||
http
|
||||
}
|
||||
|
||||
/*
|
||||
/// Construct a new HttpConnector.
|
||||
///
|
||||
/// Takes an executor to run blocking `getaddrinfo` tasks on.
|
||||
@@ -100,6 +103,7 @@ impl HttpConnector {
|
||||
http.set_reactor(handle);
|
||||
http
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl HttpConnector<TokioThreadpoolGaiResolver> {
|
||||
@@ -335,55 +339,54 @@ enum State<R: Resolve> {
|
||||
Error(Option<io::Error>),
|
||||
}
|
||||
|
||||
impl<R: Resolve> Future for HttpConnecting<R> {
|
||||
type Item = (TcpStream, Connected);
|
||||
type Error = io::Error;
|
||||
impl<R: Resolve> Future for HttpConnecting<R>
|
||||
where
|
||||
R::Future: Unpin,
|
||||
{
|
||||
type Output = Result<(TcpStream, Connected), io::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
||||
let me = &mut *self;
|
||||
loop {
|
||||
let state;
|
||||
match self.state {
|
||||
match me.state {
|
||||
State::Lazy(ref resolver, ref mut host, local_addr) => {
|
||||
// If the host is already an IP addr (v4 or v6),
|
||||
// skip resolving the dns and start connecting right away.
|
||||
if let Some(addrs) = dns::IpAddrs::try_parse(host, self.port) {
|
||||
if let Some(addrs) = dns::IpAddrs::try_parse(host, me.port) {
|
||||
state = State::Connecting(ConnectingTcp::new(
|
||||
local_addr, addrs, self.happy_eyeballs_timeout, self.reuse_address));
|
||||
local_addr, addrs, me.happy_eyeballs_timeout, me.reuse_address));
|
||||
} else {
|
||||
let name = dns::Name::new(mem::replace(host, String::new()));
|
||||
state = State::Resolving(resolver.resolve(name), local_addr);
|
||||
}
|
||||
},
|
||||
State::Resolving(ref mut future, local_addr) => {
|
||||
match future.poll()? {
|
||||
Async::NotReady => return Ok(Async::NotReady),
|
||||
Async::Ready(addrs) => {
|
||||
let port = self.port;
|
||||
let addrs = addrs
|
||||
.map(|addr| SocketAddr::new(addr, port))
|
||||
.collect();
|
||||
let addrs = dns::IpAddrs::new(addrs);
|
||||
state = State::Connecting(ConnectingTcp::new(
|
||||
local_addr, addrs, self.happy_eyeballs_timeout, self.reuse_address));
|
||||
}
|
||||
};
|
||||
let addrs = ready!(Pin::new(future).poll(cx))?;
|
||||
let port = me.port;
|
||||
let addrs = addrs
|
||||
.map(|addr| SocketAddr::new(addr, port))
|
||||
.collect();
|
||||
let addrs = dns::IpAddrs::new(addrs);
|
||||
state = State::Connecting(ConnectingTcp::new(
|
||||
local_addr, addrs, me.happy_eyeballs_timeout, me.reuse_address));
|
||||
},
|
||||
State::Connecting(ref mut c) => {
|
||||
let sock = try_ready!(c.poll(&self.handle));
|
||||
let sock = ready!(c.poll(cx, &me.handle))?;
|
||||
|
||||
if let Some(dur) = self.keep_alive_timeout {
|
||||
if let Some(dur) = me.keep_alive_timeout {
|
||||
sock.set_keepalive(Some(dur))?;
|
||||
}
|
||||
|
||||
if let Some(size) = self.send_buffer_size {
|
||||
if let Some(size) = me.send_buffer_size {
|
||||
sock.set_send_buffer_size(size)?;
|
||||
}
|
||||
|
||||
if let Some(size) = self.recv_buffer_size {
|
||||
if let Some(size) = me.recv_buffer_size {
|
||||
sock.set_recv_buffer_size(size)?;
|
||||
}
|
||||
|
||||
sock.set_nodelay(self.nodelay)?;
|
||||
sock.set_nodelay(me.nodelay)?;
|
||||
|
||||
let extra = HttpInfo {
|
||||
remote_addr: sock.peer_addr()?,
|
||||
@@ -391,11 +394,11 @@ impl<R: Resolve> Future for HttpConnecting<R> {
|
||||
let connected = Connected::new()
|
||||
.extra(extra);
|
||||
|
||||
return Ok(Async::Ready((sock, connected)));
|
||||
return Poll::Ready(Ok((sock, connected)));
|
||||
},
|
||||
State::Error(ref mut e) => return Err(e.take().expect("polled more than once")),
|
||||
State::Error(ref mut e) => return Poll::Ready(Err(e.take().expect("polled more than once"))),
|
||||
}
|
||||
self.state = state;
|
||||
me.state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -474,20 +477,21 @@ impl ConnectingTcpRemote {
|
||||
// not a Future, since passing a &Handle to poll
|
||||
fn poll(
|
||||
&mut self,
|
||||
cx: &mut task::Context<'_>,
|
||||
local_addr: &Option<IpAddr>,
|
||||
handle: &Option<Handle>,
|
||||
reuse_address: bool,
|
||||
) -> Poll<TcpStream, io::Error> {
|
||||
) -> Poll<io::Result<TcpStream>> {
|
||||
let mut err = None;
|
||||
loop {
|
||||
if let Some(ref mut current) = self.current {
|
||||
match current.poll() {
|
||||
Ok(Async::Ready(tcp)) => {
|
||||
match current.as_mut().poll(cx) {
|
||||
Poll::Ready(Ok(tcp)) => {
|
||||
debug!("connected to {:?}", tcp.peer_addr().ok());
|
||||
return Ok(Async::Ready(tcp));
|
||||
return Poll::Ready(Ok(tcp));
|
||||
},
|
||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||
Err(e) => {
|
||||
Poll::Pending => return Poll::Pending,
|
||||
Poll::Ready(Err(e)) => {
|
||||
trace!("connect error {:?}", e);
|
||||
err = Some(e);
|
||||
if let Some(addr) = self.addrs.next() {
|
||||
@@ -503,7 +507,7 @@ impl ConnectingTcpRemote {
|
||||
continue;
|
||||
}
|
||||
|
||||
return Err(err.take().expect("missing connect error"));
|
||||
return Poll::Ready(Err(err.take().expect("missing connect error")));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -540,51 +544,46 @@ fn connect(addr: &SocketAddr, local_addr: &Option<IpAddr>, handle: &Option<Handl
|
||||
None => Cow::Owned(Handle::default()),
|
||||
};
|
||||
|
||||
Ok(TcpStream::connect_std(builder.to_tcp_stream()?, addr, &handle))
|
||||
Ok(Box::pin(TcpStream::connect_std(builder.to_tcp_stream()?, addr, &handle)))
|
||||
}
|
||||
|
||||
impl ConnectingTcp {
|
||||
// not a Future, since passing a &Handle to poll
|
||||
fn poll(&mut self, handle: &Option<Handle>) -> Poll<TcpStream, io::Error> {
|
||||
fn poll(&mut self, cx: &mut task::Context<'_>, handle: &Option<Handle>) -> Poll<io::Result<TcpStream>> {
|
||||
match self.fallback.take() {
|
||||
None => self.preferred.poll(&self.local_addr, handle, self.reuse_address),
|
||||
Some(mut fallback) => match self.preferred.poll(&self.local_addr, handle, self.reuse_address) {
|
||||
Ok(Async::Ready(stream)) => {
|
||||
None => self.preferred.poll(cx, &self.local_addr, handle, self.reuse_address),
|
||||
Some(mut fallback) => match self.preferred.poll(cx, &self.local_addr, handle, self.reuse_address) {
|
||||
Poll::Ready(Ok(stream)) => {
|
||||
// Preferred successful - drop fallback.
|
||||
Ok(Async::Ready(stream))
|
||||
Poll::Ready(Ok(stream))
|
||||
}
|
||||
Ok(Async::NotReady) => match fallback.delay.poll() {
|
||||
Ok(Async::Ready(_)) => match fallback.remote.poll(&self.local_addr, handle, self.reuse_address) {
|
||||
Ok(Async::Ready(stream)) => {
|
||||
Poll::Pending => match Pin::new(&mut fallback.delay).poll(cx) {
|
||||
Poll::Ready(()) => match fallback.remote.poll(cx, &self.local_addr, handle, self.reuse_address) {
|
||||
Poll::Ready(Ok(stream)) => {
|
||||
// Fallback successful - drop current preferred,
|
||||
// but keep fallback as new preferred.
|
||||
self.preferred = fallback.remote;
|
||||
Ok(Async::Ready(stream))
|
||||
Poll::Ready(Ok(stream))
|
||||
}
|
||||
Ok(Async::NotReady) => {
|
||||
Poll::Pending => {
|
||||
// Neither preferred nor fallback are ready.
|
||||
self.fallback = Some(fallback);
|
||||
Ok(Async::NotReady)
|
||||
Poll::Pending
|
||||
}
|
||||
Err(_) => {
|
||||
Poll::Ready(Err(_)) => {
|
||||
// Fallback failed - resume with preferred only.
|
||||
Ok(Async::NotReady)
|
||||
Poll::Pending
|
||||
}
|
||||
},
|
||||
Ok(Async::NotReady) => {
|
||||
Poll::Pending => {
|
||||
// Too early to attempt fallback.
|
||||
self.fallback = Some(fallback);
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
Err(_) => {
|
||||
// Fallback delay failed - resume with preferred only.
|
||||
Ok(Async::NotReady)
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
Poll::Ready(Err(_)) => {
|
||||
// Preferred failed - use fallback as new preferred.
|
||||
self.preferred = fallback.remote;
|
||||
self.preferred.poll(&self.local_addr, handle, self.reuse_address)
|
||||
self.preferred.poll(cx, &self.local_addr, handle, self.reuse_address)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -734,10 +733,10 @@ mod tests {
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
match self.0.poll(&Some(Handle::default())) {
|
||||
Ok(Async::Ready(stream)) => Ok(Async::Ready(
|
||||
Poll::Ready(Ok(stream)) => Poll::Ready(Ok(
|
||||
if stream.peer_addr().unwrap().is_ipv4() { 4 } else { 6 }
|
||||
)),
|
||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||
Poll::Pending => Poll::Pending,
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,11 @@ use std::{fmt, mem};
|
||||
#[cfg(try_from)] use std::convert::TryFrom;
|
||||
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use futures::Future;
|
||||
use ::http::{uri, Response, Uri};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use crate::common::{Future, Unpin};
|
||||
|
||||
#[cfg(feature = "runtime")] pub mod dns;
|
||||
#[cfg(feature = "runtime")] mod http;
|
||||
#[cfg(feature = "runtime")] pub use self::http::{HttpConnector, HttpInfo};
|
||||
@@ -25,11 +26,11 @@ use tokio_io::{AsyncRead, AsyncWrite};
|
||||
/// ready connection.
|
||||
pub trait Connect: Send + Sync {
|
||||
/// The connected IO Stream.
|
||||
type Transport: AsyncRead + AsyncWrite + Send + 'static;
|
||||
type Transport: AsyncRead + AsyncWrite + Unpin + Send + 'static;
|
||||
/// An error occured when trying to connect.
|
||||
type Error: Into<Box<dyn StdError + Send + Sync>>;
|
||||
/// A Future that will resolve to the connected Transport.
|
||||
type Future: Future<Item=(Self::Transport, Connected), Error=Self::Error> + Send;
|
||||
type Future: Future<Output=Result<(Self::Transport, Connected), Self::Error>> + Unpin + Send;
|
||||
/// Connect to a destination.
|
||||
fn connect(&self, dst: Destination) -> Self::Future;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user