MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / push

Method push

nodedb/src/event/cdc/buffer.rs:65–114  ·  view source on GitHub ↗

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>>)

Source from the content-addressed store, hash-verified

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

Callers 15

newMethod · 0.45
growMethod · 0.45
freeMethod · 0.45
allocMethod · 0.45
tier_grows_on_demandFunction · 0.45
tick_expiryMethod · 0.45
scan_with_surrogateMethod · 0.45
scanMethod · 0.45
lookup_rangeMethod · 0.45
build_keyMethod · 0.45
lookup_prefixMethod · 0.45

Calls 8

nowFunction · 0.85
pop_frontMethod · 0.80
duration_sinceMethod · 0.80
frontMethod · 0.80
entryMethod · 0.80
writeMethod · 0.45
lenMethod · 0.45
as_millisMethod · 0.45