use crate::{error::Error, Config, Connection, ConnectionAsync, ConnectionTls, ConnectionTlsAsync};
const QUERY_SYSCTL_STATUS: &str = "sysctl report status";
pub fn get(pool_size: u32, config: Config) -> Result<r2d2::Pool<ConnectionMgrTcp>, r2d2::Error> {
let mgr = ConnectionMgrTcp::new(config);
r2d2::Pool::builder().max_size(pool_size).build(mgr)
}
pub async fn get_async(
pool_size: u32,
config: Config,
) -> Result<bb8::Pool<ConnectionMgrTcp>, Error> {
let mgr = ConnectionMgrTcp::new(config);
bb8::Pool::builder().max_size(pool_size).build(mgr).await
}
pub fn get_tls(
pool_size: u32,
config: Config,
pem_cert: &str,
) -> Result<r2d2::Pool<ConnectionMgrTls>, r2d2::Error> {
let mgr = ConnectionMgrTls::new(config, pem_cert.into());
r2d2::Pool::builder().max_size(pool_size).build(mgr)
}
pub async fn get_tls_async(
pool_size: u32,
config: Config,
pem_cert: &str,
) -> Result<bb8::Pool<ConnectionMgrTls>, Error> {
let mgr = ConnectionMgrTls::new(config, pem_cert.into());
bb8::Pool::builder().max_size(pool_size).build(mgr).await
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConnectionMgrTcp {
config: Config,
}
impl ConnectionMgrTcp {
pub fn new(config: Config) -> Self {
Self { config }
}
}
impl r2d2::ManageConnection for ConnectionMgrTcp {
type Connection = Connection;
type Error = Error;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.config.connect()
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.query_parse::<()>(&query!(QUERY_SYSCTL_STATUS))
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}
impl bb8::ManageConnection for ConnectionMgrTcp {
type Connection = ConnectionAsync;
type Error = Error;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.config.connect_async().await
}
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.query_parse::<()>(&query!(QUERY_SYSCTL_STATUS)).await
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ConnectionMgrTls {
config: Config,
pem_cert: String,
}
impl ConnectionMgrTls {
pub fn new(config: Config, pem_cert: String) -> Self {
Self { config, pem_cert }
}
}
impl r2d2::ManageConnection for ConnectionMgrTls {
type Connection = ConnectionTls;
type Error = Error;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.config.connect_tls(&self.pem_cert)
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.query_parse::<()>(&query!(QUERY_SYSCTL_STATUS))
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}
impl bb8::ManageConnection for ConnectionMgrTls {
type Connection = ConnectionTlsAsync;
type Error = Error;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.config.connect_tls_async(&self.pem_cert).await
}
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.query_parse::<()>(&query!(QUERY_SYSCTL_STATUS)).await
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}