Skip to content

Fix the pool fix #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use arc_swap::ArcSwap;
use log::{error, info};
use once_cell::sync::Lazy;
use serde_derive::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::Hash;
use std::path::Path;
use std::sync::Arc;
use tokio::fs::File;
Expand Down Expand Up @@ -250,7 +250,7 @@ impl ToString for PoolMode {
}
}

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

pub sharding_function: String,
pub shards: HashMap<String, Shard>,
pub users: HashMap<String, User>,
}

impl Hash for Pool {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pool_mode.hash(state);
self.default_role.hash(state);
self.query_parser_enabled.hash(state);
self.primary_reads_enabled.hash(state);
self.sharding_function.hash(state);
self.connect_timeout.hash(state);

for (key, value) in &self.shards {
key.hash(state);
value.hash(state);
}

for (key, value) in &self.users {
key.hash(state);
value.hash(state);
}
}
pub shards: BTreeMap<String, Shard>,
pub users: BTreeMap<String, User>,
}

impl Pool {
Expand All @@ -302,8 +281,8 @@ impl Default for Pool {
fn default() -> Pool {
Pool {
pool_mode: Pool::default_pool_mode(),
shards: HashMap::from([(String::from("1"), Shard::default())]),
users: HashMap::default(),
shards: BTreeMap::from([(String::from("1"), Shard::default())]),
users: BTreeMap::default(),
default_role: String::from("any"),
query_parser_enabled: false,
primary_reads_enabled: false,
Expand Down
22 changes: 17 additions & 5 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,25 @@ impl ConnectionPool {
for (pool_name, pool_config) in &config.pools {
let changed = pools_hash.insert(pool_config.clone());

if !changed {
info!("[db: {}] has not changed", pool_name);
continue;
}

// There is one pool per database/user pair.
for (_, user) in &pool_config.users {
// If the pool hasn't changed, get existing reference and insert it into the new_pools.
// We replace all pools at the end, but if the reference is kept, the pool won't get re-created (bb8).
if !changed {
match get_pool(pool_name.clone(), user.username.clone()) {
Some(pool) => {
info!(
"[pool: {}][user: {}] has not changed",
pool_name, user.username
);
new_pools
.insert((pool_name.clone(), user.username.clone()), pool.clone());
continue;
}
None => (),
}
}

info!(
"[pool: {}][user: {}] creating new pool",
pool_name, user.username
Expand Down