Skip to content

Commit f72dac4

Browse files
authored
Add defaults for configs (#174)
* add statement timeout to readme * Add defaults to various configs * primary read enabled default to false
1 parent 3a729bb commit f72dac4

File tree

5 files changed

+115
-58
lines changed

5 files changed

+115
-58
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ psql -h 127.0.0.1 -p 6432 -c 'SELECT 1'
6060
| **`user`** | | |
6161
| `name` | The user name. | `sharding_user` |
6262
| `password` | The user password in plaintext. | `hunter2` |
63+
| `statement_timeout` | Timeout in milliseconds for how long a query takes to execute | `0 (disabled)` |
64+
6365
| | | |
6466
| **`shards`** | Shards are numerically numbered starting from 0; the order in the config is preserved by the pooler to route queries accordingly. | `[shards.0]` |
6567
| `servers` | List of servers to connect to and their roles. A server is: `[host, port, role]`, where `role` is either `primary` or `replica`. | `["127.0.0.1", 5432, "primary"]` |

src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use tokio::sync::broadcast::Receiver;
99
use tokio::sync::mpsc::Sender;
1010

1111
use crate::admin::{generate_server_info_for_admin, handle_admin};
12-
use crate::config::{get_config, Address};
12+
use crate::config::{get_config, Address, PoolMode};
1313
use crate::constants::*;
1414
use crate::errors::Error;
1515
use crate::messages::*;
16-
use crate::pool::{get_pool, ClientServerMap, ConnectionPool, PoolMode};
16+
use crate::pool::{get_pool, ClientServerMap, ConnectionPool};
1717
use crate::query_router::{Command, QueryRouter};
1818
use crate::server::Server;
1919
use crate::stats::{get_reporter, Reporter};

src/config.rs

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct User {
127127
pub username: String,
128128
pub password: String,
129129
pub pool_size: u32,
130+
#[serde(default)] // 0
130131
pub statement_timeout: u64,
131132
}
132133

@@ -144,34 +145,81 @@ impl Default for User {
144145
/// General configuration.
145146
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
146147
pub struct General {
148+
#[serde(default = "General::default_host")]
147149
pub host: String,
150+
151+
#[serde(default = "General::default_port")]
148152
pub port: i16,
153+
149154
pub enable_prometheus_exporter: Option<bool>,
150155
pub prometheus_exporter_port: i16,
156+
157+
#[serde(default = "General::default_connect_timeout")]
151158
pub connect_timeout: u64,
152-
pub healthcheck_timeout: u64,
159+
160+
#[serde(default = "General::default_shutdown_timeout")]
153161
pub shutdown_timeout: u64,
162+
163+
#[serde(default = "General::default_healthcheck_timeout")]
164+
pub healthcheck_timeout: u64,
165+
166+
#[serde(default = "General::default_healthcheck_delay")]
154167
pub healthcheck_delay: u64,
168+
169+
#[serde(default = "General::default_ban_time")]
155170
pub ban_time: i64,
171+
172+
#[serde(default)] // False
156173
pub autoreload: bool,
174+
157175
pub tls_certificate: Option<String>,
158176
pub tls_private_key: Option<String>,
159177
pub admin_username: String,
160178
pub admin_password: String,
161179
}
162180

181+
impl General {
182+
fn default_host() -> String {
183+
"0.0.0.0".into()
184+
}
185+
186+
fn default_port() -> i16 {
187+
5432
188+
}
189+
190+
fn default_connect_timeout() -> u64 {
191+
1000
192+
}
193+
194+
fn default_shutdown_timeout() -> u64 {
195+
60000
196+
}
197+
198+
fn default_healthcheck_timeout() -> u64 {
199+
1000
200+
}
201+
202+
fn default_healthcheck_delay() -> u64 {
203+
30000
204+
}
205+
206+
fn default_ban_time() -> i64 {
207+
60
208+
}
209+
}
210+
163211
impl Default for General {
164212
fn default() -> General {
165213
General {
166-
host: String::from("localhost"),
167-
port: 5432,
214+
host: General::default_host(),
215+
port: General::default_port(),
168216
enable_prometheus_exporter: Some(false),
169217
prometheus_exporter_port: 9930,
170-
connect_timeout: 5000,
171-
healthcheck_timeout: 1000,
172-
shutdown_timeout: 60000,
173-
healthcheck_delay: 30000,
174-
ban_time: 60,
218+
connect_timeout: General::default_connect_timeout(),
219+
shutdown_timeout: General::default_shutdown_timeout(),
220+
healthcheck_timeout: General::default_healthcheck_timeout(),
221+
healthcheck_delay: General::default_healthcheck_delay(),
222+
ban_time: General::default_ban_time(),
175223
autoreload: false,
176224
tls_certificate: None,
177225
tls_private_key: None,
@@ -180,25 +228,61 @@ impl Default for General {
180228
}
181229
}
182230
}
231+
232+
/// Pool mode:
233+
/// - transaction: server serves one transaction,
234+
/// - session: server is attached to the client.
235+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Copy)]
236+
pub enum PoolMode {
237+
#[serde(alias = "transaction", alias = "Transaction")]
238+
Transaction,
239+
240+
#[serde(alias = "session", alias = "Session")]
241+
Session,
242+
}
243+
244+
impl ToString for PoolMode {
245+
fn to_string(&self) -> String {
246+
match *self {
247+
PoolMode::Transaction => "transaction".to_string(),
248+
PoolMode::Session => "session".to_string(),
249+
}
250+
}
251+
}
252+
183253
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
184254
pub struct Pool {
185-
pub pool_mode: String,
255+
#[serde(default = "Pool::default_pool_mode")]
256+
pub pool_mode: PoolMode,
257+
186258
pub default_role: String,
259+
260+
#[serde(default)] // False
187261
pub query_parser_enabled: bool,
262+
263+
#[serde(default)] // False
188264
pub primary_reads_enabled: bool,
265+
189266
pub sharding_function: String,
190267
pub shards: HashMap<String, Shard>,
191268
pub users: HashMap<String, User>,
192269
}
270+
271+
impl Pool {
272+
fn default_pool_mode() -> PoolMode {
273+
PoolMode::Transaction
274+
}
275+
}
276+
193277
impl Default for Pool {
194278
fn default() -> Pool {
195279
Pool {
196-
pool_mode: String::from("transaction"),
280+
pool_mode: Pool::default_pool_mode(),
197281
shards: HashMap::from([(String::from("1"), Shard::default())]),
198282
users: HashMap::default(),
199283
default_role: String::from("any"),
200284
query_parser_enabled: false,
201-
primary_reads_enabled: true,
285+
primary_reads_enabled: false,
202286
sharding_function: "pg_bigint_hash".to_string(),
203287
}
204288
}
@@ -231,10 +315,6 @@ impl Default for Shard {
231315
}
232316
}
233317

234-
fn default_path() -> String {
235-
String::from("pgcat.toml")
236-
}
237-
238318
/// Configuration wrapper.
239319
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
240320
pub struct Config {
@@ -249,17 +329,23 @@ pub struct Config {
249329
// [main.subconf]
250330
// field1_under_subconf = 1
251331
// field3_under_main = 3 # This field will be interpreted as being under subconf and not under main
252-
#[serde(default = "default_path")]
332+
#[serde(default = "Config::default_path")]
253333
pub path: String,
254334

255335
pub general: General,
256336
pub pools: HashMap<String, Pool>,
257337
}
258338

339+
impl Config {
340+
fn default_path() -> String {
341+
String::from("pgcat.toml")
342+
}
343+
}
344+
259345
impl Default for Config {
260346
fn default() -> Config {
261347
Config {
262-
path: String::from("pgcat.toml"),
348+
path: Config::default_path(),
263349
general: General::default(),
264350
pools: HashMap::default(),
265351
}
@@ -275,7 +361,7 @@ impl From<&Config> for std::collections::HashMap<String, String> {
275361
[
276362
(
277363
format!("pools.{}.pool_mode", pool_name),
278-
pool.pool_mode.clone(),
364+
pool.pool_mode.to_string(),
279365
),
280366
(
281367
format!("pools.{}.primary_reads_enabled", pool_name),
@@ -383,7 +469,10 @@ impl Config {
383469
.sum::<u32>()
384470
.to_string()
385471
);
386-
info!("[pool: {}] Pool mode: {}", pool_name, pool_config.pool_mode);
472+
info!(
473+
"[pool: {}] Pool mode: {:?}",
474+
pool_name, pool_config.pool_mode
475+
);
387476
info!(
388477
"[pool: {}] Sharding function: {}",
389478
pool_name, pool_config.sharding_function
@@ -513,18 +602,6 @@ pub async fn parse(path: &str) -> Result<(), Error> {
513602
}
514603
};
515604

516-
match pool.pool_mode.as_ref() {
517-
"transaction" => (),
518-
"session" => (),
519-
other => {
520-
error!(
521-
"pool_mode can be 'session' or 'transaction', got: '{}'",
522-
other
523-
);
524-
return Err(Error::BadConfig);
525-
}
526-
};
527-
528605
for shard in &pool.shards {
529606
// We use addresses as unique identifiers,
530607
// let's make sure they are unique in the config as well.

src/pool.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::collections::HashMap;
1212
use std::sync::Arc;
1313
use std::time::Instant;
1414

15-
use crate::config::{get_config, Address, Role, User};
15+
use crate::config::{get_config, Address, PoolMode, Role, User};
1616
use crate::errors::Error;
1717

1818
use crate::server::Server;
@@ -27,24 +27,6 @@ pub type PoolMap = HashMap<(String, String), ConnectionPool>;
2727
/// The pool is recreated dynamically when the config is reloaded.
2828
pub static POOLS: Lazy<ArcSwap<PoolMap>> = Lazy::new(|| ArcSwap::from_pointee(HashMap::default()));
2929

30-
/// Pool mode:
31-
/// - transaction: server serves one transaction,
32-
/// - session: server is attached to the client.
33-
#[derive(Debug, Clone, Copy, PartialEq)]
34-
pub enum PoolMode {
35-
Session,
36-
Transaction,
37-
}
38-
39-
impl std::fmt::Display for PoolMode {
40-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41-
match *self {
42-
PoolMode::Session => write!(f, "session"),
43-
PoolMode::Transaction => write!(f, "transaction"),
44-
}
45-
}
46-
}
47-
4830
/// Pool settings.
4931
#[derive(Clone, Debug)]
5032
pub struct PoolSettings {
@@ -199,11 +181,7 @@ impl ConnectionPool {
199181
stats: get_reporter(),
200182
server_info: BytesMut::new(),
201183
settings: PoolSettings {
202-
pool_mode: match pool_config.pool_mode.as_str() {
203-
"transaction" => PoolMode::Transaction,
204-
"session" => PoolMode::Session,
205-
_ => unreachable!(),
206-
},
184+
pool_mode: pool_config.pool_mode,
207185
// shards: pool_config.shards.clone(),
208186
shards: shard_ids.len(),
209187
user: user.clone(),

src/query_router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ impl QueryRouter {
359359
#[cfg(test)]
360360
mod test {
361361
use super::*;
362+
use crate::config::PoolMode;
362363
use crate::messages::simple_query;
363-
use crate::pool::PoolMode;
364364
use crate::sharding::ShardingFunction;
365365
use bytes::BufMut;
366366

0 commit comments

Comments
 (0)