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`.
1
9
use std:: { fmt, io, vec} ;
2
10
use std:: net:: {
3
11
IpAddr , Ipv4Addr , Ipv6Addr ,
@@ -10,12 +18,10 @@ use futures::{Async, Future, Poll};
10
18
use futures:: future:: { Executor , ExecuteError } ;
11
19
use futures:: sync:: oneshot;
12
20
use futures_cpupool:: { Builder as CpuPoolBuilder } ;
21
+ use tokio_threadpool;
13
22
14
23
use self :: sealed:: GaiTask ;
15
24
16
- #[ cfg( feature = "runtime" ) ]
17
- pub use self :: blocking:: { TokioThreadpoolGaiFuture , TokioThreadpoolGaiResolver } ;
18
-
19
25
/// Resolve a hostname to a set of IP addresses.
20
26
pub trait Resolve {
21
27
/// The set of IP addresses to try to connect to.
@@ -37,10 +43,12 @@ pub struct GaiResolver {
37
43
executor : GaiExecutor ,
38
44
}
39
45
46
+ /// An iterator of IP addresses returned from `getaddrinfo`.
40
47
pub struct GaiAddrs {
41
48
inner : IpAddrs ,
42
49
}
43
50
51
+ /// A future to resole a name returned by `GaiResolver`.
44
52
pub struct GaiFuture {
45
53
rx : oneshot:: SpawnHandle < IpAddrs , io:: Error > ,
46
54
}
@@ -242,46 +250,49 @@ pub(super) mod sealed {
242
250
}
243
251
}
244
252
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;
251
253
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 ( ( ) ) ;
253
260
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
+ }
260
266
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 ( )
263
274
}
275
+ }
264
276
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 ;
268
280
269
- fn resolve ( & self , name : Name ) -> TokioThreadpoolGaiFuture {
270
- TokioThreadpoolGaiFuture { name }
271
- }
281
+ fn resolve ( & self , name : Name ) -> TokioThreadpoolGaiFuture {
282
+ TokioThreadpoolGaiFuture { name }
272
283
}
284
+ }
285
+
286
+ impl Future for TokioThreadpoolGaiFuture {
287
+ type Item = GaiAddrs ;
288
+ type Error = io:: Error ;
273
289
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) ) ,
285
296
}
286
297
}
287
298
}
0 commit comments