Skip to main content

hdbconnect_mcp/
pool.rs

1use std::time::Duration;
2
3use deadpool::managed::{self, Metrics, RecycleResult};
4use hdbconnect_async::Connection;
5
6pub type Pool = managed::Pool<ConnectionManager>;
7pub type PooledConnection = managed::Object<ConnectionManager>;
8
9#[derive(Debug)]
10pub struct ConnectionManager {
11    url: String,
12}
13
14impl ConnectionManager {
15    pub const fn new(url: String) -> Self {
16        Self { url }
17    }
18}
19
20impl managed::Manager for ConnectionManager {
21    type Type = Connection;
22    type Error = hdbconnect::HdbError;
23
24    async fn create(&self) -> Result<Connection, hdbconnect::HdbError> {
25        Connection::new(self.url.clone()).await
26    }
27
28    async fn recycle(&self, _conn: &mut Connection, _: &Metrics) -> RecycleResult<Self::Error> {
29        // Skip connection validation during recycle.
30        // Connection errors will be caught on actual query execution.
31        Ok(())
32    }
33}
34
35pub fn create_pool(url: String, max_size: usize) -> Pool {
36    Pool::builder(ConnectionManager::new(url))
37        .max_size(max_size)
38        .wait_timeout(Some(Duration::from_secs(10)))
39        .create_timeout(Some(Duration::from_secs(30)))
40        .recycle_timeout(Some(Duration::from_secs(5)))
41        .runtime(deadpool::Runtime::Tokio1)
42        .build()
43        .expect("Failed to create connection pool")
44}