Skip to content

Commit 28ee184

Browse files
authored
Allow disabling idle healthchecks (#1030)
Close #1029 Treat `idle_healthcheck_interval = 0` as disabled so deployments can avoid background idle healthchecks when they are not useful. This only disables background idle healthchecks. Checkout-time healthchecks controlled by `healthcheck_interval` should keep their current behavior, with `healthcheck_interval = 0` meaning “check every checkout.”
1 parent 75335fb commit 28ee184

5 files changed

Lines changed: 48 additions & 4 deletions

File tree

.schema/pgdog.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@
785785
"minimum": 0
786786
},
787787
"idle_healthcheck_interval": {
788-
"description": "Frequency of healthchecks performed by PgDog on idle connections.\n\n_Default:_ `30000`\n\nhttps://docs.pgdog.dev/configuration/pgdog.toml/general/#idle_healthcheck_interval",
788+
"description": "Frequency of healthchecks performed by PgDog on idle connections.\nSet to `0` to disable idle healthchecks.\n\n_Default:_ `30000`\n\nhttps://docs.pgdog.dev/configuration/pgdog.toml/general/#idle_healthcheck_interval",
789789
"type": "integer",
790790
"format": "uint64",
791791
"default": 30000,

example.pgdog.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pooler_mode = "transaction"
4949
healthcheck_interval = 30_000
5050
# How often to check databases with a health check. This happens independently from clients
5151
# and runs on a separate loop. This is helpful if databases aren't frequently used.
52+
# Set to 0 to disable idle health checks.
5253
#
5354
# Default: 30 seconds
5455
idle_healthcheck_interval = 30_000

pgdog-config/src/general.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub struct General {
126126
pub healthcheck_interval: u64,
127127

128128
/// Frequency of healthchecks performed by PgDog on idle connections.
129+
/// Set to `0` to disable idle healthchecks.
129130
///
130131
/// _Default:_ `30000`
131132
///

pgdog/src/backend/pool/monitor.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ impl Monitor {
9191
// Delay starting health checks to give
9292
// time for the pool to spin up.
9393
let pool = self.pool.clone();
94-
let (delay, replication_mode) = {
94+
let (delay, interval, replication_mode) = {
9595
let lock = pool.lock();
9696
let config = lock.config();
97-
(config.idle_healthcheck_delay(), config.replication_mode)
97+
(
98+
config.idle_healthcheck_delay(),
99+
config.idle_healthcheck_interval(),
100+
config.replication_mode,
101+
)
98102
};
99103

100-
if !replication_mode {
104+
if !replication_mode && interval > Duration::ZERO {
101105
spawn(async move {
102106
sleep(delay).await;
103107
Self::healthchecks(pool).await

pgdog/src/backend/pool/test/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,44 @@ async fn test_idle_healthcheck_loop() {
530530
);
531531
}
532532

533+
#[tokio::test]
534+
async fn test_idle_healthcheck_loop_disabled_with_zero_interval() {
535+
crate::logger();
536+
537+
let config = Config {
538+
inner: pgdog_stats::Config {
539+
max: 1,
540+
min: 0,
541+
idle_healthcheck_interval: Duration::ZERO,
542+
idle_healthcheck_delay: Duration::from_millis(10),
543+
healthcheck_timeout: Duration::from_millis(10),
544+
..Config::default().inner
545+
},
546+
};
547+
548+
let pool = Pool::new(&PoolConfig {
549+
address: Address {
550+
host: "127.0.0.1".into(),
551+
port: 1,
552+
database_name: "pgdog".into(),
553+
user: "pgdog".into(),
554+
passwords: vec!["pgdog".into()],
555+
..Default::default()
556+
},
557+
config,
558+
});
559+
pool.launch();
560+
561+
let initial_healthchecks = pool.state().stats.counts.healthchecks;
562+
563+
sleep(Duration::from_millis(350)).await;
564+
565+
let after_healthchecks = pool.state().stats.counts.healthchecks;
566+
567+
assert_eq!(after_healthchecks, initial_healthchecks);
568+
assert!(pool.healthy());
569+
}
570+
533571
#[tokio::test]
534572
async fn test_checkout_timeout() {
535573
crate::logger();

0 commit comments

Comments
 (0)