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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! Request coalescing to deduplicate concurrent in-flight requests for the same URL.
//!
//! Feature-gated behind `request_coalesce`. When multiple tasks request the
//! same URL concurrently, only one performs the actual fetch; the others wait
//! for completion and then read from cache or shared state.
#[cfg(feature = "request_coalesce")]
mod inner {
use crate::compact_str::CompactString;
use dashmap::DashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::broadcast;
/// Broadcast channel capacity — only one completion signal is sent per URL;
/// a small buffer suffices for all subscribers.
const CHANNEL_CAPACITY: usize = 4;
/// Maximum time a guard can be held before it is considered stale (seconds).
const STALE_TIMEOUT_SECS: u64 = 120;
/// Result of calling [`RequestCoalescer::try_start`].
pub enum CoalesceResult {
/// This caller should perform the fetch. Call [`CoalesceGuard::complete`]
/// (or just drop the guard) when done.
Proceed(CoalesceGuard),
/// Another caller is already fetching this URL. Await the receiver
/// to be notified on completion, then read from cache/shared state.
Wait(broadcast::Receiver<()>),
}
/// RAII guard that removes the in-flight entry on drop.
///
/// The entry is removed when:
/// - `complete()` is called explicitly, or
/// - the guard is dropped (e.g., on panic or early return).
///
/// This ensures no dangling entries even if the fetch task panics.
pub struct CoalesceGuard {
url: CompactString,
in_flight: Arc<DashMap<CompactString, InFlightEntry>>,
generation: Arc<()>,
completed: bool,
}
struct InFlightEntry {
sender: broadcast::Sender<()>,
generation: Arc<()>,
created_at: Instant,
}
impl CoalesceGuard {
/// Signal that the fetch is complete and notify all waiters.
pub fn complete(mut self) {
self.finish();
}
fn finish(&mut self) {
if self.completed {
return;
}
self.completed = true;
if let Some((_, entry)) = self.in_flight.remove_if(&self.url, |_, entry| {
Arc::ptr_eq(&entry.generation, &self.generation)
}) {
let _ = entry.sender.send(());
}
}
}
impl Drop for CoalesceGuard {
fn drop(&mut self) {
self.finish();
}
}
/// Deduplicates concurrent requests for the same URL.
///
/// Thread-safe through short-lived DashMap shard locks. No guard is held
/// across an await or a second map operation.
pub struct RequestCoalescer {
in_flight: Arc<DashMap<CompactString, InFlightEntry>>,
}
impl RequestCoalescer {
/// Create a new coalescer.
pub fn new() -> Self {
Self {
in_flight: Arc::new(DashMap::with_capacity(64)),
}
}
/// Try to start a fetch for `url`.
///
/// - Returns `CoalesceResult::Proceed(guard)` if no other task is fetching this URL.
/// The caller must perform the fetch and then call `guard.complete()`.
/// - Returns `CoalesceResult::Wait(receiver)` if another task is already fetching.
/// The caller should `receiver.recv().await` and then read from cache.
pub fn try_start(&self, url: &str) -> CoalesceResult {
let key = CompactString::new(url);
if let Some(entry) = self.in_flight.get(&key) {
if entry.created_at.elapsed() < Duration::from_secs(STALE_TIMEOUT_SECS) {
return CoalesceResult::Wait(entry.sender.subscribe());
}
}
use dashmap::mapref::entry::Entry;
// Decide ownership while holding one shard lock. A read followed by
// insert lets concurrent misses both become the fetch owner.
let generation = Arc::new(());
match self.in_flight.entry(key.clone()) {
Entry::Occupied(mut entry) => {
if entry.get().created_at.elapsed() < Duration::from_secs(STALE_TIMEOUT_SECS) {
return CoalesceResult::Wait(entry.get().sender.subscribe());
}
let (sender, _) = broadcast::channel(CHANNEL_CAPACITY);
entry.insert(InFlightEntry {
sender,
generation: generation.clone(),
created_at: Instant::now(),
});
}
Entry::Vacant(entry) => {
let (sender, _) = broadcast::channel(CHANNEL_CAPACITY);
entry.insert(InFlightEntry {
sender,
generation: generation.clone(),
created_at: Instant::now(),
});
}
};
CoalesceResult::Proceed(CoalesceGuard {
url: key,
in_flight: Arc::clone(&self.in_flight),
generation,
completed: false,
})
}
/// Number of URLs currently in-flight.
pub fn in_flight_count(&self) -> usize {
self.in_flight.len()
}
/// Purge entries older than the stale timeout.
/// Call periodically if long-running guards are a concern.
pub fn purge_stale(&self) {
let cutoff = Duration::from_secs(STALE_TIMEOUT_SECS);
self.in_flight
.retain(|_, entry| entry.created_at.elapsed() < cutoff);
}
}
impl Default for RequestCoalescer {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simultaneous_misses_have_one_owner() {
let coalescer = Arc::new(RequestCoalescer::new());
let barrier = Arc::new(std::sync::Barrier::new(16));
let handles: Vec<_> = (0..16)
.map(|_| {
let c = coalescer.clone();
let b = barrier.clone();
std::thread::spawn(move || {
b.wait();
c.try_start("same-url")
})
})
.collect();
// Keep owners alive until all contenders have returned.
let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
assert_eq!(
results
.iter()
.filter(|r| matches!(r, CoalesceResult::Proceed(_)))
.count(),
1
);
}
#[test]
fn stale_owner_cannot_remove_replacement() {
let c = RequestCoalescer::new();
let old = match c.try_start("url") {
CoalesceResult::Proceed(g) => g,
_ => unreachable!(),
};
c.in_flight.get_mut("url").unwrap().created_at =
Instant::now() - Duration::from_secs(STALE_TIMEOUT_SECS + 1);
let new = match c.try_start("url") {
CoalesceResult::Proceed(g) => g,
_ => unreachable!(),
};
drop(old);
assert_eq!(c.in_flight_count(), 1);
let mut waiter = match c.try_start("url") {
CoalesceResult::Wait(rx) => rx,
_ => unreachable!(),
};
drop(new);
assert!(waiter.try_recv().is_ok());
assert_eq!(c.in_flight_count(), 0);
}
#[test]
fn test_first_caller_proceeds() {
let coalescer = RequestCoalescer::new();
let _guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("first caller should proceed"),
};
assert_eq!(coalescer.in_flight_count(), 1);
}
#[test]
fn test_second_caller_waits() {
let coalescer = RequestCoalescer::new();
let _guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("first caller should proceed"),
};
match coalescer.try_start("https://example.com") {
CoalesceResult::Wait(_) => {} // expected
CoalesceResult::Proceed(_) => panic!("second caller should wait"),
}
}
#[test]
fn test_different_urls_both_proceed() {
let coalescer = RequestCoalescer::new();
let _g1 = match coalescer.try_start("https://a.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed"),
};
let _g2 = match coalescer.try_start("https://b.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed for different URL"),
};
assert_eq!(coalescer.in_flight_count(), 2);
}
#[test]
fn test_guard_drop_removes_entry() {
let coalescer = RequestCoalescer::new();
{
let _guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed"),
};
assert_eq!(coalescer.in_flight_count(), 1);
// guard dropped here
}
assert_eq!(coalescer.in_flight_count(), 0);
// Next caller for same URL should proceed.
match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(_) => {} // expected
CoalesceResult::Wait(_) => panic!("should proceed after guard dropped"),
}
}
#[test]
fn test_complete_removes_entry() {
let coalescer = RequestCoalescer::new();
let guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed"),
};
assert_eq!(coalescer.in_flight_count(), 1);
guard.complete();
assert_eq!(coalescer.in_flight_count(), 0);
}
#[tokio::test]
async fn test_waiter_notified_on_complete() {
let coalescer = Arc::new(RequestCoalescer::new());
let guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed"),
};
let mut rx = match coalescer.try_start("https://example.com") {
CoalesceResult::Wait(r) => r,
CoalesceResult::Proceed(_) => panic!("should wait"),
};
// Complete in a separate task.
let handle = tokio::spawn(async move {
guard.complete();
});
// The receiver should get notified.
let result = tokio::time::timeout(Duration::from_secs(5), rx.recv()).await;
assert!(result.is_ok(), "should receive notification");
handle.await.unwrap();
}
#[test]
fn test_purge_stale_no_panic_on_empty() {
let coalescer = RequestCoalescer::new();
coalescer.purge_stale(); // Should not panic.
assert_eq!(coalescer.in_flight_count(), 0);
}
#[test]
fn test_double_complete_is_safe() {
let coalescer = RequestCoalescer::new();
let mut guard = match coalescer.try_start("https://example.com") {
CoalesceResult::Proceed(g) => g,
CoalesceResult::Wait(_) => panic!("should proceed"),
};
// Manually call finish twice (simulates complete + drop).
guard.finish();
guard.finish();
assert_eq!(coalescer.in_flight_count(), 0);
}
}
}
#[cfg(feature = "request_coalesce")]
pub use inner::{CoalesceGuard, CoalesceResult, RequestCoalescer};