Skip to content

Commit 071ca75

Browse files
committed
use reference counting for healtcheck_query
1 parent e3d6c1d commit 071ca75

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

pgdog/src/backend/pool/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Pool configuration.
22
3+
use std::sync::Arc;
34
use std::time::Duration;
45

56
use serde::{Deserialize, Serialize};
@@ -24,7 +25,7 @@ pub struct Config {
2425
/// Can this pool be banned from serving traffic?
2526
pub bannable: bool,
2627
/// Healtcheck query.
27-
pub healthcheck_query: String,
28+
pub healthcheck_query: Arc<String>,
2829
/// Healtheck timeout.
2930
pub healthcheck_timeout: Duration, // ms
3031
/// Healtcheck interval.
@@ -73,8 +74,8 @@ impl Config {
7374
}
7475

7576
/// Healthcheck query.
76-
pub fn healthcheck_query(&self) -> &String {
77-
&self.healthcheck_query
77+
pub fn healthcheck_query(&self) -> Arc<String> {
78+
self.healthcheck_query.clone()
7879
}
7980

8081
/// Healthcheck timeout.
@@ -177,7 +178,7 @@ impl Default for Config {
177178
connect_timeout: Duration::from_millis(5_000),
178179
max_age: Duration::from_millis(24 * 3600 * 1000),
179180
bannable: true,
180-
healthcheck_query: ";".into(),
181+
healthcheck_query: Arc::new(";".into()),
181182
healthcheck_timeout: Duration::from_millis(5_000),
182183
healthcheck_interval: Duration::from_millis(30_000),
183184
idle_healthcheck_interval: Duration::from_millis(5_000),

pgdog/src/backend/pool/healthcheck.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Healtcheck a connection.
22
3+
use std::sync::Arc;
34
use std::time::{Duration, Instant};
45

56
use tokio::time::timeout;
@@ -12,7 +13,7 @@ use crate::backend::Server;
1213
pub struct Healtcheck<'a> {
1314
conn: &'a mut Server,
1415
pool: Pool,
15-
healthcheck_query: String,
16+
healthcheck_query: Arc<String>,
1617
healthcheck_interval: Duration,
1718
healthcheck_timeout: Duration,
1819
}
@@ -22,7 +23,7 @@ impl<'a> Healtcheck<'a> {
2223
pub fn conditional(
2324
conn: &'a mut Server,
2425
pool: Pool,
25-
healthcheck_query: String,
26+
healthcheck_query: Arc<String>,
2627
healthcheck_interval: Duration,
2728
healthcheck_timeout: Duration,
2829
) -> Self {
@@ -39,7 +40,7 @@ impl<'a> Healtcheck<'a> {
3940
pub fn mandatory(
4041
conn: &'a mut Server,
4142
pool: Pool,
42-
healthcheck_query: String,
43+
healthcheck_query: Arc<String>,
4344
healthcheck_timeout: Duration,
4445
) -> Self {
4546
Self::conditional(

pgdog/src/backend/pool/monitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl Monitor {
280280
}
281281
(
282282
guard.take(&Request::default()),
283-
guard.config.healthcheck_query().to_owned(),
283+
guard.config.healthcheck_query().clone(),
284284
guard.config.healthcheck_timeout(),
285285
guard.config.connect_timeout(),
286286
)

pgdog/src/backend/pool/pool_impl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Pool {
129129
} else {
130130
guard.config.checkout_timeout
131131
},
132-
guard.config.healthcheck_query.to_string(),
132+
guard.config.healthcheck_query.clone(),
133133
guard.config.healthcheck_timeout,
134134
guard.config.healthcheck_interval,
135135
conn,
@@ -177,7 +177,7 @@ impl Pool {
177177
async fn maybe_healthcheck(
178178
&self,
179179
mut conn: Guard,
180-
healthcheck_query: String,
180+
healthcheck_query: Arc<String>,
181181
healthcheck_timeout: Duration,
182182
healthcheck_interval: Duration,
183183
) -> Result<Guard, Error> {
@@ -357,8 +357,8 @@ impl Pool {
357357
}
358358

359359
#[inline]
360-
pub fn healthcheck_query(&self) -> String {
361-
self.lock().config.clone().healthcheck_query
360+
pub fn healthcheck_query(&self) -> Arc<String> {
361+
self.lock().config.clone().healthcheck_query.clone()
362362
}
363363

364364
/// Get startup parameters for new server connections.
@@ -374,7 +374,7 @@ impl Pool {
374374
},
375375
];
376376

377-
let config = { *self.lock().config() };
377+
let config = { self.lock().config().clone() };
378378

379379
if let Some(statement_timeout) = config.statement_timeout {
380380
params.push(Parameter {

pgdog/src/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub struct General {
272272
pub pooler_mode: PoolerMode,
273273
/// Query to check a connection.
274274
#[serde(default = "General::healthcheck_query")]
275-
pub healthcheck_query: String,
275+
pub healthcheck_query: Arc<String>,
276276
/// How often to check a connection.
277277
#[serde(default = "General::healthcheck_interval")]
278278
pub healthcheck_interval: u64,
@@ -413,8 +413,8 @@ impl General {
413413
1
414414
}
415415

416-
fn healthcheck_query() -> String {
417-
";".into()
416+
fn healthcheck_query() -> Arc<String> {
417+
Arc::new(";".into())
418418
}
419419

420420
fn healthcheck_interval() -> u64 {

0 commit comments

Comments
 (0)