use async_trait::async_trait;
use crate::{hgtp::HGTPStream, SessionConfig};
use std::{ops::Deref, time::Duration};
#[derive(Clone)]
pub struct HGTPPool(bb8::Pool<PoolManager>);
impl Deref for HGTPPool {
type Target = bb8::Pool<PoolManager>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl HGTPPool {
pub async fn new(config: SessionConfig) -> anyhow::Result<Self> {
let manager = PoolManager { config };
let pool = bb8::Pool::builder()
.max_size(64)
.min_idle(None)
.connection_timeout(Duration::from_secs(9999))
.test_on_check_out(false)
.build(manager)
.await?;
Ok(HGTPPool(pool))
}
}
pub struct PoolManager {
pub config: SessionConfig,
}
#[async_trait]
impl bb8::ManageConnection for PoolManager {
type Connection = HGTPStream;
type Error = anyhow::Error;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
HGTPStream::new(
self.config.pem_filepath.clone(),
&self.config.address,
self.config.port,
)
.await
}
async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
Ok(())
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
conn.closed()
}
}