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.
This commit is contained in:
link2xt
2024-07-22 11:21:04 +00:00
parent 837311abce
commit 7f3e8f9796

View File

@@ -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;