From 7f3e8f97964a9015691ea3b143676837de8df4a2 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 22 Jul 2024 11:21:04 +0000 Subject: [PATCH] feat: promote fallback DNS results to cached on successful use For hardcoded built-in DNS results there is no cache entry in `dns_cache` table so they cannot be prioritized if DNS resolution never returned these results yet. If there is no entry, a new one should be created. SQL UPSERT does this. --- src/net.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/net.rs b/src/net.rs index cbdb60b9c..89e6f0bd2 100644 --- a/src/net.rs +++ b/src/net.rs @@ -232,14 +232,25 @@ pub(crate) async fn connect_tcp( Ok(stream) => { tcp_stream = Some(stream); - // Maximize priority of this cached entry. + // Update timestamp of this cached entry + // or insert a new one if cached entry does not exist. + // + // This increases priority of existing cached entries + // and copies fallback addresses from build-in cache + // into database cache on successful use. + // + // Unlike built-in cache, + // database cache is used even if DNS + // resolver returns a non-empty + // (but potentially incorrect and unusable) result. context .sql .execute( - "UPDATE dns_cache - SET timestamp = ? - WHERE address = ?", - (time(), resolved_addr.ip().to_string()), + "INSERT INTO dns_cache (hostname, address, timestamp) + VALUES (?, ?, ?) + ON CONFLICT (hostname, address) + DO UPDATE SET timestamp=excluded.timestamp", + (host, resolved_addr.ip().to_string(), time()), ) .await?; break;