@@ -19,16 +19,43 @@ use crate::server::Server;
19
19
use crate :: sharding:: ShardingFunction ;
20
20
use crate :: stats:: { get_reporter, Reporter } ;
21
21
22
+ pub type ProcessId = i32 ;
23
+ pub type SecretKey = i32 ;
24
+ pub type ServerHost = String ;
25
+ pub type ServerPort = u16 ;
26
+
22
27
pub type BanList = Arc < RwLock < Vec < HashMap < Address , NaiveDateTime > > > > ;
23
- pub type ClientServerMap = Arc < Mutex < HashMap < ( i32 , i32 ) , ( i32 , i32 , String , u16 ) > > > ;
24
- pub type PoolMap = HashMap < ( String , String ) , ConnectionPool > ;
28
+ pub type ClientServerMap =
29
+ Arc < Mutex < HashMap < ( ProcessId , SecretKey ) , ( ProcessId , SecretKey , ServerHost , ServerPort ) > > > ;
30
+ pub type PoolMap = HashMap < PoolIdentifier , ConnectionPool > ;
25
31
/// The connection pool, globally available.
26
32
/// This is atomic and safe and read-optimized.
27
33
/// The pool is recreated dynamically when the config is reloaded.
28
34
pub static POOLS : Lazy < ArcSwap < PoolMap > > = Lazy :: new ( || ArcSwap :: from_pointee ( HashMap :: default ( ) ) ) ;
29
35
static POOLS_HASH : Lazy < ArcSwap < HashSet < crate :: config:: Pool > > > =
30
36
Lazy :: new ( || ArcSwap :: from_pointee ( HashSet :: default ( ) ) ) ;
31
37
38
+ /// An identifier for a PgCat pool,
39
+ /// a database visible to clients.
40
+ #[ derive( Hash , Debug , Clone , PartialEq , Eq ) ]
41
+ pub struct PoolIdentifier {
42
+ // The name of the database clients want to connect to.
43
+ pub db : String ,
44
+
45
+ /// The username the client connects with. Each user gets its own pool.
46
+ pub user : String ,
47
+ }
48
+
49
+ impl PoolIdentifier {
50
+ /// Create a new user/pool identifier.
51
+ pub fn new ( db : & str , user : & str ) -> PoolIdentifier {
52
+ PoolIdentifier {
53
+ db : db. to_string ( ) ,
54
+ user : user. to_string ( ) ,
55
+ }
56
+ }
57
+ }
58
+
32
59
/// Pool settings.
33
60
#[ derive( Clone , Debug ) ]
34
61
pub struct PoolSettings {
@@ -113,14 +140,16 @@ impl ConnectionPool {
113
140
// If the pool hasn't changed, get existing reference and insert it into the new_pools.
114
141
// We replace all pools at the end, but if the reference is kept, the pool won't get re-created (bb8).
115
142
if !changed {
116
- match get_pool ( pool_name. clone ( ) , user. username . clone ( ) ) {
143
+ match get_pool ( & pool_name, & user. username ) {
117
144
Some ( pool) => {
118
145
info ! (
119
146
"[pool: {}][user: {}] has not changed" ,
120
147
pool_name, user. username
121
148
) ;
122
- new_pools
123
- . insert ( ( pool_name. clone ( ) , user. username . clone ( ) ) , pool. clone ( ) ) ;
149
+ new_pools. insert (
150
+ PoolIdentifier :: new ( & pool_name, & user. username ) ,
151
+ pool. clone ( ) ,
152
+ ) ;
124
153
continue ;
125
154
}
126
155
None => ( ) ,
@@ -239,7 +268,7 @@ impl ConnectionPool {
239
268
} ;
240
269
241
270
// There is one pool per database/user pair.
242
- new_pools. insert ( ( pool_name. clone ( ) , user. username . clone ( ) ) , pool) ;
271
+ new_pools. insert ( PoolIdentifier :: new ( & pool_name, & user. username ) , pool) ;
243
272
}
244
273
}
245
274
@@ -603,15 +632,15 @@ impl ManageConnection for ServerPool {
603
632
}
604
633
605
634
/// Get the connection pool
606
- pub fn get_pool ( db : String , user : String ) -> Option < ConnectionPool > {
607
- match get_all_pools ( ) . get ( & ( db, user) ) {
635
+ pub fn get_pool ( db : & str , user : & str ) -> Option < ConnectionPool > {
636
+ match get_all_pools ( ) . get ( & PoolIdentifier :: new ( & db, & user) ) {
608
637
Some ( pool) => Some ( pool. clone ( ) ) ,
609
638
None => None ,
610
639
}
611
640
}
612
641
613
642
/// Get a pointer to all configured pools.
614
- pub fn get_all_pools ( ) -> HashMap < ( String , String ) , ConnectionPool > {
643
+ pub fn get_all_pools ( ) -> HashMap < PoolIdentifier , ConnectionPool > {
615
644
return ( * ( * POOLS . load ( ) ) ) . clone ( ) ;
616
645
}
617
646
0 commit comments