1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
extern crate futures;
extern crate abstract_ns;
extern crate futures_cpupool;
use std::fmt;
use std::net::ToSocketAddrs;
use futures::Async;
use abstract_ns::{HostResolve, Name, IpList, Error};
use futures_cpupool::{CpuPool, CpuFuture};
#[derive(Clone, Debug)]
pub struct ThreadedResolver {
pool: CpuPool,
}
pub struct Future(CpuFuture<IpList, Error>);
impl ThreadedResolver {
pub fn new() -> Self {
ThreadedResolver {
pool: CpuPool::new(8),
}
}
pub fn use_pool(pool: CpuPool) -> Self {
ThreadedResolver {
pool: pool,
}
}
}
impl futures::Future for Future {
type Item = IpList;
type Error = Error;
fn poll(&mut self) -> Result<Async<IpList>, Error> {
self.0.poll()
}
}
impl HostResolve for ThreadedResolver {
type HostFuture = Future;
fn resolve_host(&self, name: &Name) -> Future {
let name = name.clone();
Future(self.pool.spawn_fn(move || {
match (name.as_ref(), 0).to_socket_addrs() {
Ok(it) => Ok(it.map(|sa| sa.ip())
.collect::<Vec<_>>().into()),
Err(e) => Err(Error::TemporaryError(Box::new(e))),
}
}))
}
}
impl fmt::Debug for Future {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ns_std_threaded::Future {{}}")
}
}