refactor: init hickory DNS using OnceCell

This commit is contained in:
Rob Ede 2025-09-09 09:03:09 +01:00
parent 62d6d5863e
commit d43738306c
No known key found for this signature in database
GPG Key ID: F5E3FCAA33CBF062
3 changed files with 11 additions and 18 deletions

View File

@ -2,12 +2,14 @@ version: "0.2"
words: words:
- actix - actix
- addrs - addrs
- ALPN
- bytestring - bytestring
- httparse - httparse
- msrv - MSRV
- realip - realip
- rustls - rustls
- rustup - rustup
- serde - serde
- uring - uring
- webpki
- zstd - zstd

View File

@ -16,7 +16,6 @@ Middleware is registered for each App, Scope, or Resource and executed in the re
Actix Web's middleware system is built on two main traits: Actix Web's middleware system is built on two main traits:
1. `Transform<S, Req>`: The builder trait that creates the actual Service. It's responsible for: 1. `Transform<S, Req>`: The builder trait that creates the actual Service. It's responsible for:
- Creating new middleware instances - Creating new middleware instances
- Assembling the middleware chain - Assembling the middleware chain
- Handling initialization errors - Handling initialization errors

View File

@ -1048,7 +1048,7 @@ mod resolver {
#[cfg(feature = "hickory-dns")] #[cfg(feature = "hickory-dns")]
mod resolver { mod resolver {
use std::{cell::RefCell, net::SocketAddr}; use std::{cell::OnceCell, net::SocketAddr};
use actix_tls::connect::Resolve; use actix_tls::connect::Resolve;
use hickory_resolver::{ use hickory_resolver::{
@ -1086,21 +1086,17 @@ mod resolver {
// resolver struct is cached in thread local so new clients can reuse the existing instance // resolver struct is cached in thread local so new clients can reuse the existing instance
thread_local! { thread_local! {
static HICKORY_DNS_RESOLVER: RefCell<Option<Resolver>> = const { RefCell::new(None) }; static HICKORY_DNS_RESOLVER: OnceCell<Resolver> = const { OnceCell::new() };
} }
// get from thread local or construct a new hickory dns resolver. // get from thread local or construct a new hickory dns resolver.
HICKORY_DNS_RESOLVER.with(|local| { HICKORY_DNS_RESOLVER.with(|local| {
let resolver = local.borrow().as_ref().map(Clone::clone); local
.get_or_init(|| {
match resolver {
Some(resolver) => resolver,
None => {
let (cfg, opts) = match read_system_conf() { let (cfg, opts) = match read_system_conf() {
Ok((cfg, opts)) => (cfg, opts), Ok((cfg, opts)) => (cfg, opts),
Err(err) => { Err(err) => {
log::error!("Hickory-DNS can not load system config: {err}"); log::error!("Hickory DNS can not load system config: {err}");
(ResolverConfig::default(), ResolverOpts::default()) (ResolverConfig::default(), ResolverOpts::default())
} }
}; };
@ -1110,13 +1106,9 @@ mod resolver {
.with_options(opts) .with_options(opts)
.build(); .build();
// box hickory dns resolver and put it in thread local Resolver::custom(HickoryDnsResolver(resolver))
let resolver = Resolver::custom(HickoryDnsResolver(resolver)); })
*local.borrow_mut() = Some(resolver.clone()); .clone()
resolver
}
}
}) })
} }
} }