Skip to content

Commit d37df43

Browse files
authored
Reduces the amount of time the get_pool operation takes (#625)
* Reduces the amount of time the get_pool operation takes * trigger build * Fix admin
1 parent 2c7bf52 commit d37df43

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ lcov.info
1010
dev/.bash_history
1111
dev/cache
1212
!dev/cache/.keepme
13+
.venv

src/client.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ where
542542
}
543543
// Authenticate normal user.
544544
else {
545-
let mut pool = match get_pool(pool_name, username) {
545+
let pool = match get_pool(pool_name, username) {
546546
Some(pool) => pool,
547547
None => {
548548
error_response(
@@ -800,6 +800,18 @@ where
800800
&self.pool_name,
801801
);
802802

803+
// Get a pool instance referenced by the most up-to-date
804+
// pointer. This ensures we always read the latest config
805+
// when starting a query.
806+
let mut pool = if self.admin {
807+
// Admin clients do not use pools.
808+
ConnectionPool::default()
809+
} else {
810+
self.get_pool().await?
811+
};
812+
813+
query_router.update_pool_settings(&pool.settings);
814+
803815
// Our custom protocol loop.
804816
// We expect the client to either start a transaction with regular queries
805817
// or issue commands for our sharding and server selection protocol.
@@ -853,12 +865,6 @@ where
853865
continue;
854866
}
855867

856-
// Get a pool instance referenced by the most up-to-date
857-
// pointer. This ensures we always read the latest config
858-
// when starting a query.
859-
let mut pool = self.get_pool().await?;
860-
query_router.update_pool_settings(pool.settings.clone());
861-
862868
let mut initial_parsed_ast = None;
863869

864870
match message[0] as char {
@@ -990,12 +996,11 @@ where
990996
};
991997

992998
// Check if the pool is paused and wait until it's resumed.
993-
if pool.wait_paused().await {
994-
// Refresh pool information, something might have changed.
995-
pool = self.get_pool().await?;
996-
}
999+
pool.wait_paused().await;
9971000

998-
query_router.update_pool_settings(pool.settings.clone());
1001+
// Refresh pool information, something might have changed.
1002+
pool = self.get_pool().await?;
1003+
query_router.update_pool_settings(&pool.settings);
9991004

10001005
let current_shard = query_router.shard();
10011006

src/pool.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ impl Default for PoolSettings {
190190
#[derive(Clone, Debug, Default)]
191191
pub struct ConnectionPool {
192192
/// The pools handled internally by bb8.
193-
databases: Vec<Vec<Pool<ServerPool>>>,
193+
databases: Arc<Vec<Vec<Pool<ServerPool>>>>,
194194

195195
/// The addresses (host, port, role) to handle
196196
/// failover and load balancing deterministically.
197-
addresses: Vec<Vec<Address>>,
197+
addresses: Arc<Vec<Vec<Address>>>,
198198

199199
/// List of banned addresses (see above)
200200
/// that should not be queried.
@@ -206,7 +206,7 @@ pub struct ConnectionPool {
206206
original_server_parameters: Arc<RwLock<ServerParameters>>,
207207

208208
/// Pool configuration.
209-
pub settings: PoolSettings,
209+
pub settings: Arc<PoolSettings>,
210210

211211
/// If not validated, we need to double check the pool is available before allowing a client
212212
/// to use it.
@@ -445,13 +445,13 @@ impl ConnectionPool {
445445
}
446446

447447
let pool = ConnectionPool {
448-
databases: shards,
449-
addresses,
448+
databases: Arc::new(shards),
449+
addresses: Arc::new(addresses),
450450
banlist: Arc::new(RwLock::new(banlist)),
451451
config_hash: new_pool_hash_value,
452452
original_server_parameters: Arc::new(RwLock::new(ServerParameters::new())),
453453
auth_hash: pool_auth_hash,
454-
settings: PoolSettings {
454+
settings: Arc::new(PoolSettings {
455455
pool_mode: match user.pool_mode {
456456
Some(pool_mode) => pool_mode,
457457
None => pool_config.pool_mode,
@@ -494,7 +494,7 @@ impl ConnectionPool {
494494
Some(ref plugins) => Some(plugins.clone()),
495495
None => config.plugins.clone(),
496496
},
497-
},
497+
}),
498498
validated: Arc::new(AtomicBool::new(false)),
499499
paused: Arc::new(AtomicBool::new(false)),
500500
paused_waiter: Arc::new(Notify::new()),
@@ -504,7 +504,7 @@ impl ConnectionPool {
504504
// before setting it globally.
505505
// Do this async and somewhere else, we don't have to wait here.
506506
if config.general.validate_config {
507-
let mut validate_pool = pool.clone();
507+
let validate_pool = pool.clone();
508508
tokio::task::spawn(async move {
509509
let _ = validate_pool.validate().await;
510510
});
@@ -525,7 +525,7 @@ impl ConnectionPool {
525525
/// when they connect.
526526
/// This also warms up the pool for clients that connect when
527527
/// the pooler starts up.
528-
pub async fn validate(&mut self) -> Result<(), Error> {
528+
pub async fn validate(&self) -> Result<(), Error> {
529529
let mut futures = Vec::new();
530530
let validated = Arc::clone(&self.validated);
531531

src/query_router.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ impl QueryRouter {
128128
}
129129

130130
/// Pool settings can change because of a config reload.
131-
pub fn update_pool_settings(&mut self, pool_settings: PoolSettings) {
132-
self.pool_settings = pool_settings;
131+
pub fn update_pool_settings(&mut self, pool_settings: &PoolSettings) {
132+
self.pool_settings = pool_settings.clone();
133133
}
134134

135135
pub fn pool_settings(&self) -> &PoolSettings {
@@ -1403,7 +1403,7 @@ mod test {
14031403
assert_eq!(qr.primary_reads_enabled, None);
14041404

14051405
// Internal state must not be changed due to this, only defaults
1406-
qr.update_pool_settings(pool_settings.clone());
1406+
qr.update_pool_settings(&pool_settings);
14071407

14081408
assert_eq!(qr.active_role, None);
14091409
assert_eq!(qr.active_shard, None);
@@ -1476,7 +1476,7 @@ mod test {
14761476
};
14771477

14781478
let mut qr = QueryRouter::new();
1479-
qr.update_pool_settings(pool_settings);
1479+
qr.update_pool_settings(&pool_settings);
14801480

14811481
// Shard should start out unset
14821482
assert_eq!(qr.active_shard, None);
@@ -1860,7 +1860,7 @@ mod test {
18601860
..Default::default()
18611861
};
18621862
let mut qr = QueryRouter::new();
1863-
qr.update_pool_settings(pool_settings);
1863+
qr.update_pool_settings(&pool_settings);
18641864

18651865
let query = simple_query("SELECT * FROM pg_database");
18661866
let ast = qr.parse(&query).unwrap();

0 commit comments

Comments
 (0)