1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Health check trait for browser instances.
//!
//! This module provides the [`Healthcheck`] trait, which defines how
//! browser instances verify they are still functional and responsive.
//!
//! # Overview
//!
//! The browser pool periodically pings active browsers to detect failures.
//! When a browser fails too many consecutive health checks, it is removed
//! from the pool and replaced.
//!
//! # Default Implementation
//!
//! [`TrackedBrowser`](crate::TrackedBrowser) implements this trait by:
//! 1. Creating a new tab
//! 2. Closing the tab
//! 3. Updating the last ping timestamp
//!
//! This is a lightweight operation that verifies the browser process
//! is alive and the CDP (Chrome DevTools Protocol) connection works.
use crateResult;
/// Trait for browser-like objects that support health checking.
///
/// Implementors must provide a [`ping()`](Self::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
///
/// ```rust,ignore
/// 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`](crate::BrowserPoolConfig::ping_interval)).
///
/// ```text
/// Keep-Alive Thread
/// │
/// ├─── ping() ──→ Browser 1 ──→ ✓ OK
/// │
/// ├─── ping() ──→ Browser 2 ──→ ✗ Failed (count: 1)
/// │
/// └─── ping() ──→ Browser 3 ──→ ✓ OK
/// ```
///
/// After [`max_ping_failures`](crate::BrowserPoolConfig::max_ping_failures)
/// consecutive failures, the browser is removed and replaced.