Skip to content

Commit 19fd677

Browse files
authored
Fix the pool fix (#176)
* Always listen to the compiler * Its fine
1 parent 964a5e1 commit 19fd677

File tree

2 files changed

+24
-33
lines changed

2 files changed

+24
-33
lines changed

src/config.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use arc_swap::ArcSwap;
33
use log::{error, info};
44
use once_cell::sync::Lazy;
55
use serde_derive::{Deserialize, Serialize};
6-
use std::collections::{HashMap, HashSet};
7-
use std::hash::{Hash, Hasher};
6+
use std::collections::{BTreeMap, HashMap, HashSet};
7+
use std::hash::Hash;
88
use std::path::Path;
99
use std::sync::Arc;
1010
use tokio::fs::File;
@@ -250,7 +250,7 @@ impl ToString for PoolMode {
250250
}
251251
}
252252

253-
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
253+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
254254
pub struct Pool {
255255
#[serde(default = "Pool::default_pool_mode")]
256256
pub pool_mode: PoolMode,
@@ -267,29 +267,8 @@ pub struct Pool {
267267
pub connect_timeout: u64,
268268

269269
pub sharding_function: String,
270-
pub shards: HashMap<String, Shard>,
271-
pub users: HashMap<String, User>,
272-
}
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-
}
270+
pub shards: BTreeMap<String, Shard>,
271+
pub users: BTreeMap<String, User>,
293272
}
294273

295274
impl Pool {
@@ -302,8 +281,8 @@ impl Default for Pool {
302281
fn default() -> Pool {
303282
Pool {
304283
pool_mode: Pool::default_pool_mode(),
305-
shards: HashMap::from([(String::from("1"), Shard::default())]),
306-
users: HashMap::default(),
284+
shards: BTreeMap::from([(String::from("1"), Shard::default())]),
285+
users: BTreeMap::default(),
307286
default_role: String::from("any"),
308287
query_parser_enabled: false,
309288
primary_reads_enabled: false,

src/pool.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,25 @@ impl ConnectionPool {
108108
for (pool_name, pool_config) in &config.pools {
109109
let changed = pools_hash.insert(pool_config.clone());
110110

111-
if !changed {
112-
info!("[db: {}] has not changed", pool_name);
113-
continue;
114-
}
115-
116111
// There is one pool per database/user pair.
117112
for (_, user) in &pool_config.users {
113+
// If the pool hasn't changed, get existing reference and insert it into the new_pools.
114+
// We replace all pools at the end, but if the reference is kept, the pool won't get re-created (bb8).
115+
if !changed {
116+
match get_pool(pool_name.clone(), user.username.clone()) {
117+
Some(pool) => {
118+
info!(
119+
"[pool: {}][user: {}] has not changed",
120+
pool_name, user.username
121+
);
122+
new_pools
123+
.insert((pool_name.clone(), user.username.clone()), pool.clone());
124+
continue;
125+
}
126+
None => (),
127+
}
128+
}
129+
118130
info!(
119131
"[pool: {}][user: {}] creating new pool",
120132
pool_name, user.username

0 commit comments

Comments
 (0)