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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Lightweight per-connection tracker for paginated reports.
//!
//! The [`ReportTracker`] lives in the WebSocket connection handler's local
//! scope and tracks which pages of each paginated report have been received.
//! It stores **no payload data** — each page is processed immediately and
//! dropped. The tracker only holds page counts, timestamps, and a small
//! accumulated notification counter.
//!
//! # Memory budget
//!
//! Each [`PendingReport`] is ~200 bytes. With a maximum of
//! [`MAX_PENDING_REPORTS_PER_CONNECTION`](crate::limits::MAX_PENDING_REPORTS_PER_CONNECTION)
//! concurrent reports, the tracker uses at most ~2 KB per connection.
use std::collections::{BTreeSet, HashMap};
use uuid::Uuid;
use crate::limits::{
MAX_PENDING_REPORTS_PER_CONNECTION, MAX_REPORT_PAGES, REPORT_IDLE_TIMEOUT,
REPORT_TOTAL_TIMEOUT, WireValidationError,
};
/// Outcome of registering a page with the [`ReportTracker`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PageOutcome {
/// More pages are expected; the caller should process this page's payload
/// but defer finalization.
Pending,
/// This was the final page; the caller should process the payload and run
/// finalization. Contains the accumulated `discovered_count` from all
/// prior pages plus the current page's contribution (added by the caller).
Final {
/// Sum of `discovered_count` values accumulated across all prior pages.
/// The caller adds the current page's count before using it.
accumulated_discovered_count: u32,
},
}
/// Tracks the state of a single paginated report.
#[derive(Debug)]
struct PendingReport {
/// Which 1-based page numbers have been received.
pages_received: BTreeSet<u32>,
/// Total pages expected.
total_pages: u32,
/// When the first page was received.
started_at: tokio::time::Instant,
/// When the most recent page was received.
last_page_at: tokio::time::Instant,
/// Accumulated discovered_count across all processed pages (discovery only).
discovered_count: u32,
}
/// Per-connection tracker for paginated reports.
///
/// Created when the authenticated message loop starts, dropped when the
/// connection closes. No shared or global state.
#[derive(Debug)]
pub struct ReportTracker {
pending: HashMap<Uuid, PendingReport>,
}
impl ReportTracker {
/// Create a new empty tracker.
pub fn new() -> Self {
Self {
pending: HashMap::new(),
}
}
/// Register a page of a paginated report.
///
/// Returns:
/// - `Ok(PageOutcome::Pending)` if more pages are expected.
/// - `Ok(PageOutcome::Final { .. })` if all pages have now been received.
/// - `Err(WireValidationError)` if limits are violated (duplicate page,
/// too many concurrent reports, `total_pages` mismatch, etc.).
///
/// The caller must process the page's payload regardless of the outcome.
pub fn register_page(
&mut self,
report_id: Uuid,
page: u32,
total_pages: u32,
) -> Result<PageOutcome, WireValidationError> {
// Evict timed-out reports before checking limits.
self.evict_expired();
if let Some(existing) = self.pending.get_mut(&report_id) {
// Validate total_pages consistency.
if existing.total_pages != total_pages {
return Err(WireValidationError {
field: "pagination.total_pages",
message: format!(
"total_pages changed from {} to {total_pages} within report {report_id}",
existing.total_pages
),
});
}
// Reject duplicate pages.
if existing.pages_received.contains(&page) {
return Err(WireValidationError {
field: "pagination.page",
message: format!("duplicate page {page} for report {report_id}"),
});
}
existing.pages_received.insert(page);
existing.last_page_at = tokio::time::Instant::now();
if existing.pages_received.len() == existing.total_pages as usize {
// All pages received — remove from tracker and return Final.
#[expect(
clippy::expect_used,
reason = "infallible: we are inside a branch that already accessed this key via get_mut; the entry is guaranteed to exist"
)]
let report = self
.pending
.remove(&report_id)
.expect("report was just accessed");
Ok(PageOutcome::Final {
accumulated_discovered_count: report.discovered_count,
})
} else {
Ok(PageOutcome::Pending)
}
} else {
// New report.
if self.pending.len() >= MAX_PENDING_REPORTS_PER_CONNECTION {
return Err(WireValidationError {
field: "pagination.report_id",
message: format!(
"too many concurrent paginated reports (max {MAX_PENDING_REPORTS_PER_CONNECTION})"
),
});
}
if total_pages > MAX_REPORT_PAGES {
return Err(WireValidationError {
field: "pagination.total_pages",
message: format!("total_pages is {total_pages}, max {MAX_REPORT_PAGES}"),
});
}
let now = tokio::time::Instant::now();
let mut pages_received = BTreeSet::new();
pages_received.insert(page);
// Single-page "paginated" report: return Final immediately.
if total_pages == 1 {
return Ok(PageOutcome::Final {
accumulated_discovered_count: 0,
});
}
self.pending.insert(
report_id,
PendingReport {
pages_received,
total_pages,
started_at: now,
last_page_at: now,
discovered_count: 0,
},
);
Ok(PageOutcome::Pending)
}
}
/// Add to the accumulated `discovered_count` for an in-progress report.
///
/// No-op if the report has already been finalized or does not exist.
pub fn add_discovered_count(&mut self, report_id: Uuid, count: u32) {
if let Some(report) = self.pending.get_mut(&report_id) {
report.discovered_count = report.discovered_count.saturating_add(count);
}
}
/// Evict reports that have exceeded the total or idle timeout.
///
/// Called automatically by [`register_page`](Self::register_page). Can also
/// be called periodically from a background task.
pub fn evict_expired(&mut self) {
let now = tokio::time::Instant::now();
self.pending.retain(|id, report| {
let total_elapsed = now.duration_since(report.started_at);
let idle_elapsed = now.duration_since(report.last_page_at);
if total_elapsed >= REPORT_TOTAL_TIMEOUT {
tracing::warn!(
report_id = %id,
pages_received = report.pages_received.len(),
total_pages = report.total_pages,
"paginated report timed out (total timeout {}s exceeded)",
REPORT_TOTAL_TIMEOUT.as_secs()
);
return false;
}
if idle_elapsed >= REPORT_IDLE_TIMEOUT {
tracing::warn!(
report_id = %id,
pages_received = report.pages_received.len(),
total_pages = report.total_pages,
"paginated report timed out (idle timeout {}s exceeded)",
REPORT_IDLE_TIMEOUT.as_secs()
);
return false;
}
true
});
}
/// Returns the number of currently pending reports.
pub fn pending_count(&self) -> usize {
self.pending.len()
}
}
impl Default for ReportTracker {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(start_paused = true)]
async fn single_page_report_returns_final() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
let outcome = tracker.register_page(id, 1, 1).unwrap();
assert_eq!(
outcome,
PageOutcome::Final {
accumulated_discovered_count: 0
}
);
assert_eq!(tracker.pending_count(), 0);
}
#[tokio::test(start_paused = true)]
async fn multi_page_report_lifecycle() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
// Page 1 of 3
let outcome = tracker.register_page(id, 1, 3).unwrap();
assert_eq!(outcome, PageOutcome::Pending);
assert_eq!(tracker.pending_count(), 1);
tracker.add_discovered_count(id, 100);
// Page 2 of 3
let outcome = tracker.register_page(id, 2, 3).unwrap();
assert_eq!(outcome, PageOutcome::Pending);
tracker.add_discovered_count(id, 50);
// Page 3 of 3
let outcome = tracker.register_page(id, 3, 3).unwrap();
assert_eq!(
outcome,
PageOutcome::Final {
accumulated_discovered_count: 150
}
);
assert_eq!(tracker.pending_count(), 0);
}
#[tokio::test(start_paused = true)]
async fn pages_out_of_order() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
assert_eq!(
tracker.register_page(id, 3, 3).unwrap(),
PageOutcome::Pending
);
assert_eq!(
tracker.register_page(id, 1, 3).unwrap(),
PageOutcome::Pending
);
let outcome = tracker.register_page(id, 2, 3).unwrap();
assert_eq!(
outcome,
PageOutcome::Final {
accumulated_discovered_count: 0
}
);
}
#[tokio::test(start_paused = true)]
async fn duplicate_page_rejected() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
tracker.register_page(id, 1, 3).unwrap();
let err = tracker.register_page(id, 1, 3).unwrap_err();
assert!(err.message.contains("duplicate page"));
}
#[tokio::test(start_paused = true)]
async fn total_pages_mismatch_rejected() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
tracker.register_page(id, 1, 3).unwrap();
let err = tracker.register_page(id, 2, 5).unwrap_err();
assert!(err.message.contains("total_pages changed"));
}
#[tokio::test(start_paused = true)]
async fn max_concurrent_reports_enforced() {
let mut tracker = ReportTracker::new();
for i in 0..MAX_PENDING_REPORTS_PER_CONNECTION {
let id = Uuid::from_u128(i as u128);
tracker.register_page(id, 1, 2).unwrap();
}
let extra_id = Uuid::from_u128(999);
let err = tracker.register_page(extra_id, 1, 2).unwrap_err();
assert!(err.message.contains("too many concurrent"));
}
#[tokio::test(start_paused = true)]
async fn idle_timeout_evicts() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
tracker.register_page(id, 1, 3).unwrap();
assert_eq!(tracker.pending_count(), 1);
// Advance past idle timeout.
tokio::time::advance(REPORT_IDLE_TIMEOUT + std::time::Duration::from_secs(1)).await;
tracker.evict_expired();
assert_eq!(tracker.pending_count(), 0);
}
#[tokio::test(start_paused = true)]
async fn total_timeout_evicts() {
let mut tracker = ReportTracker::new();
let id = Uuid::new_v4();
tracker.register_page(id, 1, 3).unwrap();
// Keep the idle timeout from firing by advancing in small steps and
// sending another page each time.
for step in 0..25 {
tokio::time::advance(std::time::Duration::from_secs(13)).await;
// Pages 2..N keep the idle timer fresh (but won't reach total_pages
// since we only register up to 24 out of a hypothetical large total).
// Adjust: use a larger total_pages value for this test.
if step == 0 {
// Already registered page 1 with total_pages=3. Let's re-create
// with a large total so we can send many pages.
// Instead, just check total timeout logic directly.
}
}
// Advance past total timeout.
tokio::time::advance(REPORT_TOTAL_TIMEOUT + std::time::Duration::from_secs(1)).await;
tracker.evict_expired();
assert_eq!(tracker.pending_count(), 0);
}
#[tokio::test(start_paused = true)]
async fn add_discovered_count_no_op_for_unknown() {
let mut tracker = ReportTracker::new();
// No-op: report doesn't exist.
tracker.add_discovered_count(Uuid::new_v4(), 42);
assert_eq!(tracker.pending_count(), 0);
}
}