From dd96074c2a9d70d40d9bb69e77908314b9222e18 Mon Sep 17 00:00:00 2001 From: zephyr Date: Mon, 19 Feb 2024 23:43:42 +0900 Subject: [PATCH] lazy init dns, #121 --- realm_core/src/dns/mod.rs | 16 +++++++++++++++- src/bin.rs | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) 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) {