Skip to content

Commit d437383

Browse files
committed
refactor: init hickory DNS using OnceCell
1 parent 62d6d58 commit d437383

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

.cspell.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ version: "0.2"
22
words:
33
- actix
44
- addrs
5+
- ALPN
56
- bytestring
67
- httparse
7-
- msrv
8+
- MSRV
89
- realip
910
- rustls
1011
- rustup
1112
- serde
1213
- uring
14+
- webpki
1315
- zstd

actix-web/src/middleware/authors-guide.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Middleware is registered for each App, Scope, or Resource and executed in the re
1616
Actix Web's middleware system is built on two main traits:
1717

1818
1. `Transform<S, Req>`: The builder trait that creates the actual Service. It's responsible for:
19-
2019
- Creating new middleware instances
2120
- Assembling the middleware chain
2221
- Handling initialization errors

awc/src/client/connector.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ mod resolver {
10481048

10491049
#[cfg(feature = "hickory-dns")]
10501050
mod resolver {
1051-
use std::{cell::RefCell, net::SocketAddr};
1051+
use std::{cell::OnceCell, net::SocketAddr};
10521052

10531053
use actix_tls::connect::Resolve;
10541054
use hickory_resolver::{
@@ -1086,21 +1086,17 @@ mod resolver {
10861086

10871087
// resolver struct is cached in thread local so new clients can reuse the existing instance
10881088
thread_local! {
1089-
static HICKORY_DNS_RESOLVER: RefCell<Option<Resolver>> = const { RefCell::new(None) };
1089+
static HICKORY_DNS_RESOLVER: OnceCell<Resolver> = const { OnceCell::new() };
10901090
}
10911091

10921092
// get from thread local or construct a new hickory dns resolver.
10931093
HICKORY_DNS_RESOLVER.with(|local| {
1094-
let resolver = local.borrow().as_ref().map(Clone::clone);
1095-
1096-
match resolver {
1097-
Some(resolver) => resolver,
1098-
1099-
None => {
1094+
local
1095+
.get_or_init(|| {
11001096
let (cfg, opts) = match read_system_conf() {
11011097
Ok((cfg, opts)) => (cfg, opts),
11021098
Err(err) => {
1103-
log::error!("Hickory-DNS can not load system config: {err}");
1099+
log::error!("Hickory DNS can not load system config: {err}");
11041100
(ResolverConfig::default(), ResolverOpts::default())
11051101
}
11061102
};
@@ -1110,13 +1106,9 @@ mod resolver {
11101106
.with_options(opts)
11111107
.build();
11121108

1113-
// box hickory dns resolver and put it in thread local
1114-
let resolver = Resolver::custom(HickoryDnsResolver(resolver));
1115-
*local.borrow_mut() = Some(resolver.clone());
1116-
1117-
resolver
1118-
}
1119-
}
1109+
Resolver::custom(HickoryDnsResolver(resolver))
1110+
})
1111+
.clone()
11201112
})
11211113
}
11221114
}

0 commit comments

Comments
 (0)