orchestra-toolkit 0.6.1

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
*
* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

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_size has to be 0 otherwise channels will reprenish after
            // we call [`crate::SessionAsync::finalize()`]
            .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> {
        // TODO: refuse to connect if a call to [`crate::SessionAsync::finalize()`]
        // is pending or has already been called
        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()
    }
}