@@ -127,6 +127,7 @@ pub struct User {
127
127
pub username : String ,
128
128
pub password : String ,
129
129
pub pool_size : u32 ,
130
+ #[ serde( default ) ] // 0
130
131
pub statement_timeout : u64 ,
131
132
}
132
133
@@ -144,34 +145,81 @@ impl Default for User {
144
145
/// General configuration.
145
146
#[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
146
147
pub struct General {
148
+ #[ serde( default = "General::default_host" ) ]
147
149
pub host : String ,
150
+
151
+ #[ serde( default = "General::default_port" ) ]
148
152
pub port : i16 ,
153
+
149
154
pub enable_prometheus_exporter : Option < bool > ,
150
155
pub prometheus_exporter_port : i16 ,
156
+
157
+ #[ serde( default = "General::default_connect_timeout" ) ]
151
158
pub connect_timeout : u64 ,
152
- pub healthcheck_timeout : u64 ,
159
+
160
+ #[ serde( default = "General::default_shutdown_timeout" ) ]
153
161
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" ) ]
154
167
pub healthcheck_delay : u64 ,
168
+
169
+ #[ serde( default = "General::default_ban_time" ) ]
155
170
pub ban_time : i64 ,
171
+
172
+ #[ serde( default ) ] // False
156
173
pub autoreload : bool ,
174
+
157
175
pub tls_certificate : Option < String > ,
158
176
pub tls_private_key : Option < String > ,
159
177
pub admin_username : String ,
160
178
pub admin_password : String ,
161
179
}
162
180
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
+
163
211
impl Default for General {
164
212
fn default ( ) -> General {
165
213
General {
166
- host : String :: from ( "localhost" ) ,
167
- port : 5432 ,
214
+ host : General :: default_host ( ) ,
215
+ port : General :: default_port ( ) ,
168
216
enable_prometheus_exporter : Some ( false ) ,
169
217
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 ( ) ,
175
223
autoreload : false ,
176
224
tls_certificate : None ,
177
225
tls_private_key : None ,
@@ -180,25 +228,61 @@ impl Default for General {
180
228
}
181
229
}
182
230
}
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
+
183
253
#[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
184
254
pub struct Pool {
185
- pub pool_mode : String ,
255
+ #[ serde( default = "Pool::default_pool_mode" ) ]
256
+ pub pool_mode : PoolMode ,
257
+
186
258
pub default_role : String ,
259
+
260
+ #[ serde( default ) ] // False
187
261
pub query_parser_enabled : bool ,
262
+
263
+ #[ serde( default ) ] // False
188
264
pub primary_reads_enabled : bool ,
265
+
189
266
pub sharding_function : String ,
190
267
pub shards : HashMap < String , Shard > ,
191
268
pub users : HashMap < String , User > ,
192
269
}
270
+
271
+ impl Pool {
272
+ fn default_pool_mode ( ) -> PoolMode {
273
+ PoolMode :: Transaction
274
+ }
275
+ }
276
+
193
277
impl Default for Pool {
194
278
fn default ( ) -> Pool {
195
279
Pool {
196
- pool_mode : String :: from ( "transaction" ) ,
280
+ pool_mode : Pool :: default_pool_mode ( ) ,
197
281
shards : HashMap :: from ( [ ( String :: from ( "1" ) , Shard :: default ( ) ) ] ) ,
198
282
users : HashMap :: default ( ) ,
199
283
default_role : String :: from ( "any" ) ,
200
284
query_parser_enabled : false ,
201
- primary_reads_enabled : true ,
285
+ primary_reads_enabled : false ,
202
286
sharding_function : "pg_bigint_hash" . to_string ( ) ,
203
287
}
204
288
}
@@ -231,10 +315,6 @@ impl Default for Shard {
231
315
}
232
316
}
233
317
234
- fn default_path ( ) -> String {
235
- String :: from ( "pgcat.toml" )
236
- }
237
-
238
318
/// Configuration wrapper.
239
319
#[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
240
320
pub struct Config {
@@ -249,17 +329,23 @@ pub struct Config {
249
329
// [main.subconf]
250
330
// field1_under_subconf = 1
251
331
// 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" ) ]
253
333
pub path : String ,
254
334
255
335
pub general : General ,
256
336
pub pools : HashMap < String , Pool > ,
257
337
}
258
338
339
+ impl Config {
340
+ fn default_path ( ) -> String {
341
+ String :: from ( "pgcat.toml" )
342
+ }
343
+ }
344
+
259
345
impl Default for Config {
260
346
fn default ( ) -> Config {
261
347
Config {
262
- path : String :: from ( "pgcat.toml" ) ,
348
+ path : Config :: default_path ( ) ,
263
349
general : General :: default ( ) ,
264
350
pools : HashMap :: default ( ) ,
265
351
}
@@ -275,7 +361,7 @@ impl From<&Config> for std::collections::HashMap<String, String> {
275
361
[
276
362
(
277
363
format ! ( "pools.{}.pool_mode" , pool_name) ,
278
- pool. pool_mode . clone ( ) ,
364
+ pool. pool_mode . to_string ( ) ,
279
365
) ,
280
366
(
281
367
format ! ( "pools.{}.primary_reads_enabled" , pool_name) ,
@@ -383,7 +469,10 @@ impl Config {
383
469
. sum:: <u32 >( )
384
470
. to_string( )
385
471
) ;
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
+ ) ;
387
476
info ! (
388
477
"[pool: {}] Sharding function: {}" ,
389
478
pool_name, pool_config. sharding_function
@@ -513,18 +602,6 @@ pub async fn parse(path: &str) -> Result<(), Error> {
513
602
}
514
603
} ;
515
604
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
-
528
605
for shard in & pool. shards {
529
606
// We use addresses as unique identifiers,
530
607
// let's make sure they are unique in the config as well.
0 commit comments