Skip to content

Commit

Permalink
feat(awc): replace trust dns by hickory dns
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Dec 10, 2024
1 parent 002c1b5 commit 528478b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions awc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update `brotli` dependency to `7`.
- Prevent panics on connection pool drop when Tokio runtime is shutdown early.
- Minimum supported Rust version (MSRV) is now 1.75.
- Replace `trust-dns-resolver` with `hickory-resolver` for DNS resolution in awc when using `trust-dns` feature.

## 3.5.1

Expand Down
8 changes: 5 additions & 3 deletions awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ compress-zstd = ["actix-http/compress-zstd", "__compress"]
# Cookie parsing and cookie jar
cookies = ["dep:cookie"]

# Use `trust-dns-resolver` crate as DNS resolver
trust-dns = ["trust-dns-resolver"]
# Use `trust-dns-resolver` crate as DNS resolver, deprecated in favor of `hickory-dns` which is a drop-in replacement
trust-dns = ["hickory-dns"]
# Use `hickory-dns-resolver` crate as DNS resolver
hickory-dns = ["hickory-resolver"]

# Internal (PRIVATE!) features used to aid testing and checking feature status.
# Don't rely on these whatsoever. They may disappear at anytime.
Expand Down Expand Up @@ -130,7 +132,7 @@ tls-rustls-0_21 = { package = "rustls", version = "0.21", optional = true, featu
tls-rustls-0_22 = { package = "rustls", version = "0.22", optional = true }
tls-rustls-0_23 = { package = "rustls", version = "0.23", optional = true, default-features = false }

trust-dns-resolver = { version = "0.23", optional = true }
hickory-resolver = { version = "0.24.2", optional = true }

[dev-dependencies]
actix-http = { version = "3.7", features = ["openssl"] }
Expand Down
22 changes: 11 additions & 11 deletions awc/src/client/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ where
}
}

#[cfg(not(feature = "trust-dns"))]
#[cfg(not(feature = "hickory-dns"))]
mod resolver {
use super::*;

Expand All @@ -1041,12 +1041,12 @@ mod resolver {
}
}

#[cfg(feature = "trust-dns")]
#[cfg(feature = "hickory-dns")]
mod resolver {
use std::{cell::RefCell, net::SocketAddr};

use actix_tls::connect::Resolve;
use trust_dns_resolver::{
use hickory_resolver::{
config::{ResolverConfig, ResolverOpts},
system_conf::read_system_conf,
TokioAsyncResolver,
Expand All @@ -1056,9 +1056,9 @@ mod resolver {

pub(super) fn resolver() -> Resolver {
// new type for impl Resolve trait for TokioAsyncResolver.
struct TrustDnsResolver(TokioAsyncResolver);
struct HickoryDnsResolver(TokioAsyncResolver);

impl Resolve for TrustDnsResolver {
impl Resolve for HickoryDnsResolver {
fn lookup<'a>(
&'a self,
host: &'a str,
Expand All @@ -1080,11 +1080,11 @@ mod resolver {

// resolver struct is cached in thread local so new clients can reuse the existing instance
thread_local! {
static TRUST_DNS_RESOLVER: RefCell<Option<Resolver>> = const { RefCell::new(None) };
static HICKORY_DNS_RESOLVER: RefCell<Option<Resolver>> = const { RefCell::new(None) };
}

// get from thread local or construct a new trust-dns resolver.
TRUST_DNS_RESOLVER.with(|local| {
// get from thread local or construct a new hickory dns resolver.
HICKORY_DNS_RESOLVER.with(|local| {
let resolver = local.borrow().as_ref().map(Clone::clone);

match resolver {
Expand All @@ -1094,15 +1094,15 @@ mod resolver {
let (cfg, opts) = match read_system_conf() {
Ok((cfg, opts)) => (cfg, opts),
Err(err) => {
log::error!("Trust-DNS can not load system config: {err}");
log::error!("Hickory-DNS can not load system config: {err}");
(ResolverConfig::default(), ResolverOpts::default())
}
};

let resolver = TokioAsyncResolver::tokio(cfg, opts);

// box trust dns resolver and put it in thread local.
let resolver = Resolver::custom(TrustDnsResolver(resolver));
// box hickory dns resolver and put it in thread local.
let resolver = Resolver::custom(HickoryDnsResolver(resolver));
*local.borrow_mut() = Some(resolver.clone());

resolver
Expand Down

0 comments on commit 528478b

Please sign in to comment.