use std::time::Duration;
use hyperdb_api::pool::{create_pool, PoolConfig};
use hyperdb_api::{CreateMode, HyperProcess, Result};
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter("hyperdb_api=debug")
.init();
println!("=== Connection Pool Example ===\n");
std::fs::create_dir_all("test_results")?;
use hyperdb_api::Parameters;
let mut params = Parameters::new();
params.set("log_dir", "test_results");
let hyper = HyperProcess::new(None, Some(¶ms))?;
let endpoint = hyper.require_endpoint()?.to_string();
println!("Started Hyper server at: {endpoint}");
let config = PoolConfig::new(&endpoint, "test_results/connection_pool.hyper")
.create_mode(CreateMode::CreateAndReplace)
.max_size(4);
let pool = create_pool(config)?;
println!("Created connection pool with max_size=4");
{
let conn = pool
.get()
.await
.map_err(|e| hyperdb_api::Error::new(e.to_string()))?;
conn.execute_command(
"CREATE TABLE counters (
id INT NOT NULL,
name TEXT NOT NULL,
value INT DEFAULT 0
)",
)
.await?;
println!("Created counters table");
for i in 1..=10 {
conn.execute_command(&format!(
"INSERT INTO counters (id, name, value) VALUES ({i}, 'counter_{i}', 0)"
))
.await?;
}
println!("Inserted 10 counter rows");
}
println!("\nSpawning 8 concurrent tasks...");
let mut handles = Vec::new();
for task_id in 0..8 {
let pool = pool.clone();
let handle = tokio::spawn(async move {
let conn = pool.get().await.expect("Failed to get connection");
tokio::time::sleep(Duration::from_millis(100)).await;
let counter_id = (task_id % 10) + 1;
conn.execute_command(&format!(
"UPDATE counters SET value = value + 1 WHERE id = {counter_id}"
))
.await
.expect("Failed to update counter");
println!("Task {task_id} updated counter {counter_id}");
});
handles.push(handle);
}
for handle in handles {
handle.await.expect("Task panicked");
}
println!("\nAll tasks completed!");
println!("Pool status: {} connections in use", pool.status().size);
Ok(())
}