Healthcheck

Trait Healthcheck 

Source
pub trait Healthcheck: Send + Sync {
    // Required method
    fn ping(&self) -> Result<()>;
}
Expand description

Trait for browser-like objects that support health checking.

Implementors must provide a ping() method that verifies the browser is still functional and responsive.

§Thread Safety

This trait requires Send + Sync because browsers may be health-checked from a background thread while being used from another thread.

§Example Implementation

use html2pdf_api::{Healthcheck, Result, BrowserPoolError};

struct MyBrowser {
    inner: SomeBrowserType,
}

impl Healthcheck for MyBrowser {
    fn ping(&self) -> Result<()> {
        // Try to create a tab to verify browser is responsive
        let tab = self.inner.new_tab()
            .map_err(|e| BrowserPoolError::HealthCheckFailed(e.to_string()))?;
         
        // Clean up
        let _ = tab.close();
         
        Ok(())
    }
}

§How It’s Used

The browser pool’s keep-alive thread calls ping() on all active browsers at regular intervals (configured via ping_interval).

Keep-Alive Thread
      │
      ├─── ping() ──→ Browser 1 ──→ ✓ OK
      │
      ├─── ping() ──→ Browser 2 ──→ ✗ Failed (count: 1)
      │
      └─── ping() ──→ Browser 3 ──→ ✓ OK

After max_ping_failures consecutive failures, the browser is removed and replaced.

Required Methods§

Source

fn ping(&self) -> Result<()>

Perform a health check on the browser.

Should perform a lightweight operation like creating/closing a tab to verify the browser process is still responsive.

§Implementation Guidelines
  • Keep it fast: Health checks run frequently; avoid heavy operations
  • Don’t hold locks: Release any locks before performing I/O
  • Be idempotent: Multiple calls should be safe
  • Clean up: Close any tabs or resources created during the check
§Errors

Returns BrowserPoolError::HealthCheckFailed if the health check fails (browser unresponsive or crashed).

§Example
use html2pdf_api::{Healthcheck, Result};

fn check_browser_health<T: Healthcheck>(browser: &T) -> Result<()> {
    browser.ping()?;
    println!("Browser is healthy!");
    Ok(())
}

Implementors§