#![cfg_attr(test, allow(clippy::unwrap_used))]
mod candidates;
mod checkout;
pub mod prepared;
mod refill;
pub mod types;
mod wait;
pub use types::{ConnGuard, Routing};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::Instant;
use arc_swap::ArcSwap;
use parking_lot::Mutex;
use tracing::debug;
use crate::auth::pgpass::Pgpass;
use crate::config::Config;
use crate::errors::ResolveError;
use crate::o11y;
use crate::topology;
pub struct PoolManager {
pub(in crate::pool) inner: Mutex<types::Inner>,
pub(in crate::pool) config: Arc<ArcSwap<Config>>,
pub(in crate::pool) topology: Arc<topology::TopologyManager>,
pub(in crate::pool) pgpass: Arc<Pgpass>,
pub(in crate::pool) replica_rr: AtomicUsize,
pub(crate) stmt_store: Mutex<prepared::StatementStore>,
pub(in crate::pool) shutting_down: AtomicBool,
}
impl PoolManager {
pub fn new(
config: Arc<ArcSwap<Config>>,
topology: Arc<topology::TopologyManager>,
pgpass: Arc<Pgpass>,
) -> Self {
Self {
inner: Mutex::new(types::Inner {
pools: HashMap::new(),
waits: HashMap::new(),
}),
config,
topology,
pgpass,
replica_rr: AtomicUsize::new(0),
stmt_store: Mutex::new(prepared::StatementStore::new()),
shutting_down: AtomicBool::new(false),
}
}
pub fn config(&self) -> &Arc<ArcSwap<Config>> {
&self.config
}
pub fn pgpass(&self) -> &Pgpass {
&self.pgpass
}
pub fn shutdown(&self) {
self.shutting_down.store(true, Ordering::Release);
self.wake_all_waiters();
}
pub fn resolve(&self, database: &str, routing: Routing) -> Result<String, ResolveError> {
let cfg = self.config.load_full();
let (cluster_name, _, _) =
cfg.find_pool(database)
.ok_or_else(|| ResolveError::UnknownDatabase {
database: database.to_owned(),
})?;
match routing {
Routing::Primary => {
self.topology
.primary(cluster_name)
.ok_or_else(|| ResolveError::NoPrimary {
cluster: cluster_name.to_owned(),
})
}
Routing::Replica => {
let replicas = self.topology.replicas(cluster_name);
if replicas.is_empty() {
return Err(ResolveError::NoReplica {
cluster: cluster_name.to_owned(),
});
}
let idx = self.replica_rr.load(Ordering::Relaxed) % replicas.len();
Ok(replicas[idx].clone())
}
}
}
pub(in crate::pool) fn lookup_password(
&self,
addr: &str,
database: &str,
user: &str,
) -> Option<String> {
self.pgpass
.lookup_addr(addr, database, user)
.map(str::to_owned)
}
pub fn drain_node(&self, node: &str) {
let drained = {
let mut inner = self.inner.lock();
let mut drained = 0u32;
for (key, pool) in &mut inner.pools {
if key.node == node {
drained += pool.idle.len() as u32;
pool.idle.clear();
}
}
drained
};
if drained > 0 {
debug!(node, drained, "drained idle connections for node");
}
self.wake_all_waiters();
}
pub fn drain_all(&self) {
let mut inner = self.inner.lock();
let mut drained = 0u32;
for pool in inner.pools.values_mut() {
drained += pool.idle.len() as u32;
pool.idle.clear();
}
if drained > 0 {
debug!(drained, "drained all idle connections for shutdown");
}
}
pub fn pool_stats(&self) -> Vec<(o11y::metrics::PoolKeyInfo, o11y::metrics::PoolStats)> {
let inner = self.inner.lock();
inner
.pools
.iter()
.map(|(key, pool)| {
(
o11y::metrics::PoolKeyInfo {
node: key.node.clone(),
database: key.database.clone(),
user: key.user.clone(),
},
o11y::metrics::PoolStats {
active: pool.active,
idle: pool.idle.len() as u32,
resetting: pool.resetting,
},
)
})
.collect()
}
#[must_use]
pub fn queue_stats(&self) -> Vec<o11y::metrics::QueueInfo> {
let now = Instant::now();
let inner = self.inner.lock();
inner
.waits
.iter()
.filter_map(|(key, queue)| {
let depth = queue.waiters.len();
if depth == 0 {
return None;
}
let oldest = queue.waiters.front().map_or(0.0, |(enqueued_at, _)| {
now.saturating_duration_since(*enqueued_at).as_secs_f64()
});
Some(o11y::metrics::QueueInfo {
database: key.database.clone(),
user: key.user.clone(),
role: key.role,
depth: depth as u32,
oldest_wait_secs: oldest,
})
})
.collect()
}
pub fn pool_limits(&self) -> Vec<o11y::metrics::PoolLimits> {
let cfg = self.config.load_full();
cfg.cluster
.values()
.flat_map(|cluster| {
cluster
.pool
.iter()
.map(|(db_name, pool)| o11y::metrics::PoolLimits {
database: pool.database_name(db_name).to_owned(),
max_primary: pool.max_connections.primary,
max_replica: pool.max_connections.replica,
})
})
.collect()
}
}