From cb30f9e86a37ba134e926d628535cfff8841e54d Mon Sep 17 00:00:00 2001 From: Jonathas-Conceicao Date: Sat, 7 Mar 2020 14:37:39 -0300 Subject: [PATCH 1/2] actix-connect: Run cargo fmt Signed-off-by: Jonathas-Conceicao --- actix-connect/tests/test_connect.rs | 60 ++++++++++++----------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index 4e9fb40a..b22352ca 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -14,12 +14,10 @@ use actix_connect::Connect; #[actix_rt::test] async fn test_string() { let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); @@ -33,12 +31,10 @@ async fn test_string() { #[actix_rt::test] async fn test_rustls_string() { let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); @@ -51,12 +47,10 @@ async fn test_rustls_string() { #[actix_rt::test] async fn test_static_str() { let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); @@ -75,12 +69,10 @@ async fn test_static_str() { #[actix_rt::test] async fn test_new_service() { let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); @@ -100,12 +92,10 @@ async fn test_uri() { use std::convert::TryFrom; let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); @@ -121,12 +111,10 @@ async fn test_rustls_uri() { use std::convert::TryFrom; let srv = TestServer::with(|| { - fn_service(|io: TcpStream| { - async { - let mut framed = Framed::new(io, BytesCodec); - framed.send(Bytes::from_static(b"test")).await?; - Ok::<_, io::Error>(()) - } + fn_service(|io: TcpStream| async { + let mut framed = Framed::new(io, BytesCodec); + framed.send(Bytes::from_static(b"test")).await?; + Ok::<_, io::Error>(()) }) }); From 7fdd4a1118cac838254bbd6d77fec5d7ff5f97cc Mon Sep 17 00:00:00 2001 From: Jonathas-Conceicao Date: Tue, 25 Feb 2020 23:44:59 -0300 Subject: [PATCH 2/2] 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 --- actix-connect/CHANGES.md | 9 ++++++ actix-connect/Cargo.toml | 4 +-- actix-connect/src/connect.rs | 2 +- actix-connect/src/lib.rs | 24 +++++++-------- actix-connect/src/resolve.rs | 45 ++++++++++++++++++++--------- actix-connect/src/service.rs | 2 +- actix-connect/src/ssl/openssl.rs | 2 +- actix-connect/tests/test_connect.rs | 6 ++-- 8 files changed, 62 insertions(+), 32 deletions(-) diff --git a/actix-connect/CHANGES.md b/actix-connect/CHANGES.md index 90d6667b..9df80434 100644 --- a/actix-connect/CHANGES.md +++ b/actix-connect/CHANGES.md @@ -1,5 +1,14 @@ # 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 ### Changed diff --git a/actix-connect/Cargo.toml b/actix-connect/Cargo.toml index 6beee4e7..d76e92c3 100644 --- a/actix-connect/Cargo.toml +++ b/actix-connect/Cargo.toml @@ -40,8 +40,8 @@ either = "1.5.3" futures = "0.3.1" http = { version = "0.2.0", optional = true } log = "0.4" -trust-dns-proto = "=0.18.0-alpha.2" -trust-dns-resolver = "=0.18.0-alpha.2" +trust-dns-proto = { version = "0.19", default-features = false, features = ["tokio-runtime"] } +trust-dns-resolver = { version = "0.19", default-features = false, features = ["tokio-runtime", "system-config"] } # openssl open-ssl = { version="0.10", package = "openssl", optional = true } diff --git a/actix-connect/src/connect.rs b/actix-connect/src/connect.rs index 1a4c51c5..f4f7f6b7 100644 --- a/actix-connect/src/connect.rs +++ b/actix-connect/src/connect.rs @@ -6,7 +6,7 @@ use std::net::SocketAddr; use either::Either; /// Connect request -pub trait Address: Unpin { +pub trait Address: Unpin + 'static { /// Host name of the request fn host(&self) -> &str; diff --git a/actix-connect/src/lib.rs b/actix-connect/src/lib.rs index 5de0b153..85dcf99e 100644 --- a/actix-connect/src/lib.rs +++ b/actix-connect/src/lib.rs @@ -25,7 +25,7 @@ use actix_rt::{net::TcpStream, Arbiter}; use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory}; use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; 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 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::service::{ConnectService, ConnectServiceFactory, TcpConnectService}; -pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver { - let (resolver, bg) = AsyncResolver::new(cfg, opts); - actix_rt::spawn(bg); - resolver +pub async fn start_resolver( + cfg: ResolverConfig, + opts: ResolverOpts, +) -> Result { + Ok(AsyncResolver::tokio(cfg, opts).await?) } struct DefaultResolver(AsyncResolver); -pub(crate) fn get_default_resolver() -> AsyncResolver { +pub(crate) async fn get_default_resolver() -> Result { if Arbiter::contains_item::() { - Arbiter::get_item(|item: &DefaultResolver| item.0.clone()) + Ok(Arbiter::get_item(|item: &DefaultResolver| item.0.clone())) } else { let (cfg, opts) = match read_system_conf() { Ok((cfg, opts)) => (cfg, opts), @@ -59,16 +60,15 @@ pub(crate) fn get_default_resolver() -> AsyncResolver { } }; - let (resolver, bg) = AsyncResolver::new(cfg, opts); - actix_rt::spawn(bg); + let resolver = AsyncResolver::tokio(cfg, opts).await?; Arbiter::set_item(DefaultResolver(resolver.clone())); - resolver + Ok(resolver) } } -pub fn start_default_resolver() -> AsyncResolver { - get_default_resolver() +pub async fn start_default_resolver() -> Result { + get_default_resolver().await } /// Create tcp connector service diff --git a/actix-connect/src/resolve.rs b/actix-connect/src/resolve.rs index 31ee05d0..70dd511a 100644 --- a/actix-connect/src/resolve.rs +++ b/actix-connect/src/resolve.rs @@ -6,8 +6,8 @@ use std::task::{Context, Poll}; use actix_service::{Service, ServiceFactory}; use futures::future::{ok, Either, Ready}; -use trust_dns_resolver::lookup_ip::LookupIpFuture; -use trust_dns_resolver::{AsyncResolver, Background}; +use trust_dns_resolver::TokioAsyncResolver as AsyncResolver; +use trust_dns_resolver::{error::ResolveError, lookup_ip::LookupIp}; use crate::connect::{Address, Connect}; use crate::error::ConnectError; @@ -106,7 +106,10 @@ impl Service for Resolver { type Request = Connect; type Response = Connect; type Error = ConnectError; - type Future = Either, Ready, Self::Error>>>; + type Future = Either< + Pin>>>, + Ready, Self::Error>>, + >; fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) @@ -119,32 +122,48 @@ impl Service for Resolver { req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port()))); Either::Right(ok(req)) } else { - trace!("DNS resolver: resolving host {:?}", req.host()); - if self.resolver.is_none() { - self.resolver = Some(get_default_resolver()); - } - Either::Left(ResolverFuture::new(req, self.resolver.as_ref().unwrap())) + let resolver = self.resolver.as_ref().map(AsyncResolver::clone); + Either::Left(Box::pin(async move { + trace!("DNS resolver: resolving host {:?}", req.host()); + let resolver = if let Some(resolver) = resolver { + resolver + } else { + get_default_resolver() + .await + .expect("Failed to get default resolver") + }; + ResolverFuture::new(req, &resolver).await + })) } } } +type LookupIpFuture = Pin>>>; + #[doc(hidden)] /// Resolver future pub struct ResolverFuture { req: Option>, - lookup: Background, + lookup: LookupIpFuture, } impl ResolverFuture { pub fn new(req: Connect, resolver: &AsyncResolver) -> Self { - let lookup = if let Some(host) = req.host().splitn(2, ':').next() { - resolver.lookup_ip(host) + let host = if let Some(host) = req.host().splitn(2, ':').next() { + host } 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 { - lookup, + lookup: Box::pin(async move { + let resolver = resolver_clone; + resolver.lookup_ip(host_clone).await + }), req: Some(req), } } diff --git a/actix-connect/src/service.rs b/actix-connect/src/service.rs index 6a290929..dcd03d2c 100644 --- a/actix-connect/src/service.rs +++ b/actix-connect/src/service.rs @@ -6,7 +6,7 @@ use actix_rt::net::TcpStream; use actix_service::{Service, ServiceFactory}; use either::Either; 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::connector::{TcpConnector, TcpConnectorFactory}; diff --git a/actix-connect/src/ssl/openssl.rs b/actix-connect/src/ssl/openssl.rs index f2fa1943..c9cf666e 100644 --- a/actix-connect/src/ssl/openssl.rs +++ b/actix-connect/src/ssl/openssl.rs @@ -11,7 +11,7 @@ use actix_codec::{AsyncRead, AsyncWrite}; use actix_rt::net::TcpStream; use actix_service::{Service, ServiceFactory}; use futures::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready}; -use trust_dns_resolver::AsyncResolver; +use trust_dns_resolver::TokioAsyncResolver as AsyncResolver; use crate::{ Address, Connect, ConnectError, ConnectService, ConnectServiceFactory, Connection, diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index b22352ca..1712bf49 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -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 con = conn.call(Connect::with("10", srv.addr())).await.unwrap(); @@ -77,7 +77,9 @@ async fn test_new_service() { }); 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);