Skip to content

Adds health check setting to pool and avoids get_config in hotpath #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::Arc;
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::server::Server;
Expand Down Expand Up @@ -82,6 +82,12 @@ pub struct PoolSettings {

// Sharding key
pub automatic_sharding_key: Option<String>,

// Health check timeout
pub healthcheck_timeout: u64,

// Health check delay
pub healthcheck_delay: u64,
}

impl Default for PoolSettings {
Expand All @@ -95,6 +101,8 @@ impl Default for PoolSettings {
primary_reads_enabled: true,
sharding_function: ShardingFunction::PgBigintHash,
automatic_sharding_key: None,
healthcheck_delay: General::default_healthcheck_delay(),
healthcheck_timeout: General::default_healthcheck_timeout(),
}
}
}
Expand Down Expand Up @@ -256,6 +264,8 @@ impl ConnectionPool {
primary_reads_enabled: pool_config.primary_reads_enabled,
sharding_function: pool_config.sharding_function,
automatic_sharding_key: pool_config.automatic_sharding_key.clone(),
healthcheck_delay: config.general.healthcheck_delay,
healthcheck_timeout: config.general.healthcheck_timeout,
},
};

Expand Down Expand Up @@ -343,9 +353,6 @@ impl ConnectionPool {
// Random load balancing
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() {
// Get the next candidate
let address = match candidates.pop() {
Expand Down Expand Up @@ -380,8 +387,8 @@ impl ConnectionPool {
let server = &mut *conn;

// Will return error if timestamp is greater than current system time, which it should never be set to
let require_healthcheck =
server.last_activity().elapsed().unwrap().as_millis() > healthcheck_delay;
let require_healthcheck = server.last_activity().elapsed().unwrap().as_millis()
> self.settings.healthcheck_delay as u128;

// Do not issue a health check unless it's been a little while
// since we last checked the server is ok.
Expand All @@ -398,7 +405,7 @@ impl ConnectionPool {
self.stats.server_tested(server.server_id());

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
)
.await
Expand Down
2 changes: 2 additions & 0 deletions src/query_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ mod test {
primary_reads_enabled: false,
sharding_function: ShardingFunction::PgBigintHash,
automatic_sharding_key: Some(String::from("id")),
healthcheck_delay: PoolSettings::default().healthcheck_delay,
healthcheck_timeout: PoolSettings::default().healthcheck_timeout,
};
let mut qr = QueryRouter::new();
assert_eq!(qr.active_role, None);
Expand Down