Add much better debug logging of address resolution

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2023-08-25 22:54:41 +02:00
parent 483c058105
commit 40b890bc13

View File

@ -51,20 +51,24 @@ impl UpstreamAddress {
pub async fn resolve(&mut self, mode: ResolutionMode) -> Result<Vec<SocketAddr>> { pub async fn resolve(&mut self, mode: ResolutionMode) -> Result<Vec<SocketAddr>> {
if self.is_resolved() && self.is_valid() { if self.is_resolved() && self.is_valid() {
debug!( debug!(
"Already got address {:?}, still valid for {}", "Already got address {:?}, still valid for {:.3}s",
&self.resolved_addresses, &self.resolved_addresses,
self.time_remaining() self.time_remaining().as_seconds_f64()
); );
return Ok(self.resolved_addresses.clone()); return Ok(self.resolved_addresses.clone());
} }
debug!("Resolving addresses for {}", &self.address); debug!(
"Resolving addresses for {} with mode {:?}",
&self.address, &mode
);
let lookup_result = tokio::net::lookup_host(&self.address).await; let lookup_result = tokio::net::lookup_host(&self.address).await;
let resolved_addresses = match lookup_result { let resolved_addresses: Vec<SocketAddr> = match lookup_result {
Ok(resolved_addresses) => resolved_addresses, Ok(resolved_addresses) => resolved_addresses.into_iter().collect(),
Err(e) => { Err(e) => {
debug!("Failed looking up {}: {}", &self.address, &e);
// Protect against DNS flooding. Cache the result for 1 second. // Protect against DNS flooding. Cache the result for 1 second.
self.resolved_time = Some(Instant::now()); self.resolved_time = Some(Instant::now());
self.ttl = Some(Duration::seconds(3)); self.ttl = Some(Duration::seconds(3));
@ -72,6 +76,8 @@ impl UpstreamAddress {
} }
}; };
debug!("Resolved addresses: {:?}", &resolved_addresses);
let addresses: Vec<SocketAddr> = match mode { let addresses: Vec<SocketAddr> = match mode {
ResolutionMode::Ipv4 => resolved_addresses ResolutionMode::Ipv4 => resolved_addresses
.into_iter() .into_iter()
@ -83,10 +89,13 @@ impl UpstreamAddress {
.filter(|a| a.is_ipv6()) .filter(|a| a.is_ipv6())
.collect(), .collect(),
_ => resolved_addresses.collect(), _ => resolved_addresses,
}; };
debug!("Got addresses for {}: {:?}", &self.address, &addresses); debug!(
"Got {} addresses for {}: {:?}",
&mode, &self.address, &addresses
);
debug!( debug!(
"Resolved at {}", "Resolved at {}",
OffsetDateTime::now_utc() OffsetDateTime::now_utc()
@ -120,3 +129,13 @@ impl From<&str> for ResolutionMode {
} }
} }
} }
impl Display for ResolutionMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ResolutionMode::Ipv4 => write!(f, "IPv4Only"),
ResolutionMode::Ipv6 => write!(f, "IPv6Only"),
ResolutionMode::Ipv4AndIpv6 => write!(f, "IPv4 and IPv6"),
}
}
}