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))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user