Skip to content

Commit 34d780a

Browse files
committed
feat(dns): export client::connect::dns module, and
`TokioThreadpoolGaiResolver` type.
1 parent 1e8d643 commit 34d780a

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ tokio-executor = { version = "0.1.0", optional = true }
3434
tokio-io = "0.1"
3535
tokio-reactor = { version = "0.1", optional = true }
3636
tokio-tcp = { version = "0.1", optional = true }
37+
tokio-threadpool = { version = "0.1.3", optional = true }
3738
tokio-timer = { version = "0.2", optional = true }
38-
tokio-threadpool = { version = "0.1", optional = true }
3939
want = "0.0.6"
4040

4141
[dev-dependencies]
@@ -62,8 +62,8 @@ runtime = [
6262
"tokio-executor",
6363
"tokio-reactor",
6464
"tokio-tcp",
65-
"tokio-timer",
6665
"tokio-threadpool",
66+
"tokio-timer",
6767
]
6868
nightly = []
6969
__internal_flaky_tests = []

src/client/connect/dns.rs

+46-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//! The `Resolve` trait, support types, and some basic implementations.
2+
//!
3+
//! This module contains:
4+
//!
5+
//! - A [`GaiResolver`](GaiResolver) that is the default resolver for the
6+
//! `HttpConnector`.
7+
//! - The [`Resolve`](Resolve) trait and related types to build a custom
8+
//! resolver for use with the `HttpConnector`.
19
use std::{fmt, io, vec};
210
use std::net::{
311
IpAddr, Ipv4Addr, Ipv6Addr,
@@ -10,12 +18,10 @@ use futures::{Async, Future, Poll};
1018
use futures::future::{Executor, ExecuteError};
1119
use futures::sync::oneshot;
1220
use futures_cpupool::{Builder as CpuPoolBuilder};
21+
use tokio_threadpool;
1322

1423
use self::sealed::GaiTask;
1524

16-
#[cfg(feature = "runtime")]
17-
pub use self::blocking::{TokioThreadpoolGaiFuture, TokioThreadpoolGaiResolver};
18-
1925
/// Resolve a hostname to a set of IP addresses.
2026
pub trait Resolve {
2127
/// The set of IP addresses to try to connect to.
@@ -37,10 +43,12 @@ pub struct GaiResolver {
3743
executor: GaiExecutor,
3844
}
3945

46+
/// An iterator of IP addresses returned from `getaddrinfo`.
4047
pub struct GaiAddrs {
4148
inner: IpAddrs,
4249
}
4350

51+
/// A future to resole a name returned by `GaiResolver`.
4452
pub struct GaiFuture {
4553
rx: oneshot::SpawnHandle<IpAddrs, io::Error>,
4654
}
@@ -242,46 +250,49 @@ pub(super) mod sealed {
242250
}
243251
}
244252

245-
#[cfg(feature = "runtime")]
246-
mod blocking {
247-
use futures::{Async, Future, Poll};
248-
use std::io;
249-
use std::net::ToSocketAddrs;
250-
use tokio_threadpool;
251253

252-
use super::{Name, IpAddrs, GaiAddrs, Resolve};
254+
/// A resolver using `getaddrinfo` calls via the `tokio_threadpool::blocking` API.
255+
///
256+
/// Unlike the `GaiResolver` this will not spawn dedicated threads, but only works when running on the
257+
/// multi-threaded Tokio runtime.
258+
#[derive(Clone, Debug)]
259+
pub struct TokioThreadpoolGaiResolver(());
253260

254-
/// A resolver using `getaddrinfo` calls via the `tokio_threadpool::blocking` API.
255-
///
256-
/// Unlike the `GaiResolver` this will not spawn dedicated threads, but only works when running on the
257-
/// multi-threaded Tokio runtime.
258-
#[derive(Clone)]
259-
pub struct TokioThreadpoolGaiResolver(());
261+
/// The future returned by `TokioThreadpoolGaiResolver`.
262+
#[derive(Debug)]
263+
pub struct TokioThreadpoolGaiFuture {
264+
name: Name,
265+
}
260266

261-
pub struct TokioThreadpoolGaiFuture {
262-
name: Name,
267+
impl TokioThreadpoolGaiResolver {
268+
/// Creates a new DNS resolver that will use tokio threadpool's blocking
269+
/// feature.
270+
///
271+
/// **Requires** its futures to be run on the threadpool runtime.
272+
pub fn new() -> Self {
273+
TokioThreadpoolGaiResolver::new()
263274
}
275+
}
264276

265-
impl Resolve for TokioThreadpoolGaiResolver {
266-
type Addrs = GaiAddrs;
267-
type Future = TokioThreadpoolGaiFuture;
277+
impl Resolve for TokioThreadpoolGaiResolver {
278+
type Addrs = GaiAddrs;
279+
type Future = TokioThreadpoolGaiFuture;
268280

269-
fn resolve(&self, name: Name) -> TokioThreadpoolGaiFuture {
270-
TokioThreadpoolGaiFuture { name }
271-
}
281+
fn resolve(&self, name: Name) -> TokioThreadpoolGaiFuture {
282+
TokioThreadpoolGaiFuture { name }
272283
}
284+
}
285+
286+
impl Future for TokioThreadpoolGaiFuture {
287+
type Item = GaiAddrs;
288+
type Error = io::Error;
273289

274-
impl Future for TokioThreadpoolGaiFuture {
275-
type Item = GaiAddrs;
276-
type Error = io::Error;
277-
278-
fn poll(&mut self) -> Poll<GaiAddrs, io::Error> {
279-
match tokio_threadpool::blocking(|| (self.name.as_str(), 0).to_socket_addrs()) {
280-
Ok(Async::Ready(Ok(iter))) => Ok(Async::Ready(GaiAddrs { inner: IpAddrs { iter } })),
281-
Ok(Async::Ready(Err(e))) => Err(e),
282-
Ok(Async::NotReady) => Ok(Async::NotReady),
283-
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
284-
}
290+
fn poll(&mut self) -> Poll<GaiAddrs, io::Error> {
291+
match tokio_threadpool::blocking(|| (self.name.as_str(), 0).to_socket_addrs()) {
292+
Ok(Async::Ready(Ok(iter))) => Ok(Async::Ready(GaiAddrs { inner: IpAddrs { iter } })),
293+
Ok(Async::Ready(Err(e))) => Err(e),
294+
Ok(Async::NotReady) => Ok(Async::NotReady),
295+
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
285296
}
286297
}
287298
}

src/client/connect/http.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tokio_tcp::{TcpStream, ConnectFuture};
1515
use tokio_timer::Delay;
1616

1717
use super::{Connect, Connected, Destination};
18-
use super::dns::{self, GaiResolver, Resolve};
18+
use super::dns::{self, GaiResolver, Resolve, TokioThreadpoolGaiResolver};
1919

2020
/// A connector for the `http` scheme.
2121
///
@@ -99,6 +99,15 @@ impl HttpConnector {
9999
}
100100
}
101101

102+
impl HttpConnector<TokioThreadpoolGaiResolver> {
103+
/// Construct a new HttpConnector using the `TokioThreadpoolGaiResolver`.
104+
///
105+
/// This resolver **requires** the threadpool runtime to be used.
106+
pub fn new_with_tokio_threadpool_resolver() -> Self {
107+
HttpConnector::new_with_resolver(TokioThreadpoolGaiResolver::new())
108+
}
109+
}
110+
102111

103112
impl<R> HttpConnector<R> {
104113
/// Construct a new HttpConnector.

src/client/connect/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use futures::Future;
1313
use http::{uri, Response, Uri};
1414
use tokio_io::{AsyncRead, AsyncWrite};
1515

16-
#[cfg(feature = "runtime")] mod dns;
16+
#[cfg(feature = "runtime")] pub mod dns;
1717
#[cfg(feature = "runtime")] mod http;
18-
#[cfg(feature = "runtime")] pub use self::dns::{GaiResolver, Name, Resolve};
1918
#[cfg(feature = "runtime")] pub use self::http::{HttpConnector, HttpInfo};
2019

2120
/// Connect to a destination, returning an IO transport.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ extern crate time;
3030
#[macro_use] extern crate tokio_io;
3131
#[cfg(feature = "runtime")] extern crate tokio_reactor;
3232
#[cfg(feature = "runtime")] extern crate tokio_tcp;
33-
#[cfg(feature = "runtime")] extern crate tokio_timer;
3433
#[cfg(feature = "runtime")] extern crate tokio_threadpool;
34+
#[cfg(feature = "runtime")] extern crate tokio_timer;
3535
extern crate want;
3636

3737
#[cfg(all(test, feature = "nightly"))]

0 commit comments

Comments
 (0)