From 7fc9fb93e2d30badb95a87e2406f225ea73b8b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Fri, 13 Mar 2026 14:00:10 +0100 Subject: [PATCH] refactor: Remove special fast path for hickory resolver (#4022) ## Description Remove special fast path for hickory resolver The downside is that we have 1 more box for each lookup_ipv6 or lookup_ipv4, but I doubt that it matters. These are network ops, maybe cache hits, but in any case much more heavyweight than a boxed future. The upside is that hickory will be easier to feature gate later, which we need for embedded. And it's less code. Rationale for this: hickory is really nasty on embedded. It requires giant (for embedded) stack sizes and also is a big dep. We need it for address_discovery_dns, but there is an alternative for that: just use pkarr for both put and get. If we don't have a default dep on hickory we can implement a simple dns resolver that just uses getaddrinfo. It won't work for TXT records, but we don't need them for the core functionality of doing just plain A or AAAA requests to figure out relay ip addrs. ## Breaking Changes None ## Notes & open questions ## Change checklist - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme) --- iroh-relay/src/dns.rs | 153 +++++++++++------------------------------- 1 file changed, 38 insertions(+), 115 deletions(-) diff --git a/iroh-relay/src/dns.rs b/iroh-relay/src/dns.rs index 7f035c1289..61e0d23ef5 100644 --- a/iroh-relay/src/dns.rs +++ b/iroh-relay/src/dns.rs @@ -200,8 +200,7 @@ impl Builder { /// Builds the DNS resolver. pub fn build(self) -> DnsResolver { - let resolver = HickoryResolver::new(self); - DnsResolver(DnsResolverInner::Hickory(Arc::new(RwLock::new(resolver)))) + DnsResolver(Arc::new(RwLock::new(HickoryResolver::new(self)))) } } @@ -212,7 +211,7 @@ impl Builder { /// Alternatively, you can create a fully custom DNS resolver by implementing the [`Resolver`] /// trait and creating the resolver with [`Self::custom`]. #[derive(Debug, Clone)] -pub struct DnsResolver(DnsResolverInner); +pub struct DnsResolver(Arc>); impl DnsResolver { /// Creates a new DNS resolver with sensible cross-platform defaults. @@ -242,17 +241,17 @@ impl DnsResolver { /// implement the [`Resolver`] trait on a struct and implement DNS resolution /// however you see fit. pub fn custom(resolver: impl Resolver) -> Self { - Self(DnsResolverInner::Custom(Arc::new(RwLock::new(resolver)))) + Self(Arc::new(RwLock::new(resolver))) } /// Removes all entries from the cache. pub async fn clear_cache(&self) { - self.0.clear_cache().await + self.0.read().await.clear_cache() } /// Recreates the inner resolver. pub async fn reset(&self) { - self.0.reset().await + self.0.write().await.reset() } /// Looks up a TXT record. @@ -262,7 +261,8 @@ impl DnsResolver { timeout: Duration, ) -> Result, DnsError> { let host = host.to_string(); - let res = time::timeout(timeout, self.0.lookup_txt(host)).await??; + let fut = self.0.read().await.lookup_txt(host); + let res = time::timeout(timeout, fut).await??; Ok(res) } @@ -273,7 +273,8 @@ impl DnsResolver { timeout: Duration, ) -> Result + use, DnsError> { let host = host.to_string(); - let addrs = time::timeout(timeout, self.0.lookup_ipv4(host)).await??; + let fut = self.0.read().await.lookup_ipv4(host); + let addrs = time::timeout(timeout, fut).await??; Ok(addrs.into_iter().map(IpAddr::V4)) } @@ -284,7 +285,8 @@ impl DnsResolver { timeout: Duration, ) -> Result + use, DnsError> { let host = host.to_string(); - let addrs = time::timeout(timeout, self.0.lookup_ipv6(host)).await??; + let fut = self.0.read().await.lookup_ipv6(host); + let addrs = time::timeout(timeout, fut).await??; Ok(addrs.into_iter().map(IpAddr::V6)) } @@ -490,62 +492,6 @@ impl reqwest::dns::Resolve for DnsResolver { } } -/// Wrapper enum that contains either a hickory resolver or a custom resolver. -/// -/// We do this to save the cost of boxing the futures and iterators when using -/// default hickory resolver. -#[derive(Debug, Clone)] -enum DnsResolverInner { - Hickory(Arc>), - Custom(Arc>), -} - -impl DnsResolverInner { - async fn lookup_ipv4( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(match self { - Self::Hickory(resolver) => Either::Left(resolver.read().await.lookup_ipv4(host).await?), - Self::Custom(resolver) => Either::Right(resolver.read().await.lookup_ipv4(host).await?), - }) - } - - async fn lookup_ipv6( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(match self { - Self::Hickory(resolver) => Either::Left(resolver.read().await.lookup_ipv6(host).await?), - Self::Custom(resolver) => Either::Right(resolver.read().await.lookup_ipv6(host).await?), - }) - } - - async fn lookup_txt( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(match self { - Self::Hickory(resolver) => Either::Left(resolver.read().await.lookup_txt(host).await?), - Self::Custom(resolver) => Either::Right(resolver.read().await.lookup_txt(host).await?), - }) - } - - async fn clear_cache(&self) { - match self { - Self::Hickory(resolver) => resolver.read().await.clear_cache(), - Self::Custom(resolver) => resolver.read().await.clear_cache(), - } - } - - async fn reset(&self) { - match self { - Self::Hickory(resolver) => resolver.write().await.reset(), - Self::Custom(resolver) => resolver.write().await.reset(), - } - } -} - #[derive(Debug)] struct HickoryResolver { resolver: TokioResolver, @@ -610,46 +556,40 @@ impl HickoryResolver { } Ok((config, options)) } +} - async fn lookup_ipv4( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(self - .resolver - .ipv4_lookup(host) - .await? - .into_iter() - .map(Ipv4Addr::from)) +impl Resolver for HickoryResolver { + fn lookup_ipv4(&self, host: String) -> BoxFuture, DnsError>> { + let resolver = self.resolver.clone(); + Box::pin(async move { + let lookup = resolver.ipv4_lookup(host).await?; + let iter: BoxIter = Box::new(lookup.into_iter().map(Ipv4Addr::from)); + Ok(iter) + }) } - /// Looks up an IPv6 address. - async fn lookup_ipv6( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(self - .resolver - .ipv6_lookup(host) - .await? - .into_iter() - .map(Ipv6Addr::from)) + fn lookup_ipv6(&self, host: String) -> BoxFuture, DnsError>> { + let resolver = self.resolver.clone(); + Box::pin(async move { + let lookup = resolver.ipv6_lookup(host).await?; + let iter: BoxIter = Box::new(lookup.into_iter().map(Ipv6Addr::from)); + Ok(iter) + }) } - /// Looks up TXT records. - async fn lookup_txt( - &self, - host: String, - ) -> Result + use<>, DnsError> { - Ok(self - .resolver - .txt_lookup(host) - .await? - .into_iter() - .map(|txt| TxtRecordData::from_iter(txt.iter().cloned()))) + fn lookup_txt(&self, host: String) -> BoxFuture, DnsError>> { + let resolver = self.resolver.clone(); + Box::pin(async move { + let lookup = resolver.txt_lookup(host).await?; + let iter: BoxIter = Box::new( + lookup + .into_iter() + .map(|txt| TxtRecordData::from_iter(txt.iter().cloned())), + ); + Ok(iter) + }) } - /// Clears the internal cache. fn clear_cache(&self) { self.resolver.clear_cache() } @@ -701,23 +641,6 @@ impl From>> for TxtRecordData { } } -/// Helper enum to give a unified type to either of two iterators -enum Either { - Left(A), - Right(B), -} - -impl, B: Iterator> Iterator for Either { - type Item = T; - - fn next(&mut self) -> Option { - match self { - Either::Left(iter) => iter.next(), - Either::Right(iter) => iter.next(), - } - } -} - /// Deprecated IPv6 site-local anycast addresses still configured by windows. /// /// Windows still configures these site-local addresses as soon even as an IPv6 loopback