Skip to content

Commit

Permalink
Add dns_cache so server addresses are cached and invalidated when DNS…
Browse files Browse the repository at this point in the history
… changes.

Adds a module to deal with dns_cache feature. It's
main struct is CachedResolver, which is a simple thread safe
hostname <-> Ips cache with the ability to refresh resolutions
every `dns_max_ttl` seconds. This way, a client can check whether its
ip address has changed.
  • Loading branch information
magec committed Feb 20, 2023
1 parent 28f2d19 commit 131db6d
Show file tree
Hide file tree
Showing 10 changed files with 705 additions and 3 deletions.
288 changes: 288 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ phf = { version = "0.11.1", features = ["macros"] }
exitcode = "1.1.2"
futures = "0.3"
socket2 = { version = "0.4.7", features = ["all"] }
trust-dns-resolver = "0.22"
tokio-test = "0.4.2"

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.5.0"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ The config can be reloaded by sending a `kill -s SIGHUP` to the process or by qu
| `default_role` | no |
| `primary_reads_enabled` | no |
| `query_parser_enabled` | no |
| `dns_max_ttl` | no |
| `dns_cache_enabled` | no |


## Benchmarks
Expand Down
9 changes: 9 additions & 0 deletions examples/docker/pgcat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ log_client_disconnections = false
# Reload config automatically if it changes.
autoreload = false

# If enabled, hostname resolution will be cached and
# and server connections will be invalidated if a change on the ip is
# detected. This check is done every `dns_max_ttl` seconds.
# dns_cache_enabled = false

# The number of seconds to wait until we check again the
# cached hostnames resolution. 30 seconds by default.
# dns_max_ttl = 30

# TLS
# tls_certificate = "server.cert"
# tls_private_key = "server.key"
Expand Down
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ pub struct General {
#[serde(default)] // False
pub log_client_disconnections: bool,

#[serde(default)] // False
pub dns_cache_enabled: bool,

#[serde(default = "General::default_dns_max_ttl")]
pub dns_max_ttl: u64,

#[serde(default = "General::default_shutdown_timeout")]
pub shutdown_timeout: u64,

Expand Down Expand Up @@ -234,6 +240,10 @@ impl General {
60000
}

pub fn default_dns_max_ttl() -> u64 {
30
}

pub fn default_healthcheck_timeout() -> u64 {
1000
}
Expand Down Expand Up @@ -270,6 +280,8 @@ impl Default for General {
tcp_keepalives_interval: Self::default_tcp_keepalives_interval(),
log_client_connections: false,
log_client_disconnections: false,
dns_cache_enabled: false,
dns_max_ttl: Self::default_dns_max_ttl(),
autoreload: false,
tls_certificate: None,
tls_private_key: None,
Expand Down
Loading

0 comments on commit 131db6d

Please sign in to comment.