diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 6aa75289..30322d3d 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -20,6 +20,12 @@ changes go in their respective binding's NEXT_CHANGELOG. with an optional partner entry from `adbc.databricks.user_agent_entry` (`adbc.spark.user_agent_entry` is a compatibility alias). `User-Agent` in `adbc.databricks.http.headers` is now rejected; use the dedicated entry option. +- All HTTP clients in a process share one DNS lookup per host at a time and + reuse a successful answer for 30 s, so many sessions opening at once no + longer send one identical lookup each. A lookup that hasn't succeeded after + 1 s, or that failed, is tried again alongside (up to 3 attempts), and every + waiting session gets the first successful answer. A failure is reused for + 2 s. ### Updated diff --git a/src/client/dns.rs b/src/client/dns.rs new file mode 100644 index 00000000..5e73da86 --- /dev/null +++ b/src/client/dns.rs @@ -0,0 +1,643 @@ +// Copyright (c) 2026 Databricks, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Process-wide DNS cache shared by every kernel HTTP client. +//! +//! Each `Session` owns its own `reqwest::Client`, and reqwest's default +//! resolver runs one uncached `getaddrinfo` per new connection. When hundreds +//! of sessions open at once, the burst of identical lookups overflows local +//! stub resolvers (systemd-resolved drops the excess and glibc retries after +//! its 5 s timeout), so a share of the opens stall for 5 s or more. This +//! resolver runs one lookup per host at a time for the whole process and +//! reuses a successful answer for [`TTL`]. A lookup that hasn't succeeded +//! after [`HEDGE`], or whose attempt failed, gets another attempt, up to +//! [`MAX_ATTEMPTS`]. The first successful answer reaches every caller waiting +//! on it, so one dropped query doesn't hold the whole burst for glibc's retry. +//! An error reaches them only when every attempt failed, and is reused for +//! [`NEGATIVE_TTL`]. +//! +//! [`TTL`] is fixed: `getaddrinfo` doesn't report the record's own DNS TTL, +//! so that is ignored. The answer is also not invalidated when connecting to +//! it fails, so after a host moves to new addresses, new connections in this +//! process may try the old ones for up to [`TTL`] (the JVM's default caches +//! the same way). + +use std::collections::HashMap; +use std::io; +use std::net::{SocketAddr, ToSocketAddrs}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex, OnceLock}; +use std::time::{Duration, Instant}; + +use reqwest::dns::{Addrs, Name, Resolve, Resolving}; +use tokio::sync::watch; + +/// How long a successful answer is reused. Matches the JVM's default +/// positive-lookup TTL, so the JDBC driver's behaviour is the reference. +const TTL: Duration = Duration::from_secs(30); +/// How long a lookup goes unanswered before another attempt starts; well +/// under glibc's 5 s retry timeout. +const HEDGE: Duration = Duration::from_secs(1); +/// Attempts per lookup, started [`HEDGE`] apart while none has succeeded +/// (or at once when one fails). +const MAX_ATTEMPTS: usize = 3; +/// How long a failed lookup is reused, so retries from every session against +/// a host that doesn't resolve make one lookup, not one each (the JVM's +/// default negative TTL is 10 s). +const NEGATIVE_TTL: Duration = Duration::from_secs(2); +/// An unanswered lookup older than this is replaced; a backstop for a lookup +/// that never returns. (One inherited across `fork()` is replaced at once, see +/// [`Slot::pid`].) +const STUCK_AFTER: Duration = Duration::from_secs(60); +/// Bound on distinct cached hosts (workspace + CloudFetch storage hosts). +const MAX_HOSTS: usize = 256; + +type Lookup = Arc io::Result> + Send + Sync>; +type Answer = (Result, Arc>, Instant); +/// `None` while the lookup thread runs; the thread publishes the answer. +type Entry = watch::Receiver>; + +/// A host's latest lookup. +struct Slot { + entry: Entry, + started: Instant, + /// The process that started it. A child after `fork()` has the entry but + /// not the lookup threads that would answer it. + pid: u32, +} + +pub(crate) struct CachingResolver { + entries: Mutex>, + ttl: Duration, + negative_ttl: Duration, + stuck_after: Duration, + hedge: Duration, + lookup: Lookup, + /// Rotates the first address handed out, as round-robin DNS would. + next: AtomicUsize, +} + +impl CachingResolver { + /// The resolver every kernel HTTP client in this process uses. + pub(crate) fn shared() -> Arc { + static SHARED: OnceLock> = OnceLock::new(); + SHARED + .get_or_init(|| Arc::new(Self::new(TTL, HEDGE, Arc::new(system_lookup)))) + .clone() + } + + fn new(ttl: Duration, hedge: Duration, lookup: Lookup) -> Self { + Self { + entries: Mutex::new(HashMap::new()), + ttl, + negative_ttl: NEGATIVE_TTL, + stuck_after: STUCK_AFTER, + hedge, + lookup, + next: AtomicUsize::new(0), + } + } + + /// The lookup to join for `host`: one in flight, a recent answer, or a new + /// lookup when the last one expired. + fn entry(&self, host: &str) -> Entry { + let (tx, rx) = { + let mut entries = self.entries.lock().unwrap_or_else(|e| e.into_inner()); + match self.current_or_insert(&mut entries, host) { + Ok(current) => return current, + Err(new) => new, + } + }; + // Spawned after the lock is released: every resolve takes that lock. + self.start_lookup(host, tx); + rx + } + + /// The usable entry for `host`, or the sender and receiver of a new one + /// just inserted for it. + fn current_or_insert( + &self, + entries: &mut HashMap, + host: &str, + ) -> Result>, Entry)> { + let usable = |slot: &Slot| match &*slot.entry.borrow() { + // An unanswered entry is joined only while something can still + // answer it. + None => { + slot.entry.has_changed().is_ok() + && slot.pid == std::process::id() + && slot.started.elapsed() < self.stuck_after + } + Some((Ok(_), at)) => at.elapsed() < self.ttl, + Some((Err(_), at)) => at.elapsed() < self.negative_ttl, + }; + if let Some(current) = entries.get(host) { + if usable(current) { + return Ok(current.entry.clone()); + } + } + if entries.len() >= MAX_HOSTS { + entries.retain(|_, current| usable(current)); + if entries.len() >= MAX_HOSTS { + entries.clear(); + } + } + let (tx, rx) = watch::channel(None); + let slot = Slot { + entry: rx.clone(), + started: Instant::now(), + pid: std::process::id(), + }; + entries.insert(host.to_owned(), slot); + Err((tx, rx)) + } + + /// Whether `host` has a cached successful answer (tests only). + #[cfg(test)] + pub(crate) fn has_answer(&self, host: &str) -> bool { + let entries = self.entries.lock().unwrap_or_else(|e| e.into_inner()); + entries + .get(host) + .is_some_and(|slot| matches!(&*slot.entry.borrow(), Some((Ok(_), _)))) + } + + /// Run the lookup off the caller's runtime on a thread that publishes the + /// answer itself: waiters from other sessions depend on it, so it must not + /// be abandoned when the caller that started it is cancelled or its + /// runtime shuts down. See [`first_answer`] for how attempts are made. + fn start_lookup(&self, host: &str, tx: watch::Sender>) { + let lookup = Arc::clone(&self.lookup); + let (hedge, deadline) = (self.hedge, self.stuck_after); + let host = host.to_owned(); + let tx = Arc::new(tx); + let thread_tx = Arc::clone(&tx); + let spawned = std::thread::Builder::new() + .name("dbx-sql-kernel-dns".to_owned()) + .spawn(move || { + let result = first_answer(&lookup, &host, hedge, deadline); + thread_tx.send_replace(Some(( + result.map(Arc::from).map_err(Arc::new), + Instant::now(), + ))); + }); + if let Err(e) = spawned { + tx.send_replace(Some((Err(Arc::new(e)), Instant::now()))); + } + } +} + +/// The first successful answer from up to [`MAX_ATTEMPTS`] attempts at +/// `lookup(host)`, each on its own thread. Another attempt starts every +/// `hedge` while none has succeeded, and at once when one fails: a fast +/// failure (`EAI_AGAIN` from an overloaded stub) must not fail every waiter +/// while another attempt may still succeed. The last error is returned only +/// once every attempt has failed, and `TimedOut` if none has answered by +/// `deadline`, so a resolver that hangs still settles the lookup. +fn first_answer( + lookup: &Lookup, + host: &str, + hedge: Duration, + deadline: Duration, +) -> io::Result> { + let started = Instant::now(); + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let (mut attempts, mut running) = (0, 0); + let mut last_error = None; + // Each pass starts an attempt while under the cap: the first, then one + // per `hedge` without a success, or one right after a failure. + loop { + if attempts < MAX_ATTEMPTS { + attempts += 1; + let (sender, lookup, host) = (done_tx.clone(), Arc::clone(lookup), host.to_owned()); + let spawned = std::thread::Builder::new() + .name("dbx-sql-kernel-dns".to_owned()) + .spawn(move || { + // Always report back, so the attempt is never waited on forever. + let result = + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| lookup(&host))) + .unwrap_or_else(|_| Err(io::Error::other("DNS lookup panicked"))); + let _ = sender.send(result); + }); + match spawned { + Ok(_) => running += 1, + Err(e) => last_error = Some(e), + } + } + if running == 0 { + if attempts < MAX_ATTEMPTS { + continue; + } + return Err(last_error.unwrap_or_else(|| io::Error::other("DNS lookup failed"))); + } + let Some(left) = deadline.checked_sub(started.elapsed()) else { + return Err(io::Error::new( + io::ErrorKind::TimedOut, + format!("DNS lookup for {host} timed out after {deadline:?}"), + )); + }; + match done_rx.recv_timeout(hedge.min(left)) { + Ok(Ok(addrs)) if !addrs.is_empty() => return Ok(addrs), + Ok(result) => { + // Retried at once whatever the error: std reports every + // getaddrinfo failure as the same kind with gai_strerror's + // localized text, so a final NXDOMAIN can't be told from a + // transient EAI_AGAIN. A host that doesn't resolve costs + // MAX_ATTEMPTS queries per NEGATIVE_TTL rather than one. + running -= 1; + last_error = Some(result.err().unwrap_or_else(|| { + io::Error::new(io::ErrorKind::NotFound, "DNS lookup returned no addresses") + })); + } + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {} + // The coordinator holds a sender, so the channel can't close. + Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => unreachable!(), + } + } +} + +impl Resolve for CachingResolver { + fn resolve(&self, name: Name) -> Resolving { + let mut entry = self.entry(name.as_str()); + let start = self.next.fetch_add(1, Ordering::Relaxed); + Box::pin(async move { + let result = match entry.wait_for(Option::is_some).await { + Ok(answer) => answer.as_ref().map(|(result, _)| result.clone()), + Err(_) => None, + }; + match result { + Some(Ok(addrs)) => Ok(Box::new(rotated(addrs, start)) as Addrs), + Some(Err(e)) => Err(io::Error::new(e.kind(), e.to_string()).into()), + None => Err(io::Error::other("DNS lookup thread exited").into()), + } + }) + } +} + +/// `addrs` starting at a different address per call. Only the leading run +/// of one address family rotates, so the family the connector tries first +/// (happy eyeballs) stays the resolver's choice. +fn rotated(addrs: Arc<[SocketAddr]>, start: usize) -> impl Iterator + Send { + let lead = addrs + .iter() + .take_while(|a| a.is_ipv4() == addrs[0].is_ipv4()) + .count(); + let offset = start % lead.max(1); + (0..addrs.len()).map(move |i| { + if i < lead { + addrs[(i + offset) % lead] + } else { + addrs[i] + } + }) +} + +fn system_lookup(host: &str) -> io::Result> { + Ok((host, 0).to_socket_addrs()?.collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::str::FromStr; + use std::sync::atomic::{AtomicUsize, Ordering}; + + fn counting(delay: Duration, fail: bool) -> (Lookup, Arc) { + let calls = Arc::new(AtomicUsize::new(0)); + let c = Arc::clone(&calls); + let lookup: Lookup = Arc::new(move |_host| { + c.fetch_add(1, Ordering::SeqCst); + std::thread::sleep(delay); + if fail { + Err(io::Error::other("boom")) + } else { + Ok(vec!["10.0.0.1:0".parse().unwrap()]) + } + }); + (lookup, calls) + } + + async fn resolve(r: &Arc, host: &str) -> Result, String> { + r.resolve(Name::from_str(host).unwrap()) + .await + .map(|a| a.collect()) + .map_err(|e| e.to_string()) + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn concurrent_lookups_for_one_host_share_one_query() { + let (lookup, calls) = counting(Duration::from_millis(50), false); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let tasks: Vec<_> = (0..64) + .map(|_| { + let r = Arc::clone(&r); + tokio::spawn(async move { resolve(&r, "example.com").await }) + }) + .collect(); + for t in tasks { + assert_eq!( + t.await.unwrap().unwrap(), + vec!["10.0.0.1:0".parse().unwrap()] + ); + } + assert_eq!(calls.load(Ordering::SeqCst), 1); + resolve(&r, "example.com").await.unwrap(); + assert_eq!(calls.load(Ordering::SeqCst), 1, "fresh answer is reused"); + resolve(&r, "other.example.com").await.unwrap(); + assert_eq!( + calls.load(Ordering::SeqCst), + 2, + "hosts are cached separately" + ); + } + + #[tokio::test] + async fn expired_answers_are_looked_up_again() { + let (lookup, calls) = counting(Duration::ZERO, false); + let r = Arc::new(CachingResolver::new( + Duration::from_millis(20), + HEDGE, + lookup, + )); + resolve(&r, "example.com").await.unwrap(); + tokio::time::sleep(Duration::from_millis(40)).await; + resolve(&r, "example.com").await.unwrap(); + assert_eq!(calls.load(Ordering::SeqCst), 2); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn failures_reach_current_waiters_and_are_reused() { + let (lookup, calls) = counting(Duration::from_millis(50), true); + let mut r = CachingResolver::new(TTL, HEDGE, lookup); + r.negative_ttl = Duration::from_secs(60); + let r = Arc::new(r); + let tasks: Vec<_> = (0..16) + .map(|_| { + let r = Arc::clone(&r); + tokio::spawn(async move { resolve(&r, "example.com").await }) + }) + .collect(); + for t in tasks { + assert!(t.await.unwrap().unwrap_err().contains("boom")); + } + assert_eq!( + calls.load(Ordering::SeqCst), + MAX_ATTEMPTS, + "every attempt failed" + ); + assert!(resolve(&r, "example.com").await.is_err()); + assert_eq!( + calls.load(Ordering::SeqCst), + MAX_ATTEMPTS, + "a recent failure is reused" + ); + } + + #[tokio::test] + async fn expired_failures_are_looked_up_again() { + let (lookup, calls) = counting(Duration::ZERO, true); + let mut r = CachingResolver::new(TTL, HEDGE, lookup); + r.negative_ttl = Duration::ZERO; + let r = Arc::new(r); + assert!(resolve(&r, "example.com").await.is_err()); + assert!(resolve(&r, "example.com").await.is_err()); + assert_eq!(calls.load(Ordering::SeqCst), 2 * MAX_ATTEMPTS); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn a_fast_failure_does_not_beat_an_attempt_that_succeeds() { + // The first attempt fails at once (EAI_AGAIN from an overloaded + // stub); the next one answers, and that is what every waiter gets. + let calls = Arc::new(AtomicUsize::new(0)); + let c = Arc::clone(&calls); + let lookup: Lookup = Arc::new(move |_| { + if c.fetch_add(1, Ordering::SeqCst) == 0 { + Err(io::Error::other("Temporary failure in name resolution")) + } else { + Ok(vec!["10.0.0.1:0".parse().unwrap()]) + } + }); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let tasks: Vec<_> = (0..8) + .map(|_| { + let r = Arc::clone(&r); + tokio::spawn(async move { resolve(&r, "example.com").await }) + }) + .collect(); + for t in tasks { + assert_eq!( + t.await.unwrap().unwrap(), + vec!["10.0.0.1:0".parse().unwrap()] + ); + } + // A slow scheduler may let another hedge start before the answer arrives. + assert!((2..=MAX_ATTEMPTS).contains(&calls.load(Ordering::SeqCst))); + } + + /// An unanswered entry nothing will answer: its sender is kept alive, as + /// in a child after `fork()`, where the lookup threads don't exist. + fn insert_orphan( + r: &CachingResolver, + started: Instant, + pid: u32, + ) -> watch::Sender> { + let (orphan_tx, orphan_rx) = watch::channel(None); + let slot = Slot { + entry: orphan_rx, + started, + pid, + }; + r.entries + .lock() + .unwrap() + .insert("example.com".to_owned(), slot); + orphan_tx + } + + async fn resolves_with_one_new_lookup(r: &Arc, calls: &AtomicUsize) { + let addrs = tokio::time::timeout(Duration::from_secs(10), resolve(r, "example.com")) + .await + .expect("waited on the orphaned entry") + .unwrap(); + assert_eq!(addrs, vec!["10.0.0.1:0".parse().unwrap()]); + assert_eq!(calls.load(Ordering::SeqCst), 1); + } + + #[tokio::test] + async fn an_entry_from_another_process_is_replaced_at_once() { + let (lookup, calls) = counting(Duration::ZERO, false); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let parent = std::process::id().wrapping_add(1); + let _orphan = insert_orphan(&r, Instant::now(), parent); + resolves_with_one_new_lookup(&r, &calls).await; + } + + #[tokio::test] + async fn an_unanswered_entry_past_its_deadline_is_replaced() { + let (lookup, calls) = counting(Duration::ZERO, false); + let mut r = CachingResolver::new(TTL, HEDGE, lookup); + r.stuck_after = Duration::from_millis(20); + let r = Arc::new(r); + let _orphan = insert_orphan(&r, Instant::now(), std::process::id()); + tokio::time::sleep(Duration::from_millis(40)).await; + resolves_with_one_new_lookup(&r, &calls).await; + } + + #[tokio::test] + async fn empty_answers_are_errors() { + let lookup: Lookup = Arc::new(|_| Ok(Vec::new())); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + assert!(resolve(&r, "example.com").await.is_err()); + } + + #[test] + fn lookup_survives_the_runtime_that_started_it() { + let (lookup, calls) = counting(Duration::from_millis(100), false); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let first = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .enable_all() + .build() + .unwrap(); + let r1 = Arc::clone(&r); + first.spawn(async move { resolve(&r1, "example.com").await }); + // Shut down only once that task's lookup is running. + let deadline = Instant::now() + Duration::from_secs(10); + while calls.load(Ordering::SeqCst) == 0 { + assert!(Instant::now() < deadline, "the lookup never started"); + std::thread::sleep(Duration::from_millis(1)); + } + first.shutdown_background(); + + let second = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + assert!(second.block_on(resolve(&r, "example.com")).is_ok()); + assert_eq!(calls.load(Ordering::SeqCst), 1); + } + + #[tokio::test] + async fn lookup_survives_a_cancelled_caller() { + let (lookup, calls) = counting(Duration::from_millis(100), false); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let timed_out = + tokio::time::timeout(Duration::from_millis(10), resolve(&r, "example.com")).await; + assert!(timed_out.is_err(), "first caller is cancelled mid-lookup"); + resolve(&r, "example.com").await.unwrap(); + assert_eq!(calls.load(Ordering::SeqCst), 1); + } + + #[tokio::test] + async fn each_call_starts_at_the_next_address_of_the_leading_family() { + let lookup: Lookup = Arc::new(|_| { + Ok(["10.0.0.1:0", "10.0.0.2:0", "10.0.0.3:0", "[::1]:0"] + .iter() + .map(|a| a.parse().unwrap()) + .collect()) + }); + let r = Arc::new(CachingResolver::new(TTL, HEDGE, lookup)); + let mut firsts = std::collections::HashSet::new(); + for _ in 0..3 { + let addrs = resolve(&r, "example.com").await.unwrap(); + assert_eq!(addrs.len(), 4); + assert!(addrs[..3].iter().all(SocketAddr::is_ipv4)); + assert_eq!(addrs[3], "[::1]:0".parse().unwrap()); + firsts.insert(addrs[0]); + } + assert_eq!(firsts.len(), 3); + } + + type Release = Arc<(Mutex, std::sync::Condvar)>; + + /// A lookup whose first `stalled` attempts block until released, like a + /// dropped query waiting for glibc's retry; later attempts answer at once. + fn stalling(stalled: usize) -> (Lookup, Arc, Release) { + let calls = Arc::new(AtomicUsize::new(0)); + let release: Release = Arc::new((Mutex::new(false), std::sync::Condvar::new())); + let (c, rel) = (Arc::clone(&calls), Arc::clone(&release)); + let lookup: Lookup = Arc::new(move |_| { + if c.fetch_add(1, Ordering::SeqCst) < stalled { + let (released, wake) = &*rel; + let _released = wake + .wait_while(released.lock().unwrap(), |done| !*done) + .unwrap(); + } + Ok(vec!["10.0.0.1:0".parse().unwrap()]) + }); + (lookup, calls, release) + } + + fn release(stall: &Release) { + *stall.0.lock().unwrap() = true; + stall.1.notify_all(); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn callers_waiting_on_a_stalled_lookup_get_the_hedged_answer() { + // The first query never answers on its own; everyone already waiting + // on it is answered by the second attempt. + let (lookup, calls, stall) = stalling(1); + let r = Arc::new(CachingResolver::new(TTL, Duration::from_millis(50), lookup)); + let waiters: Vec<_> = (0..8) + .map(|_| { + let r = Arc::clone(&r); + tokio::spawn(async move { resolve(&r, "example.com").await }) + }) + .collect(); + for waiter in waiters { + let addrs = tokio::time::timeout(Duration::from_secs(10), waiter) + .await + .expect("held by the stalled query") + .unwrap() + .unwrap(); + assert_eq!(addrs, vec!["10.0.0.1:0".parse().unwrap()]); + } + // A slow scheduler may let another hedge start before the answer arrives. + assert!((2..=MAX_ATTEMPTS).contains(&calls.load(Ordering::SeqCst))); + release(&stall); + } + + #[tokio::test] + async fn a_lookup_whose_attempts_all_hang_times_out() { + let (lookup, calls, stall) = stalling(usize::MAX); + let mut r = CachingResolver::new(TTL, Duration::from_millis(10), lookup); + r.stuck_after = Duration::from_millis(100); + let r = Arc::new(r); + let err = tokio::time::timeout(Duration::from_secs(10), resolve(&r, "example.com")) + .await + .expect("the lookup never settled") + .unwrap_err(); + assert!(err.contains("timed out"), "{err}"); + assert_eq!(calls.load(Ordering::SeqCst), MAX_ATTEMPTS); + release(&stall); + } + + #[tokio::test] + async fn a_lookup_makes_at_most_max_attempts() { + let (lookup, calls, stall) = stalling(usize::MAX); + let r = Arc::new(CachingResolver::new(TTL, Duration::from_millis(10), lookup)); + let r1 = Arc::clone(&r); + let pending = tokio::spawn(async move { resolve(&r1, "example.com").await }); + let deadline = Instant::now() + Duration::from_secs(10); + while calls.load(Ordering::SeqCst) < MAX_ATTEMPTS { + assert!(Instant::now() < deadline, "attempts didn't start"); + tokio::time::sleep(Duration::from_millis(5)).await; + } + // Ten more hedge intervals pass without another attempt. + tokio::time::sleep(Duration::from_millis(100)).await; + assert_eq!(calls.load(Ordering::SeqCst), MAX_ATTEMPTS); + release(&stall); + pending.await.unwrap().unwrap(); + } +} diff --git a/src/client/http.rs b/src/client/http.rs index e5f9ad0b..8545e0bc 100644 --- a/src/client/http.rs +++ b/src/client/http.rs @@ -522,7 +522,8 @@ impl DatabricksHttpClient { .timeout(config.read_timeout) .pool_max_idle_per_host(config.max_connections_per_host) .user_agent(&config.user_agent) - .redirect(redirect); + .redirect(redirect) + .dns_resolver(crate::client::dns::CachingResolver::shared()); // Apply TLS configuration if config.tls.enabled == Some(false) { @@ -1104,6 +1105,26 @@ mod tests { assert!(client.is_ok()); } + #[tokio::test] + async fn clients_resolve_hosts_through_the_shared_dns_cache() { + use wiremock::matchers::any; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let server = MockServer::start().await; + Mock::given(any()) + .respond_with(ResponseTemplate::new(200)) + .mount(&server) + .await; + let mut config = HttpClientConfig::default(); + config.tls.enabled = Some(false); + let client = DatabricksHttpClient::new(config, default_retry_configs()).unwrap(); + // A name, not the mock's IP literal, so a resolver is consulted. + let url = format!("http://localhost:{}/", server.address().port()); + client.inner().get(url).send().await.expect("request"); + // Only clients built here write to the shared cache. + assert!(crate::client::dns::CachingResolver::shared().has_answer("localhost")); + } + #[tokio::test] async fn test_auth_header_after_set() { let config = HttpClientConfig::default(); diff --git a/src/client/mod.rs b/src/client/mod.rs index af553abf..3b972d5e 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -31,6 +31,7 @@ //! [`transport`]: self::transport //! [`sea`]: self::sea +pub(crate) mod dns; pub mod http; pub mod retry; pub mod sea;