#![warn(missing_docs)]
pub use postgres;
pub use r2d2;
use postgres::tls::{MakeTlsConnect, TlsConnect};
use postgres::{Client, Config, Error, Socket};
use r2d2::ManageConnection;
#[derive(Debug)]
pub struct PostgresConnectionManager<T> {
config: Config,
tls_connector: T,
}
impl<T> PostgresConnectionManager<T>
where
T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send,
T::TlsConnect: Send,
T::Stream: Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
pub fn new(config: Config, tls_connector: T) -> PostgresConnectionManager<T> {
PostgresConnectionManager {
config,
tls_connector,
}
}
}
impl<T> ManageConnection for PostgresConnectionManager<T>
where
T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send,
T::TlsConnect: Send,
T::Stream: Send,
<T::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
type Connection = Client;
type Error = Error;
fn connect(&self) -> Result<Client, Error> {
self.config.connect(self.tls_connector.clone())
}
fn is_valid(&self, client: &mut Client) -> Result<(), Error> {
client.simple_query("").map(|_| ())
}
fn has_broken(&self, client: &mut Client) -> bool {
client.is_closed()
}
}