mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-23 01:16:30 +00:00
@@ -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;
|
||||
@@ -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,
|
||||
@@ -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 {
|
||||
@@ -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,
|
||||
|
||||
22
src/pool.rs
22
src/pool.rs
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user