Skip to content

Commit f016ce9

Browse files
authored
feat: allow to bypass DNS TTL entirely (#1031)
Allow `dns_ttl` to be set to 0, ignoring TTL from the name servers entirely. This wasn't possible previously because our DNS cache was implemented as a background loop, which would of DoS'ed nameservers.
1 parent 28ee184 commit f016ce9

4 files changed

Lines changed: 147 additions & 216 deletions

File tree

integration/pgdog.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ openmetrics_namespace = "pgdog_"
1313
prepared_statements_limit = 500
1414
prepared_statements = "extended"
1515
expanded_explain = true
16-
# dns_ttl = 15_000
16+
dns_ttl = 1_000
1717
query_cache_limit = 500
1818
pub_sub_channel_size = 4098
1919
two_phase_commit = false

pgdog/src/backend/pool/address.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ impl TryFrom<Url> for Address {
198198
mod test {
199199
use std::time::{Duration, SystemTime};
200200

201+
use crate::config;
202+
201203
use super::*;
202204

203205
// ── Address::new ─────────────────────────────────────────────────────────
@@ -479,4 +481,29 @@ mod test {
479481

480482
assert_eq!(secret, "stale-token");
481483
}
484+
485+
#[tokio::test]
486+
async fn test_addr_uses_dns_cache_when_dns_ttl_is_configured() {
487+
let cache = DnsCache::global();
488+
let hostname = "localhost";
489+
490+
let mut test_config = (*config::config()).clone();
491+
test_config.config.general.dns_ttl = Some(60_000);
492+
config::set(test_config).expect("set dns_ttl");
493+
cache.clear_cache_for_testing();
494+
495+
let addr = Address {
496+
host: hostname.into(),
497+
port: 15432,
498+
..Address::new_test()
499+
};
500+
501+
let socket_addr = addr.addr().await.expect("resolve address");
502+
503+
assert_eq!(socket_addr.port(), addr.port);
504+
assert_eq!(
505+
cache.cached_ip_for_testing(hostname),
506+
Some(socket_addr.ip())
507+
);
508+
}
482509
}

0 commit comments

Comments
 (0)