Skip to content

Commit d06e265

Browse files
authored
Add setting for controlling connection cleanup (#611)
* Add setting for controlling connection cleanup * early return * test early return * var name
1 parent 5f561be commit d06e265

File tree

7 files changed

+416
-20
lines changed

7 files changed

+416
-20
lines changed

pgdog-config/src/general.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::net::Ipv4Addr;
44
use std::path::PathBuf;
55
use std::time::Duration;
66

7+
use crate::pooling::ConnectionRecovery;
8+
79
use super::auth::{AuthType, PassthoughAuth};
810
use super::database::{LoadBalancingStrategy, ReadWriteSplit, ReadWriteStrategy};
911
use super::networking::TlsVerifyMode;
@@ -167,6 +169,9 @@ pub struct General {
167169
/// Stats averaging period (in milliseconds).
168170
#[serde(default = "General::stats_period")]
169171
pub stats_period: u64,
172+
/// Connection cleanup algorithm.
173+
#[serde(default = "General::connection_recovery")]
174+
pub connection_recovery: ConnectionRecovery,
170175
}
171176

172177
impl Default for General {
@@ -227,6 +232,7 @@ impl Default for General {
227232
expanded_explain: Self::expanded_explain(),
228233
server_lifetime: Self::server_lifetime(),
229234
stats_period: Self::stats_period(),
235+
connection_recovery: Self::connection_recovery(),
230236
}
231237
}
232238
}
@@ -509,6 +515,10 @@ impl General {
509515
)
510516
}
511517

518+
pub fn connection_recovery() -> ConnectionRecovery {
519+
Self::env_enum_or_default("PGDOG_CONNECTION_RECOVERY")
520+
}
521+
512522
fn stats_period() -> u64 {
513523
Self::env_or_default("PGDOG_STATS_PERIOD", 15_000)
514524
}

pgdog-config/src/pooling.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,38 @@ impl FromStr for PoolerMode {
7171
}
7272
}
7373
}
74+
75+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd)]
76+
#[serde(rename_all = "snake_case")]
77+
pub enum ConnectionRecovery {
78+
#[default]
79+
Recover,
80+
RollbackOnly,
81+
Drop,
82+
}
83+
84+
impl ConnectionRecovery {
85+
pub fn can_recover(&self) -> bool {
86+
matches!(self, ConnectionRecovery::Recover)
87+
}
88+
89+
pub fn can_rollback(&self) -> bool {
90+
matches!(
91+
self,
92+
ConnectionRecovery::Recover | ConnectionRecovery::RollbackOnly
93+
)
94+
}
95+
}
96+
97+
impl FromStr for ConnectionRecovery {
98+
type Err = String;
99+
100+
fn from_str(s: &str) -> Result<Self, Self::Err> {
101+
match s.to_lowercase().as_str() {
102+
"recover" => Ok(Self::Recover),
103+
"rollbackonly" => Ok(Self::RollbackOnly),
104+
"drop" => Ok(Self::Drop),
105+
_ => Err(format!("Invalid pooler mode: {}", s)),
106+
}
107+
}
108+
}

pgdog/src/backend/pool/cluster.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use crate::{
1616
Schema, ShardedTables,
1717
},
1818
config::{
19-
General, MultiTenant, PoolerMode, ReadWriteSplit, ReadWriteStrategy, ShardedTable, User,
19+
ConnectionRecovery, General, MultiTenant, PoolerMode, ReadWriteSplit, ReadWriteStrategy,
20+
ShardedTable, User,
2021
},
2122
net::{messages::BackendKeyData, Query},
2223
};
@@ -59,6 +60,7 @@ pub struct Cluster {
5960
expanded_explain: bool,
6061
pub_sub_channel_size: usize,
6162
query_parser_enabled: bool,
63+
connection_recovery: ConnectionRecovery,
6264
}
6365

6466
/// Sharding configuration from the cluster.
@@ -107,6 +109,7 @@ pub struct ClusterConfig<'a> {
107109
pub expanded_explain: bool,
108110
pub pub_sub_channel_size: usize,
109111
pub query_parser_enabled: bool,
112+
pub connection_recovery: ConnectionRecovery,
110113
}
111114

112115
impl<'a> ClusterConfig<'a> {
@@ -146,6 +149,7 @@ impl<'a> ClusterConfig<'a> {
146149
expanded_explain: general.expanded_explain,
147150
pub_sub_channel_size: general.pub_sub_channel_size,
148151
query_parser_enabled: general.query_parser_enabled,
152+
connection_recovery: general.connection_recovery,
149153
}
150154
}
151155
}
@@ -176,6 +180,7 @@ impl Cluster {
176180
expanded_explain,
177181
pub_sub_channel_size,
178182
query_parser_enabled,
183+
connection_recovery,
179184
} = config;
180185

181186
Self {
@@ -207,6 +212,7 @@ impl Cluster {
207212
expanded_explain,
208213
pub_sub_channel_size,
209214
query_parser_enabled,
215+
connection_recovery,
210216
}
211217
}
212218

@@ -304,6 +310,10 @@ impl Cluster {
304310
&self.prepared_statements
305311
}
306312

313+
pub fn connection_recovery(&self) -> &ConnectionRecovery {
314+
&self.connection_recovery
315+
}
316+
307317
pub fn dry_run(&self) -> bool {
308318
self.dry_run
309319
}

pgdog/src/backend/pool/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::time::Duration;
44

5+
use pgdog_config::pooling::ConnectionRecovery;
56
use serde::{Deserialize, Serialize};
67

78
use crate::config::{Database, General, PoolerMode, User};
@@ -59,6 +60,8 @@ pub struct Config {
5960
pub prepared_statements_limit: usize,
6061
/// Stats averaging period.
6162
pub stats_period: Duration,
63+
/// Recovery algo.
64+
pub connection_recovery: ConnectionRecovery,
6265
}
6366

6467
impl Config {
@@ -186,6 +189,7 @@ impl Config {
186189
prepared_statements_limit: general.prepared_statements_limit,
187190
stats_period: Duration::from_millis(general.stats_period),
188191
bannable: !is_only_replica,
192+
connection_recovery: general.connection_recovery,
189193
..Default::default()
190194
}
191195
}
@@ -219,6 +223,7 @@ impl Default for Config {
219223
prepared_statements_limit: usize::MAX,
220224
stats_period: Duration::from_millis(15_000),
221225
dns_ttl: Duration::from_millis(60_000),
226+
connection_recovery: ConnectionRecovery::Recover,
222227
}
223228
}
224229
}

0 commit comments

Comments
 (0)