secra-database 0.1.0

基于 SeaORM 的 Rust 数据库连接和管理库
Documentation
//! 连接池监控示例
//!
//! 演示如何监控连接池的状态

use secra_database::{DatabaseConfig, DatabaseService, ConnectionPoolService};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = DatabaseConfig {
        database_type: "postgres".to_string(),
        host: "localhost".to_string(),
        port: 5432,
        username: "postgres".to_string(),
        password: "password".to_string(),
        database_name: "mydb".to_string(),
        schema: "public".to_string(),
        logging_level: "info".to_string(),
        use_pgbouncer: false,
    };

    println!("创建数据库连接...");
    let db = DatabaseService::init(&config, None).await?;

    println!("\n获取连接池统计信息...");
    // 从配置中获取连接池参数(这里使用示例值)
    let max_connections = Some(100);
    let min_connections = Some(5);
    
    match ConnectionPoolService::get_pool_stats(&db, max_connections, min_connections).await {
        Ok(stats) => {
            println!("连接池统计信息:");
            println!("  最大连接数: {}", stats.max_connections);
            println!("  最小连接数: {}", stats.min_connections);
            if let Some(active) = stats.active_connections {
                println!("  活跃连接数: {}", active);
            }
            if let Some(idle) = stats.idle_connections {
                println!("  空闲连接数: {}", idle);
            }
            if let Some(waiting) = stats.waiting_requests {
                println!("  等待请求数: {}", waiting);
            }
        }
        Err(e) => {
            println!("获取连接池统计信息失败: {}", e);
        }
    }

    Ok(())
}