Skip to content

Commit

Permalink
lazy init dns, #121
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Feb 20, 2024
1 parent 882c2e3 commit dd96074
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion realm_core/src/dns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,23 @@ static mut DNS: Lazy<TokioAsyncResolver> = Lazy::new(|| {
TokioAsyncResolver::tokio(conf, opts)
});

/// Force initialization.
pub fn force_init() {
use std::ptr;
unsafe {
Lazy::force(&*ptr::addr_of!(DNS));
}
}

/// Setup global dns resolver. This is not thread-safe!
pub fn build(conf: Option<ResolverConfig>, opts: Option<ResolverOpts>) {
build_lazy(conf, opts);
force_init();
}

/// Setup config of global dns resolver, without initialization.
/// This is not thread-safe!
pub fn build_lazy(conf: Option<ResolverConfig>, opts: Option<ResolverOpts>) {
let mut dns_conf = DnsConf::default();

if let Some(conf) = conf {
Expand All @@ -56,7 +71,6 @@ pub fn build(conf: Option<ResolverConfig>, opts: Option<ResolverOpts>) {

unsafe {
DNS_CONF.set(dns_conf).unwrap();
Lazy::force(&DNS);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn setup_dns(dns: DnsConf) {
println!("dns: {}", &dns);

let (conf, opts) = dns.build();
realm::core::dns::build(conf, opts);
realm::core::dns::build_lazy(conf, opts);
}

fn execute(eps: Vec<EndpointInfo>) {
Expand Down

0 comments on commit dd96074

Please sign in to comment.