get_pool_stats

Function get_pool_stats 

Source
pub fn get_pool_stats(
    pool: &Mutex<BrowserPool>,
) -> Result<PoolStatsResponse, PdfServiceError>
Expand description

Get current browser pool statistics.

Returns real-time metrics about the browser pool state including available browsers, active browsers, and total count.

§Thread Safety

This function briefly acquires the pool lock to read statistics. It’s safe to call frequently for monitoring purposes.

§Blocking Behavior

This function blocks briefly (< 1ms typically) while holding the pool lock. It’s generally safe to call from async contexts directly, but for consistency, you may still wrap it in a blocking task.

§Arguments

  • pool - Reference to the mutex-wrapped browser pool

§Returns

  • Ok(PoolStatsResponse) - Current pool statistics
  • Err(PdfServiceError::PoolLockFailed) - If mutex is poisoned

§Examples

§Basic Usage

use html2pdf_api::service::get_pool_stats;

let stats = get_pool_stats(&pool)?;
println!("Available: {}", stats.available);
println!("Active: {}", stats.active);
println!("Total: {}", stats.total);

§Monitoring Integration

use prometheus::{Gauge, register_gauge};

lazy_static! {
    static ref POOL_AVAILABLE: Gauge = register_gauge!(
        "browser_pool_available",
        "Number of available browsers in pool"
    ).unwrap();
    static ref POOL_ACTIVE: Gauge = register_gauge!(
        "browser_pool_active",
        "Number of active browsers in pool"
    ).unwrap();
}

fn update_metrics(pool: &Mutex<BrowserPool>) {
    if let Ok(stats) = get_pool_stats(pool) {
        POOL_AVAILABLE.set(stats.available as f64);
        POOL_ACTIVE.set(stats.active as f64);
    }
}

§Capacity Check

let stats = get_pool_stats(&pool)?;

if stats.available == 0 {
    log::warn!("No browsers available, requests may be delayed");
}

let utilization = stats.active as f64 / stats.total.max(1) as f64;
if utilization > 0.8 {
    log::warn!("Pool utilization at {:.0}%, consider scaling", utilization * 100.0);
}