[edge] Resolver, tests first: hung name lookups pin at most a bounded number of their own threads, concurrent lookups of one name share one, and the rest fail at once (red: one blocking-pool task per request, as before)
This commit is contained in:
@@ -235,54 +235,15 @@ pub const RESOLVE_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
impl UpstreamRt {
|
||||
/// Addresses to try, in resolver order. Names are resolved at connect time (like Caddy's
|
||||
/// dialer), so DNS changes and unresolvable names only affect requests, not startup.
|
||||
/// A lookup that takes longer than [`RESOLVE_TIMEOUT`] is an error.
|
||||
/// A lookup that takes longer than [`RESOLVE_TIMEOUT`] is an error; lookups run on their
|
||||
/// own bounded threads ([`crate::resolver`]).
|
||||
pub async fn resolve(&self) -> std::io::Result<Vec<SocketAddr>> {
|
||||
resolve_with(
|
||||
&self.host,
|
||||
self.port,
|
||||
RESOLVE_TIMEOUT,
|
||||
|host, port| async move {
|
||||
tokio::net::lookup_host((host.as_str(), port))
|
||||
.await
|
||||
.map(Iterator::collect)
|
||||
},
|
||||
)
|
||||
.await
|
||||
crate::resolver::system()
|
||||
.resolve(&self.host, self.port, RESOLVE_TIMEOUT)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// [`UpstreamRt::resolve`] with the lookup passed in, so the deadline can be tested.
|
||||
pub async fn resolve_with<F, Fut>(
|
||||
host: &str,
|
||||
port: u16,
|
||||
deadline: Duration,
|
||||
lookup: F,
|
||||
) -> std::io::Result<Vec<SocketAddr>>
|
||||
where
|
||||
F: FnOnce(String, u16) -> Fut,
|
||||
Fut: Future<Output = std::io::Result<Vec<SocketAddr>>>,
|
||||
{
|
||||
if let Ok(ip) = host.parse::<std::net::IpAddr>() {
|
||||
return Ok(vec![SocketAddr::new(ip, port)]);
|
||||
}
|
||||
let addrs = match tokio::time::timeout(deadline, lookup(host.to_owned(), port)).await {
|
||||
Ok(r) => r?,
|
||||
Err(_) => {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::TimedOut,
|
||||
format!("name lookup took longer than {deadline:?}"),
|
||||
));
|
||||
}
|
||||
};
|
||||
if addrs.is_empty() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
"name resolves to no address",
|
||||
));
|
||||
}
|
||||
Ok(addrs)
|
||||
}
|
||||
|
||||
fn header_name(name: &str) -> Result<HeaderName, EdgeError> {
|
||||
HeaderName::from_bytes(name.as_bytes()).map_err(|_| EdgeError::Header {
|
||||
name: name.to_owned(),
|
||||
@@ -498,46 +459,10 @@ pub fn build(cfg: EdgeConfig, credentials_dir: Option<&Path>) -> Result<Runtime,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod resolve_tests {
|
||||
mod health_tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn a_slow_resolver_times_out() {
|
||||
let t = tokio::time::Instant::now();
|
||||
let err = resolve_with("slow.test", 80, RESOLVE_TIMEOUT, |_, _| async {
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
Ok(vec!["127.0.0.1:80".parse().unwrap()])
|
||||
})
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::TimedOut);
|
||||
assert_eq!(t.elapsed(), RESOLVE_TIMEOUT);
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn a_prompt_resolver_and_ip_literals_pass() {
|
||||
let a: SocketAddr = "127.0.0.1:80".parse().unwrap();
|
||||
let got = resolve_with(
|
||||
"ok.test",
|
||||
80,
|
||||
RESOLVE_TIMEOUT,
|
||||
|_, _| async move { Ok(vec![a]) },
|
||||
)
|
||||
.await;
|
||||
assert_eq!(got.unwrap(), vec![a]);
|
||||
let got = resolve_with("::1", 8080, RESOLVE_TIMEOUT, |_, _| async {
|
||||
unreachable!("IP literals are not looked up")
|
||||
})
|
||||
.await;
|
||||
assert_eq!(got.unwrap(), vec!["[::1]:8080".parse().unwrap()]);
|
||||
let got = resolve_with("empty.test", 80, RESOLVE_TIMEOUT, |_, _| async {
|
||||
Ok(vec![])
|
||||
})
|
||||
.await;
|
||||
assert_eq!(got.unwrap_err().kind(), std::io::ErrorKind::NotFound);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn health_records_keep_the_last_five() {
|
||||
let r = HealthRecord::default();
|
||||
|
||||
Reference in New Issue
Block a user