Push a new event into the buffer. Evicts oldest if at capacity. Accepts anything that can be turned into an `Arc `. The router hands the SAME `Arc` to every matching stream's buffer so fan-out across N subscribers is N refcount bumps, not N deep clones. Returns the number of events evicted as a result of this push (0 or more). Callers may use the return value to increment per-stream dro
(&self, event: impl Into<Arc<CdcEvent>>)
| 63 | /// more). Callers may use the return value to increment per-stream drop |
| 64 | /// counters without an extra atomic read. |
| 65 | pub fn push(&self, event: impl Into<Arc<CdcEvent>>) -> u64 { |
| 66 | let event = event.into(); |
| 67 | let mut events = self.events.write().unwrap_or_else(|p| { |
| 68 | tracing::warn!(stream = %self.name, "StreamBuffer RwLock poisoned, recovering"); |
| 69 | p.into_inner() |
| 70 | }); |
| 71 | |
| 72 | let mut evicted_this_push: u64 = 0; |
| 73 | |
| 74 | // Evict by count. |
| 75 | while events.len() as u64 >= self.retention.max_events { |
| 76 | events.pop_front(); |
| 77 | evicted_this_push += 1; |
| 78 | self.total_evicted |
| 79 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 80 | } |
| 81 | |
| 82 | // Evict by age. |
| 83 | let now_ms = SystemTime::now() |
| 84 | .duration_since(UNIX_EPOCH) |
| 85 | .unwrap_or_default() |
| 86 | .as_millis() as u64; |
| 87 | let cutoff_ms = now_ms.saturating_sub(self.retention.max_age_secs * 1000); |
| 88 | while events.front().is_some_and(|e| e.event_time < cutoff_ms) { |
| 89 | events.pop_front(); |
| 90 | evicted_this_push += 1; |
| 91 | self.total_evicted |
| 92 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 93 | } |
| 94 | |
| 95 | // Update per-partition tail tracker *before* appending: this runs |
| 96 | // regardless of whether the ring has space, so partitions whose |
| 97 | // events get evicted still keep an advancing tail. |
| 98 | { |
| 99 | let mut tails = self |
| 100 | .partition_tails |
| 101 | .write() |
| 102 | .unwrap_or_else(|p| p.into_inner()); |
| 103 | let entry = tails.entry(event.partition).or_insert(0); |
| 104 | if event.lsn > *entry { |
| 105 | *entry = event.lsn; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | events.push_back(event); |
| 110 | self.total_pushed |
| 111 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 112 | |
| 113 | evicted_this_push |
| 114 | } |
| 115 | |
| 116 | /// Latest observed LSN per partition, across the entire lifetime of |
| 117 | /// the buffer — NOT bounded by retention. This is the correct source |