1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-01 04:00:08 +01:00
actix-net/src/resolver.rs

125 lines
3.2 KiB
Rust
Raw Normal View History

2018-09-11 04:16:46 +02:00
use std::collections::VecDeque;
use std::marker::PhantomData;
use std::net::SocketAddr;
use futures::{Async, Future, Poll};
use tokio_current_thread::spawn;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
2018-10-30 04:29:47 +01:00
pub use trust_dns_resolver::error::ResolveError;
2018-09-11 04:16:46 +02:00
use trust_dns_resolver::lookup_ip::LookupIpFuture;
use trust_dns_resolver::system_conf::read_system_conf;
use trust_dns_resolver::{AsyncResolver, Background};
2018-09-11 18:30:22 +02:00
use super::service::Service;
2018-09-11 04:16:46 +02:00
pub trait HostAware {
fn host(&self) -> &str;
}
impl HostAware for String {
fn host(&self) -> &str {
self.as_ref()
}
}
pub struct Resolver<T = String> {
resolver: AsyncResolver,
req: PhantomData<T>,
}
impl<T: HostAware> Default for Resolver<T> {
fn default() -> Self {
let (cfg, opts) = if let Ok((cfg, opts)) = read_system_conf() {
(cfg, opts)
} else {
(ResolverConfig::default(), ResolverOpts::default())
};
Resolver::new(cfg, opts)
}
}
impl<T: HostAware> Resolver<T> {
pub fn new(cfg: ResolverConfig, opts: ResolverOpts) -> Self {
let (resolver, bg) = AsyncResolver::new(cfg, opts);
spawn(bg);
Resolver {
resolver,
req: PhantomData,
}
}
2018-09-11 04:39:55 +02:00
pub fn change_request<T2: HostAware>(&self) -> Resolver<T2> {
Resolver {
resolver: self.resolver.clone(),
req: PhantomData,
}
}
2018-09-11 04:16:46 +02:00
}
impl<T> Clone for Resolver<T> {
fn clone(&self) -> Self {
Resolver {
resolver: self.resolver.clone(),
req: PhantomData,
}
}
}
impl<T: HostAware> Service for Resolver<T> {
type Request = T;
2018-10-30 04:29:47 +01:00
type Response = (T, VecDeque<SocketAddr>);
type Error = ResolveError;
2018-09-11 04:16:46 +02:00
type Future = ResolverFuture<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
2018-10-30 04:29:47 +01:00
ResolverFuture::new(req, &self.resolver)
2018-09-11 04:16:46 +02:00
}
}
#[doc(hidden)]
/// Resolver future
pub struct ResolverFuture<T> {
2018-09-11 04:39:55 +02:00
req: Option<T>,
2018-09-11 04:16:46 +02:00
lookup: Option<Background<LookupIpFuture>>,
addrs: Option<VecDeque<SocketAddr>>,
}
impl<T: HostAware> ResolverFuture<T> {
2018-10-30 04:29:47 +01:00
pub fn new(addr: T, resolver: &AsyncResolver) -> Self {
2018-09-11 04:16:46 +02:00
// we need to do dns resolution
2018-10-30 04:29:47 +01:00
let lookup = Some(resolver.lookup_ip(addr.host()));
ResolverFuture {
lookup,
req: Some(addr),
addrs: None,
2018-09-11 04:16:46 +02:00
}
}
}
impl<T: HostAware> Future for ResolverFuture<T> {
2018-10-30 04:29:47 +01:00
type Item = (T, VecDeque<SocketAddr>);
type Error = ResolveError;
2018-09-11 04:16:46 +02:00
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
2018-10-30 04:29:47 +01:00
if let Some(addrs) = self.addrs.take() {
Ok(Async::Ready((self.req.take().unwrap(), addrs)))
2018-09-11 04:16:46 +02:00
} else {
match self.lookup.as_mut().unwrap().poll() {
Ok(Async::NotReady) => Ok(Async::NotReady),
Ok(Async::Ready(ips)) => {
2018-10-30 04:29:47 +01:00
let addrs: VecDeque<_> =
ips.iter().map(|ip| SocketAddr::new(ip, 0)).collect();
Ok(Async::Ready((self.req.take().unwrap(), addrs)))
2018-09-11 04:16:46 +02:00
}
2018-10-30 04:29:47 +01:00
Err(err) => Err(err),
2018-09-11 04:16:46 +02:00
}
}
}
}