@@ -19,16 +19,43 @@ use crate::server::Server;
1919use crate :: sharding:: ShardingFunction ;
2020use crate :: stats:: { get_reporter, Reporter } ;
2121
22+ pub type ProcessId = i32 ;
23+ pub type SecretKey = i32 ;
24+ pub type ServerHost = String ;
25+ pub type ServerPort = u16 ;
26+
2227pub 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 > ;
2531/// The connection pool, globally available.
2632/// This is atomic and safe and read-optimized.
2733/// The pool is recreated dynamically when the config is reloaded.
2834pub static POOLS : Lazy < ArcSwap < PoolMap > > = Lazy :: new ( || ArcSwap :: from_pointee ( HashMap :: default ( ) ) ) ;
2935static POOLS_HASH : Lazy < ArcSwap < HashSet < crate :: config:: Pool > > > =
3036 Lazy :: new ( || ArcSwap :: from_pointee ( HashSet :: default ( ) ) ) ;
3137
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+
3259/// Pool settings.
3360#[ derive( Clone , Debug ) ]
3461pub struct PoolSettings {
@@ -113,14 +140,16 @@ impl ConnectionPool {
113140 // If the pool hasn't changed, get existing reference and insert it into the new_pools.
114141 // We replace all pools at the end, but if the reference is kept, the pool won't get re-created (bb8).
115142 if !changed {
116- match get_pool ( pool_name. clone ( ) , user. username . clone ( ) ) {
143+ match get_pool ( & pool_name, & user. username ) {
117144 Some ( pool) => {
118145 info ! (
119146 "[pool: {}][user: {}] has not changed" ,
120147 pool_name, user. username
121148 ) ;
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+ ) ;
124153 continue ;
125154 }
126155 None => ( ) ,
@@ -239,7 +268,7 @@ impl ConnectionPool {
239268 } ;
240269
241270 // 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) ;
243272 }
244273 }
245274
@@ -603,15 +632,15 @@ impl ManageConnection for ServerPool {
603632}
604633
605634/// 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) ) {
608637 Some ( pool) => Some ( pool. clone ( ) ) ,
609638 None => None ,
610639 }
611640}
612641
613642/// 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 > {
615644 return ( * ( * POOLS . load ( ) ) ) . clone ( ) ;
616645}
617646
0 commit comments