#![allow(clippy::result_large_err)]
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use rustls::ClientConfig;
use tokio::sync::{Semaphore, SemaphorePermit};
use crate::pg::wire::{AuthPolicy, ConnectConfig, Connection, WireError};
use crate::typeinfo::TypeCache;
pub struct Pool {
name: String,
connect: ConnectConfig,
auth: AuthPolicy,
tls: Option<Arc<ClientConfig>>,
permits: Semaphore,
idle: Mutex<Vec<Connection>>,
types: TypeCache,
closed: AtomicBool,
superuser_checked: AtomicBool,
}
impl Pool {
pub fn new(connect: ConnectConfig, max_connections: usize) -> Pool {
Pool::with_auth(connect, max_connections, AuthPolicy::default())
}
pub fn with_auth(connect: ConnectConfig, max_connections: usize, auth: AuthPolicy) -> Pool {
Pool {
name: String::new(),
connect,
auth,
tls: None,
permits: Semaphore::new(max_connections),
idle: Mutex::new(Vec::new()),
types: TypeCache::new(),
closed: AtomicBool::new(false),
superuser_checked: AtomicBool::new(false),
}
}
pub fn with_name(mut self, name: &str) -> Pool {
self.name = name.to_owned();
self
}
pub fn with_tls(mut self, tls: Option<Arc<ClientConfig>>) -> Pool {
self.tls = tls;
self
}
pub async fn checkout(&self, wait: Duration) -> Result<Lease<'_>, PoolError> {
let permit = self.acquire(wait).await?;
let idle = self.idle.lock().unwrap_or_else(|e| e.into_inner()).pop();
match idle {
Some(conn) => Ok(Lease {
pool: self,
_permit: permit,
conn,
reused: true,
}),
None => self.open(permit).await,
}
}
pub(crate) async fn checkout_fresh(&self, wait: Duration) -> Result<Lease<'_>, PoolError> {
let permit = self.acquire(wait).await?;
self.open(permit).await
}
pub fn type_cache(&self) -> &TypeCache {
&self.types
}
pub fn idle_count(&self) -> usize {
self.idle.lock().unwrap_or_else(|e| e.into_inner()).len()
}
pub async fn close(&self) {
self.closed.store(true, Ordering::SeqCst);
self.permits.close();
let idle = std::mem::take(&mut *self.idle.lock().unwrap_or_else(|e| e.into_inner()));
for conn in idle {
conn.close().await;
}
}
async fn acquire(&self, wait: Duration) -> Result<SemaphorePermit<'_>, PoolError> {
if self.closed.load(Ordering::SeqCst) {
return Err(PoolError::Closed);
}
match tokio::time::timeout(wait, self.permits.acquire()).await {
Err(_elapsed) => Err(PoolError::Busy),
Ok(Err(_closed)) => Err(PoolError::Closed),
Ok(Ok(permit)) => Ok(permit),
}
}
async fn open<'a>(&'a self, permit: SemaphorePermit<'a>) -> Result<Lease<'a>, PoolError> {
let conn =
match Connection::connect_with_tls(&self.connect, &self.auth, self.tls.clone()).await {
Ok(conn) => {
self.check_superuser_once(&conn);
conn
}
Err(error) => {
let reason = connect_failure_text(&error);
tracing::warn!(
connection = %self.name,
host = %self.connect.host,
port = self.connect.port,
reason = %crate::scrub::scrub(&reason),
error = %crate::scrub::scrub(&log_cause(&error, &self.connect.user)),
"connection failed"
);
return Err(PoolError::Connect(reason));
}
};
Ok(Lease {
pool: self,
_permit: permit,
conn,
reused: false,
})
}
fn check_superuser_once(&self, conn: &Connection) {
if self.superuser_checked.swap(true, Ordering::SeqCst) {
return;
}
if conn.parameter("is_superuser") == Some("on") {
tracing::warn!(
"database \"{}\": the role is a superuser; the read-only transaction is then the only barrier against the functions it may call. Use a least-privilege role.",
self.name
);
}
}
fn give_back(&self, conn: Connection) {
let mut idle = self.idle.lock().unwrap_or_else(|e| e.into_inner());
if !self.closed.load(Ordering::SeqCst) {
idle.push(conn);
}
}
}
pub struct Lease<'a> {
pool: &'a Pool,
_permit: SemaphorePermit<'a>,
conn: Connection,
reused: bool,
}
impl Lease<'_> {
pub fn conn(&mut self) -> &mut Connection {
&mut self.conn
}
pub(crate) fn reused(&self) -> bool {
self.reused
}
pub fn release(self, clean: bool) {
let Lease {
pool,
_permit,
conn,
reused: _,
} = self;
if clean && !conn.is_poisoned() {
pool.give_back(conn);
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum PoolError {
#[error("all connections are busy")]
Busy,
#[error("the pool is closed")]
Closed,
#[error("{0}")]
Connect(String),
}
fn log_cause(error: &WireError, user: &str) -> String {
match error {
WireError::Db(db) if !user.is_empty() => format!(
"{} (SQLSTATE {})",
db.message.replace(user, "<user>"),
db.code
),
WireError::Db(db) => format!("{} (SQLSTATE {})", db.message, db.code),
other => other.to_string(),
}
}
fn connect_failure_text(error: &WireError) -> String {
match error {
WireError::Auth(_) => "authentication failed".to_owned(),
WireError::Db(db) => match db.code.as_str() {
code if code.starts_with("28") => "authentication failed".to_owned(),
"3D000" => "the database does not exist".to_owned(),
"53300" => "too many connections".to_owned(),
code => format!("the server refused the connection (SQLSTATE {code})"),
},
WireError::Io(_) => "cannot connect to the server".to_owned(),
WireError::Tls(_) => "TLS negotiation failed".to_owned(),
WireError::Timeout => "connection timed out".to_owned(),
WireError::Protocol(_) | WireError::Refused(_) => {
"unexpected reply from the server".to_owned()
}
}
}