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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/// Pool of reusable Chrome CDP tabs.
///
/// Lock-free design: uses a DashMap as a concurrent stack (push/pop by
/// atomic index). No Mutex, no RwLock.
pub struct TabPool {
/// Tabs stored by slot index. DashMap provides lock-free per-shard access.
slots: dashmap::DashMap<usize, chromiumoxide::Page>,
/// Next slot to write into (monotonically increasing).
head: std::sync::atomic::AtomicUsize,
/// Maximum pool capacity.
max_size: usize,
}
/// Hand a pooled tab off to actually be closed.
///
/// Dropping a `chromiumoxide::Page` does **not** close the underlying CDP tab — it only
/// decrements an internal counter (see the note on
/// [`crate::features::chrome::TabCloseGuard`]). Every path in this pool that stops
/// holding a tab must route it through here, or the tab leaks and Chrome eventually
/// stops handing out new ones.
///
/// Synchronous and infallible so it is safe to call from non-async paths such as
/// [`TabPool::clear`], and from a `Drop` if this pool ever grows one.
#[inline]
fn close_pooled_tab(page: chromiumoxide::Page) {
#[cfg(not(feature = "decentralized"))]
{
// Reuse the crate-wide background closer: it dedups by `target_id` and bounds
// each close with its own timeout, so this is just a channel send.
drop(crate::features::chrome::TabCloseGuard::new(page));
}
#[cfg(feature = "decentralized")]
{
// `TabCloseGuard` is compiled out under `decentralized`, so close inline on a
// detached task with the same bound. `try_current` (never `current`) keeps this
// a no-op instead of a panic when there is no runtime.
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
let _ = tokio::time::timeout(std::time::Duration::from_secs(5), page.close()).await;
});
}
}
}
impl TabPool {
/// Create a new tab pool with the given maximum size.
pub fn new(max_size: usize) -> Self {
Self {
slots: dashmap::DashMap::with_capacity(max_size),
head: std::sync::atomic::AtomicUsize::new(0),
max_size,
}
}
/// Acquire a tab from the pool or create a new one.
///
/// Pops the most recently pooled tab (LIFO) if available, otherwise
/// creates a fresh tab via `browser.new_page("about:blank")`.
pub async fn acquire(
&self,
browser: &chromiumoxide::Browser,
) -> Result<chromiumoxide::Page, chromiumoxide::error::CdpError> {
// Try to pop from the stack (LIFO).
loop {
let current = self.head.load(std::sync::atomic::Ordering::Acquire);
if current == 0 {
break; // pool empty
}
let target = current - 1;
// CAS to claim this slot.
if self
.head
.compare_exchange(
current,
target,
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Relaxed,
)
.is_ok()
{
// We won the slot — remove and return the tab.
if let Some((_, page)) = self.slots.remove(&target) {
return Ok(page);
}
// Slot was empty (shouldn't happen), continue to create new.
break;
}
// CAS failed — another thread popped; retry.
}
browser.new_page("about:blank").await
}
/// Release a tab back to the pool.
///
/// Navigates the tab to `about:blank` to clear state before pooling.
/// If the navigation hangs for more than 5 seconds the tab is closed.
/// If the pool is already at capacity the tab is closed instead of pooled.
pub async fn release(&self, page: chromiumoxide::Page) {
let current = self.head.load(std::sync::atomic::Ordering::Relaxed);
if current >= self.max_size {
close_pooled_tab(page); // at capacity — close, don't just drop
return;
}
// Navigate to about:blank with a 5s timeout to clear state.
let ok = matches!(
tokio::time::timeout(std::time::Duration::from_secs(5), page.goto("about:blank")).await,
Ok(Ok(_))
);
if !ok {
close_pooled_tab(page); // navigation failed/timed out — close it
return;
}
// Try to push onto the stack.
loop {
let current = self.head.load(std::sync::atomic::Ordering::Acquire);
if current >= self.max_size {
close_pooled_tab(page); // pool filled while we were navigating
return;
}
if self
.head
.compare_exchange(
current,
current + 1,
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Relaxed,
)
.is_ok()
{
self.slots.insert(current, page);
return;
}
// CAS failed — another thread pushed; retry.
}
}
/// Close all pooled tabs and empty the pool.
///
/// Takes ownership of each tab so it can be routed to the closer;
/// `DashMap::clear` alone would only drop the `Page` handles, which leaves the
/// CDP tabs open in the browser.
pub fn clear(&self) {
self.head.store(0, std::sync::atomic::Ordering::Release);
// Collect keys first so the iteration (and its shard guards) finishes before
// any `remove` — iterating a `DashMap` while mutating it can deadlock.
let keys: Vec<usize> = self.slots.iter().map(|entry| *entry.key()).collect();
for key in keys {
if let Some((_, page)) = self.slots.remove(&key) {
close_pooled_tab(page);
}
}
// Sweep anything a concurrent `release` inserted while we were draining.
// These handles are dropped rather than closed, which is the pre-existing
// behavior for a tab that races a `clear`; the window is a single insert wide.
self.slots.clear();
}
/// Returns the approximate number of pooled (idle) tabs.
pub fn pool_size(&self) -> usize {
self.head.load(std::sync::atomic::Ordering::Relaxed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_pool_is_empty() {
let pool = TabPool::new(5);
assert_eq!(pool.pool_size(), 0);
}
#[test]
fn test_pool_max_size() {
let pool = TabPool::new(0);
assert_eq!(pool.max_size, 0);
let pool = TabPool::new(100);
assert_eq!(pool.max_size, 100);
}
#[test]
fn test_clear_empty_pool() {
let pool = TabPool::new(5);
pool.clear();
assert_eq!(pool.pool_size(), 0);
}
}