mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-30 19:54:36 +01:00
actix-connect: Upgrade versions of trust-dns
- `Address` trait is now required to have static lifetime; - `start_resolver` and `start_default_resolver` are now `async` and may return a `ConnectError`; Signed-off-by: Jonathas-Conceicao <jadoliveira@inf.ufpel.edu.br>
This commit is contained in:
parent
cb30f9e86a
commit
7fdd4a1118
@ -1,5 +1,14 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [TBD] - TBD
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Update `trust-dns-proto` dependency to 0.19 [#116]
|
||||||
|
* Update `trust-dns-resolver` dependency to 0.19 [#116]
|
||||||
|
* `Address` trait is now required to have static lifetime [#116]
|
||||||
|
* `start_resolver` and `start_default_resolver` are now `async` and may return a `ConnectError` [#116]
|
||||||
|
|
||||||
## [2.0.0-alpha.1] - 2020-03-03
|
## [2.0.0-alpha.1] - 2020-03-03
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -40,8 +40,8 @@ either = "1.5.3"
|
|||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
http = { version = "0.2.0", optional = true }
|
http = { version = "0.2.0", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
trust-dns-proto = "=0.18.0-alpha.2"
|
trust-dns-proto = { version = "0.19", default-features = false, features = ["tokio-runtime"] }
|
||||||
trust-dns-resolver = "=0.18.0-alpha.2"
|
trust-dns-resolver = { version = "0.19", default-features = false, features = ["tokio-runtime", "system-config"] }
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
open-ssl = { version="0.10", package = "openssl", optional = true }
|
open-ssl = { version="0.10", package = "openssl", optional = true }
|
||||||
|
@ -6,7 +6,7 @@ use std::net::SocketAddr;
|
|||||||
use either::Either;
|
use either::Either;
|
||||||
|
|
||||||
/// Connect request
|
/// Connect request
|
||||||
pub trait Address: Unpin {
|
pub trait Address: Unpin + 'static {
|
||||||
/// Host name of the request
|
/// Host name of the request
|
||||||
fn host(&self) -> &str;
|
fn host(&self) -> &str;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ use actix_rt::{net::TcpStream, Arbiter};
|
|||||||
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
use trust_dns_resolver::system_conf::read_system_conf;
|
use trust_dns_resolver::system_conf::read_system_conf;
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
pub mod resolver {
|
pub mod resolver {
|
||||||
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
@ -39,17 +39,18 @@ pub use self::error::ConnectError;
|
|||||||
pub use self::resolve::{Resolver, ResolverFactory};
|
pub use self::resolve::{Resolver, ResolverFactory};
|
||||||
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
||||||
|
|
||||||
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
pub async fn start_resolver(
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
cfg: ResolverConfig,
|
||||||
actix_rt::spawn(bg);
|
opts: ResolverOpts,
|
||||||
resolver
|
) -> Result<AsyncResolver, ConnectError> {
|
||||||
|
Ok(AsyncResolver::tokio(cfg, opts).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DefaultResolver(AsyncResolver);
|
struct DefaultResolver(AsyncResolver);
|
||||||
|
|
||||||
pub(crate) fn get_default_resolver() -> AsyncResolver {
|
pub(crate) async fn get_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
||||||
if Arbiter::contains_item::<DefaultResolver>() {
|
if Arbiter::contains_item::<DefaultResolver>() {
|
||||||
Arbiter::get_item(|item: &DefaultResolver| item.0.clone())
|
Ok(Arbiter::get_item(|item: &DefaultResolver| item.0.clone()))
|
||||||
} else {
|
} else {
|
||||||
let (cfg, opts) = match read_system_conf() {
|
let (cfg, opts) = match read_system_conf() {
|
||||||
Ok((cfg, opts)) => (cfg, opts),
|
Ok((cfg, opts)) => (cfg, opts),
|
||||||
@ -59,16 +60,15 @@ pub(crate) fn get_default_resolver() -> AsyncResolver {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
let resolver = AsyncResolver::tokio(cfg, opts).await?;
|
||||||
actix_rt::spawn(bg);
|
|
||||||
|
|
||||||
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
||||||
resolver
|
Ok(resolver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_default_resolver() -> AsyncResolver {
|
pub async fn start_default_resolver() -> Result<AsyncResolver, ConnectError> {
|
||||||
get_default_resolver()
|
get_default_resolver().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create tcp connector service
|
/// Create tcp connector service
|
||||||
|
@ -6,8 +6,8 @@ use std::task::{Context, Poll};
|
|||||||
|
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Either, Ready};
|
use futures::future::{ok, Either, Ready};
|
||||||
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
use trust_dns_resolver::{AsyncResolver, Background};
|
use trust_dns_resolver::{error::ResolveError, lookup_ip::LookupIp};
|
||||||
|
|
||||||
use crate::connect::{Address, Connect};
|
use crate::connect::{Address, Connect};
|
||||||
use crate::error::ConnectError;
|
use crate::error::ConnectError;
|
||||||
@ -106,7 +106,10 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connect<T>;
|
type Response = Connect<T>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<ResolverFuture<T>, Ready<Result<Connect<T>, Self::Error>>>;
|
type Future = Either<
|
||||||
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>,
|
||||||
|
Ready<Result<Connect<T>, Self::Error>>,
|
||||||
|
>;
|
||||||
|
|
||||||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
@ -119,32 +122,48 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
||||||
Either::Right(ok(req))
|
Either::Right(ok(req))
|
||||||
} else {
|
} else {
|
||||||
|
let resolver = self.resolver.as_ref().map(AsyncResolver::clone);
|
||||||
|
Either::Left(Box::pin(async move {
|
||||||
trace!("DNS resolver: resolving host {:?}", req.host());
|
trace!("DNS resolver: resolving host {:?}", req.host());
|
||||||
if self.resolver.is_none() {
|
let resolver = if let Some(resolver) = resolver {
|
||||||
self.resolver = Some(get_default_resolver());
|
resolver
|
||||||
}
|
} else {
|
||||||
Either::Left(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
get_default_resolver()
|
||||||
|
.await
|
||||||
|
.expect("Failed to get default resolver")
|
||||||
|
};
|
||||||
|
ResolverFuture::new(req, &resolver).await
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LookupIpFuture = Pin<Box<dyn Future<Output = Result<LookupIp, ResolveError>>>>;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Resolver future
|
/// Resolver future
|
||||||
pub struct ResolverFuture<T: Address> {
|
pub struct ResolverFuture<T: Address> {
|
||||||
req: Option<Connect<T>>,
|
req: Option<Connect<T>>,
|
||||||
lookup: Background<LookupIpFuture>,
|
lookup: LookupIpFuture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> ResolverFuture<T> {
|
impl<T: Address> ResolverFuture<T> {
|
||||||
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
pub fn new(req: Connect<T>, resolver: &AsyncResolver) -> Self {
|
||||||
let lookup = if let Some(host) = req.host().splitn(2, ':').next() {
|
let host = if let Some(host) = req.host().splitn(2, ':').next() {
|
||||||
resolver.lookup_ip(host)
|
host
|
||||||
} else {
|
} else {
|
||||||
resolver.lookup_ip(req.host())
|
req.host()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Clone data to be moved to the lookup future
|
||||||
|
let host_clone = host.to_owned();
|
||||||
|
let resolver_clone = resolver.clone();
|
||||||
|
|
||||||
ResolverFuture {
|
ResolverFuture {
|
||||||
lookup,
|
lookup: Box::pin(async move {
|
||||||
|
let resolver = resolver_clone;
|
||||||
|
resolver.lookup_ip(host_clone).await
|
||||||
|
}),
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use actix_rt::net::TcpStream;
|
|||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use futures::future::{ok, Ready};
|
use futures::future::{ok, Ready};
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
use crate::connect::{Address, Connect, Connection};
|
use crate::connect::{Address, Connect, Connection};
|
||||||
use crate::connector::{TcpConnector, TcpConnectorFactory};
|
use crate::connector::{TcpConnector, TcpConnectorFactory};
|
||||||
|
@ -11,7 +11,7 @@ use actix_codec::{AsyncRead, AsyncWrite};
|
|||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{Service, ServiceFactory};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready};
|
use futures::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready};
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::TokioAsyncResolver as AsyncResolver;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection,
|
Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection,
|
||||||
|
@ -54,7 +54,7 @@ async fn test_static_str() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = actix_connect::start_default_resolver();
|
let resolver = actix_connect::start_default_resolver().await.unwrap();
|
||||||
let mut conn = actix_connect::new_connector(resolver.clone());
|
let mut conn = actix_connect::new_connector(resolver.clone());
|
||||||
|
|
||||||
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
||||||
@ -77,7 +77,9 @@ async fn test_new_service() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let resolver =
|
let resolver =
|
||||||
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default());
|
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let factory = actix_connect::new_connector_factory(resolver);
|
let factory = actix_connect::new_connector_factory(resolver);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user