@@ -4,7 +4,7 @@ use log::{error, info};
4
4
use once_cell:: sync:: Lazy ;
5
5
use serde_derive:: { Deserialize , Serialize } ;
6
6
use std:: collections:: { HashMap , HashSet } ;
7
- use std:: hash:: Hash ;
7
+ use std:: hash:: { Hash , Hasher } ;
8
8
use std:: path:: Path ;
9
9
use std:: sync:: Arc ;
10
10
use tokio:: fs:: File ;
@@ -122,7 +122,7 @@ impl Address {
122
122
}
123
123
124
124
/// PostgreSQL user.
125
- #[ derive( Clone , PartialEq , Hash , std :: cmp :: Eq , Serialize , Deserialize , Debug ) ]
125
+ #[ derive( Clone , PartialEq , Hash , Eq , Serialize , Deserialize , Debug ) ]
126
126
pub struct User {
127
127
pub username : String ,
128
128
pub password : String ,
@@ -232,7 +232,7 @@ impl Default for General {
232
232
/// Pool mode:
233
233
/// - transaction: server serves one transaction,
234
234
/// - session: server is attached to the client.
235
- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , Copy ) ]
235
+ #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , Eq , Copy , Hash ) ]
236
236
pub enum PoolMode {
237
237
#[ serde( alias = "transaction" , alias = "Transaction" ) ]
238
238
Transaction ,
@@ -250,7 +250,7 @@ impl ToString for PoolMode {
250
250
}
251
251
}
252
252
253
- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
253
+ #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , Eq ) ]
254
254
pub struct Pool {
255
255
#[ serde( default = "Pool::default_pool_mode" ) ]
256
256
pub pool_mode : PoolMode ,
@@ -263,11 +263,35 @@ pub struct Pool {
263
263
#[ serde( default ) ] // False
264
264
pub primary_reads_enabled : bool ,
265
265
266
+ #[ serde( default = "General::default_connect_timeout" ) ]
267
+ pub connect_timeout : u64 ,
268
+
266
269
pub sharding_function : String ,
267
270
pub shards : HashMap < String , Shard > ,
268
271
pub users : HashMap < String , User > ,
269
272
}
270
273
274
+ impl Hash for Pool {
275
+ fn hash < H : Hasher > ( & self , state : & mut H ) {
276
+ self . pool_mode . hash ( state) ;
277
+ self . default_role . hash ( state) ;
278
+ self . query_parser_enabled . hash ( state) ;
279
+ self . primary_reads_enabled . hash ( state) ;
280
+ self . sharding_function . hash ( state) ;
281
+ self . connect_timeout . hash ( state) ;
282
+
283
+ for ( key, value) in & self . shards {
284
+ key. hash ( state) ;
285
+ value. hash ( state) ;
286
+ }
287
+
288
+ for ( key, value) in & self . users {
289
+ key. hash ( state) ;
290
+ value. hash ( state) ;
291
+ }
292
+ }
293
+ }
294
+
271
295
impl Pool {
272
296
fn default_pool_mode ( ) -> PoolMode {
273
297
PoolMode :: Transaction
@@ -284,6 +308,7 @@ impl Default for Pool {
284
308
query_parser_enabled : false ,
285
309
primary_reads_enabled : false ,
286
310
sharding_function : "pg_bigint_hash" . to_string ( ) ,
311
+ connect_timeout : General :: default_connect_timeout ( ) ,
287
312
}
288
313
}
289
314
}
@@ -296,7 +321,7 @@ pub struct ServerConfig {
296
321
}
297
322
298
323
/// Shard configuration.
299
- #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq ) ]
324
+ #[ derive( Serialize , Deserialize , Debug , Clone , PartialEq , Hash , Eq ) ]
300
325
pub struct Shard {
301
326
pub database : String ,
302
327
pub servers : Vec < ServerConfig > ,
@@ -575,7 +600,10 @@ pub async fn parse(path: &str) -> Result<(), Error> {
575
600
None => ( ) ,
576
601
} ;
577
602
578
- for ( pool_name, pool) in & config. pools {
603
+ for ( pool_name, mut pool) in & mut config. pools {
604
+ // Copy the connect timeout over for hashing.
605
+ pool. connect_timeout = config. general . connect_timeout ;
606
+
579
607
match pool. sharding_function . as_ref ( ) {
580
608
"pg_bigint_hash" => ( ) ,
581
609
"sha1" => ( ) ,
@@ -666,7 +694,7 @@ pub async fn reload_config(client_server_map: ClientServerMap) -> Result<bool, E
666
694
let new_config = get_config ( ) ;
667
695
668
696
if old_config. pools != new_config. pools {
669
- info ! ( "Pool configuration changed, re-creating server pools " ) ;
697
+ info ! ( "Pool configuration changed" ) ;
670
698
ConnectionPool :: from_config ( client_server_map) . await ?;
671
699
Ok ( true )
672
700
} else if old_config != new_config {
0 commit comments