mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-28 03:06:29 +00:00
Adds health check setting to pool and avoids get_config in hotpath (#235)
* Adds healthcheck settings to pool * fmt * Fix test
This commit is contained in:
21
src/pool.rs
21
src/pool.rs
@@ -12,7 +12,7 @@ use std::collections::{HashMap, HashSet};
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use crate::config::{get_config, Address, PoolMode, Role, User};
|
use crate::config::{get_config, Address, General, PoolMode, Role, User};
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
@@ -82,6 +82,12 @@ pub struct PoolSettings {
|
|||||||
|
|
||||||
// Sharding key
|
// Sharding key
|
||||||
pub automatic_sharding_key: Option<String>,
|
pub automatic_sharding_key: Option<String>,
|
||||||
|
|
||||||
|
// Health check timeout
|
||||||
|
pub healthcheck_timeout: u64,
|
||||||
|
|
||||||
|
// Health check delay
|
||||||
|
pub healthcheck_delay: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for PoolSettings {
|
impl Default for PoolSettings {
|
||||||
@@ -95,6 +101,8 @@ impl Default for PoolSettings {
|
|||||||
primary_reads_enabled: true,
|
primary_reads_enabled: true,
|
||||||
sharding_function: ShardingFunction::PgBigintHash,
|
sharding_function: ShardingFunction::PgBigintHash,
|
||||||
automatic_sharding_key: None,
|
automatic_sharding_key: None,
|
||||||
|
healthcheck_delay: General::default_healthcheck_delay(),
|
||||||
|
healthcheck_timeout: General::default_healthcheck_timeout(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,6 +264,8 @@ impl ConnectionPool {
|
|||||||
primary_reads_enabled: pool_config.primary_reads_enabled,
|
primary_reads_enabled: pool_config.primary_reads_enabled,
|
||||||
sharding_function: pool_config.sharding_function,
|
sharding_function: pool_config.sharding_function,
|
||||||
automatic_sharding_key: pool_config.automatic_sharding_key.clone(),
|
automatic_sharding_key: pool_config.automatic_sharding_key.clone(),
|
||||||
|
healthcheck_delay: config.general.healthcheck_delay,
|
||||||
|
healthcheck_timeout: config.general.healthcheck_timeout,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -343,9 +353,6 @@ impl ConnectionPool {
|
|||||||
// Random load balancing
|
// Random load balancing
|
||||||
candidates.shuffle(&mut thread_rng());
|
candidates.shuffle(&mut thread_rng());
|
||||||
|
|
||||||
let healthcheck_timeout = get_config().general.healthcheck_timeout;
|
|
||||||
let healthcheck_delay = get_config().general.healthcheck_delay as u128;
|
|
||||||
|
|
||||||
while !candidates.is_empty() {
|
while !candidates.is_empty() {
|
||||||
// Get the next candidate
|
// Get the next candidate
|
||||||
let address = match candidates.pop() {
|
let address = match candidates.pop() {
|
||||||
@@ -380,8 +387,8 @@ impl ConnectionPool {
|
|||||||
let server = &mut *conn;
|
let server = &mut *conn;
|
||||||
|
|
||||||
// Will return error if timestamp is greater than current system time, which it should never be set to
|
// Will return error if timestamp is greater than current system time, which it should never be set to
|
||||||
let require_healthcheck =
|
let require_healthcheck = server.last_activity().elapsed().unwrap().as_millis()
|
||||||
server.last_activity().elapsed().unwrap().as_millis() > healthcheck_delay;
|
> self.settings.healthcheck_delay as u128;
|
||||||
|
|
||||||
// Do not issue a health check unless it's been a little while
|
// Do not issue a health check unless it's been a little while
|
||||||
// since we last checked the server is ok.
|
// since we last checked the server is ok.
|
||||||
@@ -398,7 +405,7 @@ impl ConnectionPool {
|
|||||||
self.stats.server_tested(server.server_id());
|
self.stats.server_tested(server.server_id());
|
||||||
|
|
||||||
match tokio::time::timeout(
|
match tokio::time::timeout(
|
||||||
tokio::time::Duration::from_millis(healthcheck_timeout),
|
tokio::time::Duration::from_millis(self.settings.healthcheck_timeout),
|
||||||
server.query(";"), // Cheap query as it skips the query planner
|
server.query(";"), // Cheap query as it skips the query planner
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -775,6 +775,8 @@ mod test {
|
|||||||
primary_reads_enabled: false,
|
primary_reads_enabled: false,
|
||||||
sharding_function: ShardingFunction::PgBigintHash,
|
sharding_function: ShardingFunction::PgBigintHash,
|
||||||
automatic_sharding_key: Some(String::from("id")),
|
automatic_sharding_key: Some(String::from("id")),
|
||||||
|
healthcheck_delay: PoolSettings::default().healthcheck_delay,
|
||||||
|
healthcheck_timeout: PoolSettings::default().healthcheck_timeout,
|
||||||
};
|
};
|
||||||
let mut qr = QueryRouter::new();
|
let mut qr = QueryRouter::new();
|
||||||
assert_eq!(qr.active_role, None);
|
assert_eq!(qr.active_role, None);
|
||||||
|
|||||||
Reference in New Issue
Block a user