#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(
nonstandard_style,
rust_2018_idioms,
rustdoc::broken_intra_doc_links,
rustdoc::private_intra_doc_links
)]
#![forbid(non_ascii_idents, unsafe_code)]
#![warn(
deprecated_in_future,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
unreachable_pub,
unused_import_braces,
unused_labels,
unused_lifetimes,
unused_qualifications,
unused_results
)]
mod config;
mod generic_client;
use std::{
borrow::Cow,
collections::HashMap,
fmt,
future::Future,
ops::{Deref, DerefMut},
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, Mutex, RwLock, Weak,
},
};
use deadpool::managed;
#[cfg(not(target_arch = "wasm32"))]
use tokio::spawn;
use tokio::task::JoinHandle;
use tokio_postgres::{
types::Type, Client as PgClient, Config as PgConfig, Error, IsolationLevel, Statement,
Transaction as PgTransaction, TransactionBuilder as PgTransactionBuilder,
};
#[cfg(not(target_arch = "wasm32"))]
use tokio_postgres::{
tls::{MakeTlsConnect, TlsConnect},
Socket,
};
pub use tokio_postgres;
pub use self::config::{
ChannelBinding, Config, ConfigError, ManagerConfig, RecyclingMethod, SslMode,
TargetSessionAttrs,
};
pub use self::generic_client::GenericClient;
pub use deadpool::managed::reexports::*;
deadpool::managed_reexports!(
"tokio_postgres",
Manager,
managed::Object<Manager>,
Error,
ConfigError
);
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub type Client = Object;
type RecycleResult = managed::RecycleResult<Error>;
type RecycleError = managed::RecycleError<Error>;
pub struct Manager {
config: ManagerConfig,
pg_config: PgConfig,
connect: Box<dyn Connect>,
pub statement_caches: StatementCaches,
}
impl Manager {
#[cfg(not(target_arch = "wasm32"))]
pub fn new<T>(pg_config: tokio_postgres::Config, tls: T) -> Self
where
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
T::Stream: Sync + Send,
T::TlsConnect: Sync + Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
Self::from_config(pg_config, tls, ManagerConfig::default())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn from_config<T>(pg_config: tokio_postgres::Config, tls: T, config: ManagerConfig) -> Self
where
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
T::Stream: Sync + Send,
T::TlsConnect: Sync + Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
Self::from_connect(pg_config, ConfigConnectImpl { tls }, config)
}
pub fn from_connect(
pg_config: tokio_postgres::Config,
connect: impl Connect + 'static,
config: ManagerConfig,
) -> Self {
Self {
config,
pg_config,
connect: Box::new(connect),
statement_caches: StatementCaches::default(),
}
}
}
impl fmt::Debug for Manager {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Manager")
.field("config", &self.config)
.field("pg_config", &self.pg_config)
.field("statement_caches", &self.statement_caches)
.finish()
}
}
impl managed::Manager for Manager {
type Type = ClientWrapper;
type Error = Error;
async fn create(&self) -> Result<ClientWrapper, Error> {
let (client, conn_task) = self.connect.connect(&self.pg_config).await?;
let client_wrapper = ClientWrapper::new(client, conn_task);
self.statement_caches
.attach(&client_wrapper.statement_cache);
Ok(client_wrapper)
}
async fn recycle(&self, client: &mut ClientWrapper, _: &Metrics) -> RecycleResult {
if client.is_closed() {
tracing::warn!(target: "deadpool.postgres", "Connection could not be recycled: Connection closed");
return Err(RecycleError::message("Connection closed"));
}
match self.config.recycling_method.query() {
Some(sql) => match client.simple_query(sql).await {
Ok(_) => Ok(()),
Err(e) => {
tracing::warn!(target: "deadpool.postgres", "Connection could not be recycled: {}", e);
Err(e.into())
}
},
None => Ok(()),
}
}
fn detach(&self, object: &mut ClientWrapper) {
self.statement_caches.detach(&object.statement_cache);
}
}
pub trait Connect: Sync + Send {
fn connect(
&self,
pg_config: &PgConfig,
) -> BoxFuture<'_, Result<(PgClient, JoinHandle<()>), Error>>;
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug)]
pub struct ConfigConnectImpl<T>
where
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
T::Stream: Sync + Send,
T::TlsConnect: Sync + Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
pub tls: T,
}
#[cfg(not(target_arch = "wasm32"))]
impl<T> Connect for ConfigConnectImpl<T>
where
T: MakeTlsConnect<Socket> + Clone + Sync + Send + 'static,
T::Stream: Sync + Send,
T::TlsConnect: Sync + Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
fn connect(
&self,
pg_config: &PgConfig,
) -> BoxFuture<'_, Result<(PgClient, JoinHandle<()>), Error>> {
let tls = self.tls.clone();
let pg_config = pg_config.clone();
Box::pin(async move {
let fut = pg_config.connect(tls);
let (client, connection) = fut.await?;
let conn_task = spawn(async move {
if let Err(e) = connection.await {
tracing::warn!(target: "deadpool.postgres", "Connection error: {}", e);
}
});
Ok((client, conn_task))
})
}
}
#[derive(Default, Debug)]
pub struct StatementCaches {
caches: Mutex<Vec<Weak<StatementCache>>>,
}
impl StatementCaches {
fn attach(&self, cache: &Arc<StatementCache>) {
let cache = Arc::downgrade(cache);
self.caches.lock().unwrap().push(cache);
}
fn detach(&self, cache: &Arc<StatementCache>) {
let cache = Arc::downgrade(cache);
self.caches.lock().unwrap().retain(|sc| !sc.ptr_eq(&cache));
}
pub fn clear(&self) {
let caches = self.caches.lock().unwrap();
for cache in caches.iter() {
if let Some(cache) = cache.upgrade() {
cache.clear();
}
}
}
pub fn remove(&self, query: &str, types: &[Type]) {
let caches = self.caches.lock().unwrap();
for cache in caches.iter() {
if let Some(cache) = cache.upgrade() {
drop(cache.remove(query, types));
}
}
}
}
impl fmt::Debug for StatementCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientWrapper")
.field("size", &self.size)
.finish()
}
}
#[derive(Debug, Eq, Hash, PartialEq)]
struct StatementCacheKey<'a> {
query: Cow<'a, str>,
types: Cow<'a, [Type]>,
}
pub struct StatementCache {
map: RwLock<HashMap<StatementCacheKey<'static>, Statement>>,
size: AtomicUsize,
}
impl StatementCache {
fn new() -> Self {
Self {
map: RwLock::new(HashMap::new()),
size: AtomicUsize::new(0),
}
}
pub fn size(&self) -> usize {
self.size.load(Ordering::Relaxed)
}
pub fn clear(&self) {
let mut map = self.map.write().unwrap();
map.clear();
self.size.store(0, Ordering::Relaxed);
}
pub fn remove(&self, query: &str, types: &[Type]) -> Option<Statement> {
let key = StatementCacheKey {
query: Cow::Owned(query.to_owned()),
types: Cow::Owned(types.to_owned()),
};
let mut map = self.map.write().unwrap();
let removed = map.remove(&key);
if removed.is_some() {
let _ = self.size.fetch_sub(1, Ordering::Relaxed);
}
removed
}
fn get(&self, query: &str, types: &[Type]) -> Option<Statement> {
let key = StatementCacheKey {
query: Cow::Borrowed(query),
types: Cow::Borrowed(types),
};
self.map.read().unwrap().get(&key).map(ToOwned::to_owned)
}
fn insert(&self, query: &str, types: &[Type], stmt: Statement) {
let key = StatementCacheKey {
query: Cow::Owned(query.to_owned()),
types: Cow::Owned(types.to_owned()),
};
let mut map = self.map.write().unwrap();
if map.insert(key, stmt).is_none() {
let _ = self.size.fetch_add(1, Ordering::Relaxed);
}
}
pub async fn prepare(&self, client: &PgClient, query: &str) -> Result<Statement, Error> {
self.prepare_typed(client, query, &[]).await
}
pub async fn prepare_typed(
&self,
client: &PgClient,
query: &str,
types: &[Type],
) -> Result<Statement, Error> {
match self.get(query, types) {
Some(statement) => Ok(statement),
None => {
let stmt = client.prepare_typed(query, types).await?;
self.insert(query, types, stmt.clone());
Ok(stmt)
}
}
}
}
#[derive(Debug)]
pub struct ClientWrapper {
client: PgClient,
conn_task: JoinHandle<()>,
pub statement_cache: Arc<StatementCache>,
}
impl ClientWrapper {
#[must_use]
pub fn new(client: PgClient, conn_task: JoinHandle<()>) -> Self {
Self {
client,
conn_task,
statement_cache: Arc::new(StatementCache::new()),
}
}
pub async fn prepare_cached(&self, query: &str) -> Result<Statement, Error> {
self.statement_cache.prepare(&self.client, query).await
}
pub async fn prepare_typed_cached(
&self,
query: &str,
types: &[Type],
) -> Result<Statement, Error> {
self.statement_cache
.prepare_typed(&self.client, query, types)
.await
}
#[allow(unused_lifetimes)] pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
Ok(Transaction {
txn: PgClient::transaction(&mut self.client).await?,
statement_cache: self.statement_cache.clone(),
})
}
pub fn build_transaction(&mut self) -> TransactionBuilder<'_> {
TransactionBuilder {
builder: self.client.build_transaction(),
statement_cache: self.statement_cache.clone(),
}
}
}
impl Deref for ClientWrapper {
type Target = PgClient;
fn deref(&self) -> &PgClient {
&self.client
}
}
impl DerefMut for ClientWrapper {
fn deref_mut(&mut self) -> &mut PgClient {
&mut self.client
}
}
impl Drop for ClientWrapper {
fn drop(&mut self) {
self.conn_task.abort()
}
}
pub struct Transaction<'a> {
txn: PgTransaction<'a>,
pub statement_cache: Arc<StatementCache>,
}
impl<'a> Transaction<'a> {
pub async fn prepare_cached(&self, query: &str) -> Result<Statement, Error> {
self.statement_cache.prepare(self.client(), query).await
}
pub async fn prepare_typed_cached(
&self,
query: &str,
types: &[Type],
) -> Result<Statement, Error> {
self.statement_cache
.prepare_typed(self.client(), query, types)
.await
}
pub async fn commit(self) -> Result<(), Error> {
self.txn.commit().await
}
pub async fn rollback(self) -> Result<(), Error> {
self.txn.rollback().await
}
#[allow(unused_lifetimes)] pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
Ok(Transaction {
txn: PgTransaction::transaction(&mut self.txn).await?,
statement_cache: self.statement_cache.clone(),
})
}
#[allow(unused_lifetimes)] pub async fn savepoint<I>(&mut self, name: I) -> Result<Transaction<'_>, Error>
where
I: Into<String>,
{
Ok(Transaction {
txn: PgTransaction::savepoint(&mut self.txn, name).await?,
statement_cache: self.statement_cache.clone(),
})
}
}
impl<'a> fmt::Debug for Transaction<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Transaction")
.field("statement_cache", &self.statement_cache)
.finish()
}
}
impl<'a> Deref for Transaction<'a> {
type Target = PgTransaction<'a>;
fn deref(&self) -> &PgTransaction<'a> {
&self.txn
}
}
impl<'a> DerefMut for Transaction<'a> {
fn deref_mut(&mut self) -> &mut PgTransaction<'a> {
&mut self.txn
}
}
#[must_use = "builder does nothing itself, use `.start()` to use it"]
pub struct TransactionBuilder<'a> {
builder: PgTransactionBuilder<'a>,
statement_cache: Arc<StatementCache>,
}
impl<'a> TransactionBuilder<'a> {
pub fn isolation_level(self, isolation_level: IsolationLevel) -> Self {
Self {
builder: self.builder.isolation_level(isolation_level),
statement_cache: self.statement_cache,
}
}
pub fn read_only(self, read_only: bool) -> Self {
Self {
builder: self.builder.read_only(read_only),
statement_cache: self.statement_cache,
}
}
pub fn deferrable(self, deferrable: bool) -> Self {
Self {
builder: self.builder.deferrable(deferrable),
statement_cache: self.statement_cache,
}
}
pub async fn start(self) -> Result<Transaction<'a>, Error> {
Ok(Transaction {
txn: self.builder.start().await?,
statement_cache: self.statement_cache,
})
}
}
impl<'a> fmt::Debug for TransactionBuilder<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TransactionBuilder")
.field("statement_cache", &self.statement_cache)
.finish()
}
}
impl<'a> Deref for TransactionBuilder<'a> {
type Target = PgTransactionBuilder<'a>;
fn deref(&self) -> &Self::Target {
&self.builder
}
}
impl<'a> DerefMut for TransactionBuilder<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.builder
}
}