diff --git a/realm_core/src/dns/mod.rs b/realm_core/src/dns/mod.rs index 4ce51507..ad5d489b 100644 --- a/realm_core/src/dns/mod.rs +++ b/realm_core/src/dns/mod.rs @@ -42,8 +42,23 @@ static mut DNS: Lazy = 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, opts: Option) { + 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, opts: Option) { let mut dns_conf = DnsConf::default(); if let Some(conf) = conf { @@ -56,7 +71,6 @@ pub fn build(conf: Option, opts: Option) { unsafe { DNS_CONF.set(dns_conf).unwrap(); - Lazy::force(&DNS); } } diff --git a/src/bin.rs b/src/bin.rs index fb746c41..76799613 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -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) {