|
| 1 | +use std::fmt::Write; |
| 2 | +use std::net::IpAddr; |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use futures_lite::{Future, StreamExt}; |
| 6 | +use hickory_resolver::{IntoName, TokioAsyncResolver}; |
| 7 | + |
| 8 | +use crate::defaults::timeouts::DNS_TIMEOUT; |
| 9 | + |
| 10 | +/// Delay used to perform staggered dns queries. |
| 11 | +const DNS_STAGGERING_MS: &[u64] = &[200, 300]; |
| 12 | + |
| 13 | +/// Extension trait to [`TokioAsyncResolver`]. |
| 14 | +pub trait ResolverExt { |
| 15 | + /// Perform an ipv4 lookup with a timeout. |
| 16 | + fn lookup_ipv4<N: IntoName>( |
| 17 | + &self, |
| 18 | + host: N, |
| 19 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 20 | + |
| 21 | + /// Perform an ipv6 lookup with a timeout. |
| 22 | + fn lookup_ipv6<N: IntoName>( |
| 23 | + &self, |
| 24 | + host: N, |
| 25 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 26 | + |
| 27 | + /// Race an ipv4 and ipv6 lookup with a timeout. |
| 28 | + fn lookup_ipv4_ipv6<N: IntoName + Clone>( |
| 29 | + &self, |
| 30 | + host: N, |
| 31 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 32 | + |
| 33 | + /// Perform an ipv4 lookup with a timeout in a staggered fashion. |
| 34 | + /// |
| 35 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 36 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 37 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied to each call individually. The |
| 38 | + /// result of the first successful call is returned, or a summary of all errors otherwise. |
| 39 | + fn lookup_ipv4_staggered<N: IntoName + Clone>( |
| 40 | + &self, |
| 41 | + host: N, |
| 42 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 43 | + |
| 44 | + /// Perform an ipv6 lookup with a timeout in a staggered fashion. |
| 45 | + /// |
| 46 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 47 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 48 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied to each call individually. The |
| 49 | + /// result of the first successful call is returned, or a summary of all errors otherwise. |
| 50 | + fn lookup_ipv6_staggered<N: IntoName + Clone>( |
| 51 | + &self, |
| 52 | + host: N, |
| 53 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 54 | + |
| 55 | + /// Race an ipv4 and ipv6 lookup with a timeout in a staggered fashion. |
| 56 | + /// |
| 57 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 58 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 59 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied as stated in |
| 60 | + /// [`Self::lookup_ipv4_ipv6`]. The result of the first successful call is returned, or a |
| 61 | + /// summary of all errors otherwise. |
| 62 | + // TODO(@divma): adjust docs |
| 63 | + fn lookup_ipv4_ipv6_staggered<N: IntoName + Clone>( |
| 64 | + &self, |
| 65 | + host: N, |
| 66 | + ) -> impl Future<Output = Result<impl Iterator<Item = IpAddr>>>; |
| 67 | +} |
| 68 | + |
| 69 | +impl ResolverExt for TokioAsyncResolver { |
| 70 | + async fn lookup_ipv4<N: IntoName>(&self, host: N) -> Result<impl Iterator<Item = IpAddr>> { |
| 71 | + let addrs = tokio::time::timeout(DNS_TIMEOUT, self.ipv4_lookup(host)).await??; |
| 72 | + Ok(addrs.into_iter().map(|ip| IpAddr::V4(ip.0))) |
| 73 | + } |
| 74 | + |
| 75 | + async fn lookup_ipv6<N: IntoName>(&self, host: N) -> Result<impl Iterator<Item = IpAddr>> { |
| 76 | + let addrs = tokio::time::timeout(DNS_TIMEOUT, self.ipv6_lookup(host)).await??; |
| 77 | + Ok(addrs.into_iter().map(|ip| IpAddr::V6(ip.0))) |
| 78 | + } |
| 79 | + |
| 80 | + /// Resolve IPv4 and IPv6 in parallel. |
| 81 | + /// |
| 82 | + /// `LookupIpStrategy::Ipv4AndIpv6` will wait for ipv6 resolution timeout, even if it is |
| 83 | + /// not usable on the stack, so we manually query both lookups concurrently and time them out |
| 84 | + /// individually. |
| 85 | + async fn lookup_ipv4_ipv6<N: IntoName + Clone>( |
| 86 | + &self, |
| 87 | + host: N, |
| 88 | + ) -> Result<impl Iterator<Item = IpAddr>> { |
| 89 | + let res = tokio::join!(self.lookup_ipv4(host.clone()), self.lookup_ipv6(host)); |
| 90 | + |
| 91 | + match res { |
| 92 | + (Ok(ipv4), Ok(ipv6)) => Ok(LookupIter::Both(ipv4.chain(ipv6))), |
| 93 | + (Ok(ipv4), Err(_)) => Ok(LookupIter::Ipv4(ipv4)), |
| 94 | + (Err(_), Ok(ipv6)) => Ok(LookupIter::Ipv6(ipv6)), |
| 95 | + (Err(ipv4_err), Err(ipv6_err)) => { |
| 96 | + anyhow::bail!("Ipv4: {:?}, Ipv6: {:?}", ipv4_err, ipv6_err) |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /// Perform an ipv4 lookup with a timeout in a staggered fashion. |
| 102 | + /// |
| 103 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 104 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 105 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied to each call individually. The |
| 106 | + /// result of the first successful call is returned, or a summary of all errors otherwise. |
| 107 | + async fn lookup_ipv4_staggered<N: IntoName + Clone>( |
| 108 | + &self, |
| 109 | + host: N, |
| 110 | + ) -> Result<impl Iterator<Item = IpAddr>> { |
| 111 | + let f = || self.lookup_ipv4(host.clone()); |
| 112 | + stagger_call(f, DNS_STAGGERING_MS).await |
| 113 | + } |
| 114 | + |
| 115 | + /// Perform an ipv6 lookup with a timeout in a staggered fashion. |
| 116 | + /// |
| 117 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 118 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 119 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied to each call individually. The |
| 120 | + /// result of the first successful call is returned, or a summary of all errors otherwise. |
| 121 | + async fn lookup_ipv6_staggered<N: IntoName + Clone>( |
| 122 | + &self, |
| 123 | + host: N, |
| 124 | + ) -> Result<impl Iterator<Item = IpAddr>> { |
| 125 | + let f = || self.lookup_ipv6(host.clone()); |
| 126 | + stagger_call(f, DNS_STAGGERING_MS).await |
| 127 | + } |
| 128 | + |
| 129 | + /// Race an ipv4 and ipv6 lookup with a timeout in a staggered fashion. |
| 130 | + /// |
| 131 | + /// From the moment this function is called, each lookup is scheduled after the delays in |
| 132 | + /// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls |
| 133 | + /// at T+0ms, T+200ms and T+300ms. The `timeout` is applied as stated in |
| 134 | + /// [`Self::lookup_ipv4_ipv6`]. The result of the first successful call is returned, or a |
| 135 | + /// summary of all errors otherwise. |
| 136 | + async fn lookup_ipv4_ipv6_staggered<N: IntoName + Clone>( |
| 137 | + &self, |
| 138 | + host: N, |
| 139 | + ) -> Result<impl Iterator<Item = IpAddr>> { |
| 140 | + let f = || self.lookup_ipv4_ipv6(host.clone()); |
| 141 | + stagger_call(f, DNS_STAGGERING_MS).await |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Helper enum to give a unified type to the iterators of [`ResolverExt::lookup_ipv4_ipv6`]. |
| 146 | +enum LookupIter<A, B> { |
| 147 | + Ipv4(A), |
| 148 | + Ipv6(B), |
| 149 | + Both(std::iter::Chain<A, B>), |
| 150 | +} |
| 151 | + |
| 152 | +impl<A: Iterator<Item = IpAddr>, B: Iterator<Item = IpAddr>> Iterator for LookupIter<A, B> { |
| 153 | + type Item = IpAddr; |
| 154 | + |
| 155 | + fn next(&mut self) -> Option<Self::Item> { |
| 156 | + match self { |
| 157 | + LookupIter::Ipv4(iter) => iter.next(), |
| 158 | + LookupIter::Ipv6(iter) => iter.next(), |
| 159 | + LookupIter::Both(iter) => iter.next(), |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Staggers calls to the future F with the given delays. |
| 165 | +/// |
| 166 | +/// The first call is performed immediately. The first call to succeed generates an Ok result |
| 167 | +/// ignoring any previous error. If all calls fail, an error summarizing all errors is returned. |
| 168 | +async fn stagger_call<T, F: Fn() -> Fut, Fut: Future<Output = Result<T>>>( |
| 169 | + f: F, |
| 170 | + delays_ms: &[u64], |
| 171 | +) -> Result<T> { |
| 172 | + let mut calls = futures_buffered::FuturesUnorderedBounded::new(delays_ms.len() + 1); |
| 173 | + // NOTE: we add the 0 delay here to have a uniform set of futures. This is more performant than |
| 174 | + // using alternatives that allow futures of different types. |
| 175 | + for delay in std::iter::once(&0u64).chain(delays_ms) { |
| 176 | + let delay = std::time::Duration::from_millis(*delay); |
| 177 | + let fut = f(); |
| 178 | + let staggered_fut = async move { |
| 179 | + tokio::time::sleep(delay).await; |
| 180 | + fut.await |
| 181 | + }; |
| 182 | + calls.push(staggered_fut) |
| 183 | + } |
| 184 | + |
| 185 | + let mut errors = vec![]; |
| 186 | + while let Some(call_result) = calls.next().await { |
| 187 | + match call_result { |
| 188 | + Ok(t) => return Ok(t), |
| 189 | + Err(e) => errors.push(e), |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + anyhow::bail!( |
| 194 | + "no calls succeed: [ {}]", |
| 195 | + errors.into_iter().fold(String::new(), |mut summary, e| { |
| 196 | + write!(summary, "{e} ").expect("infallible"); |
| 197 | + summary |
| 198 | + }) |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +#[cfg(test)] |
| 203 | +pub(crate) mod tests { |
| 204 | + use std::sync::atomic::AtomicUsize; |
| 205 | + use std::time::Duration; |
| 206 | + |
| 207 | + use super::*; |
| 208 | + const TIMEOUT: Duration = Duration::from_secs(5); |
| 209 | + const STAGGERING_DELAYS: &[u64] = &[200, 300]; |
| 210 | + |
| 211 | + #[tokio::test] |
| 212 | + async fn stagger_basic() { |
| 213 | + let _logging = iroh_test::logging::setup(); |
| 214 | + const CALL_RESULTS: &[Result<u8, u8>] = &[Err(2), Ok(3), Ok(5), Ok(7)]; |
| 215 | + static DONE_CALL: AtomicUsize = AtomicUsize::new(0); |
| 216 | + let f = || { |
| 217 | + let r_pos = DONE_CALL.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 218 | + async move { |
| 219 | + tracing::info!(r_pos, "call"); |
| 220 | + CALL_RESULTS[r_pos].map_err(|e| anyhow::anyhow!("{e}")) |
| 221 | + } |
| 222 | + }; |
| 223 | + |
| 224 | + let delays = [1000, 15]; |
| 225 | + let result = stagger_call(f, &delays).await.unwrap(); |
| 226 | + assert_eq!(result, 5) |
| 227 | + } |
| 228 | +} |
0 commit comments