refactor DNS resolver construction

- System Conf is read as `ClientBuilder::build()` time, providing the
  error earlier.
- If there is an error reading the resolve system conf, a better error
  is reported.
- Resolver only needs to lock a mutex once to spawn the background task,
  instead of every single `resolve` call.
This commit is contained in:
Sean McArthur
2019-01-10 11:17:09 -08:00
parent b129ab0bb4
commit b71787be86
6 changed files with 83 additions and 53 deletions

View File

@@ -1,62 +1,75 @@
use std::io;
use std::sync::{Arc, Mutex};
use std::{io, vec};
use std::net::IpAddr;
use std::sync::{Arc, Mutex, Once};
use futures::{future, Future};
use hyper::client::connect::dns as hyper_dns;
use trust_dns_resolver::AsyncResolver;
use tokio;
use trust_dns_resolver::{system_conf, AsyncResolver, BackgroundLookupIp};
// If instead the type were just `AsyncResolver`, it breaks the default recursion limit
// for the compiler to determine if `reqwest::Client` is `Send`. This is because `AsyncResolver`
// has **a lot** of internal generic types that pushes us over the limit.
//
// "Erasing" the internal resolver type saves us from this limit.
type ErasedResolver = Box<Fn(hyper_dns::Name) -> ::trust_dns_resolver::BackgroundLookupIp + Send + Sync>;
type ErasedResolver = Box<dyn Fn(hyper_dns::Name) -> BackgroundLookupIp + Send + Sync>;
type Background = Box<dyn Future<Item=(), Error=()> + Send>;
#[derive(Clone)]
pub(crate) struct TrustDnsResolver {
inner: Arc<Mutex<Option<ErasedResolver>>>,
inner: Arc<Inner>,
}
struct Inner {
background: Mutex<Option<Background>>,
once: Once,
resolver: ErasedResolver,
}
impl TrustDnsResolver {
pub(crate) fn new() -> Self {
TrustDnsResolver {
inner: Arc::new(Mutex::new(None)),
}
pub(crate) fn new() -> io::Result<Self> {
let (conf, opts) = system_conf::read_system_conf()?;
let (resolver, bg) = AsyncResolver::new(conf, opts);
let resolver: ErasedResolver = Box::new(move |name| {
resolver.lookup_ip(name.as_str())
});
let background = Mutex::new(Some(Box::new(bg) as Background));
let once = Once::new();
Ok(TrustDnsResolver {
inner: Arc::new(Inner {
background,
once,
resolver,
}),
})
}
}
impl hyper_dns::Resolve for TrustDnsResolver {
type Addrs = ::std::vec::IntoIter<::std::net::IpAddr>;
type Addrs = vec::IntoIter<IpAddr>;
type Future = Box<Future<Item=Self::Addrs, Error=io::Error> + Send>;
fn resolve(&self, name: hyper_dns::Name) -> Self::Future {
let inner = self.inner.clone();
Box::new(future::lazy(move || {
let mut inner = inner.lock().expect("lock resolver");
if inner.is_none() {
inner.once.call_once(|| {
// The `bg` (background) future needs to be spawned onto an executor,
// but a `reqwest::Client` may be constructed before an executor is ready.
// So, the `bg` future cannot be spawned *until* the executor starts to
// `poll` this future.
match AsyncResolver::from_system_conf() {
Ok((resolver, bg)) => {
::tokio::spawn(bg);
*inner = Some(Box::new(move |name| {
resolver.lookup_ip(name.as_str())
}));
},
Err(err) => {
return future::Either::A(
future::err(io::Error::new(io::ErrorKind::Other, err.to_string()))
);
}
}
}
let bg = inner
.background
.lock()
.expect("resolver background lock")
.take()
.expect("background only taken once");
future::Either::B((inner
.as_mut()
.expect("resolver is set"))(name)
//.lookup_ip(name.as_str())
tokio::spawn(bg);
});
(inner.resolver)(name)
.map(|lookup| {
lookup
.iter()
@@ -65,9 +78,8 @@ impl hyper_dns::Resolve for TrustDnsResolver {
})
.map_err(|err| {
io::Error::new(io::ErrorKind::Other, err.to_string())
}))
})
}))
}
}