refactor(irtt-stats): split CoreStats event handlers

This commit is contained in:
Nils Andreas Svee committed 2026-05-13 13:40:55 +07:00
1 parent 969d26d0f1
commit 851825822d
1 file changed
+45 -15
+45 -15
View File
@@ -273,7 +273,6 @@ impl CoreStats {
} }
fn apply(&mut self, event: StatsEvent) -> EventStatsUpdate { fn apply(&mut self, event: StatsEvent) -> EventStatsUpdate {
let mut update = EventStatsUpdate::default();
match event { match event {
StatsEvent::Sent { StatsEvent::Sent {
bytes, bytes,
@@ -281,16 +280,45 @@ impl CoreStats {
timer_error_ns, timer_error_ns,
.. ..
} => { } => {
self.apply_sent(bytes, send_call_ns, timer_error_ns);
EventStatsUpdate::default()
}
StatsEvent::UniqueReply {
is_late, sample, ..
} => self.apply_unique_reply(is_late, *sample),
StatsEvent::DuplicateReply { .. } => {
self.apply_duplicate_reply();
EventStatsUpdate::default()
}
StatsEvent::Loss { .. } => {
self.apply_loss();
EventStatsUpdate::default()
}
StatsEvent::Warning { .. } => {
self.apply_warning();
EventStatsUpdate::default()
}
StatsEvent::UntrackedLate { .. } => {
self.apply_untracked_late();
EventStatsUpdate::default()
}
}
}
fn apply_sent(&mut self, bytes: usize, send_call_ns: i128, timer_error_ns: i128) {
self.events.sent_events += 1; self.events.sent_events += 1;
self.packets.packets_sent += 1; self.packets.packets_sent += 1;
self.packets.bytes_sent = self.packets.bytes_sent.saturating_add(bytes as u64); self.packets.bytes_sent = self.packets.bytes_sent.saturating_add(bytes as u64);
self.send_call.push_ns(send_call_ns); self.send_call.push_ns(send_call_ns);
self.timer_error.push_ns(timer_error_ns); self.timer_error.push_ns(timer_error_ns);
} }
StatsEvent::UniqueReply {
is_late, sample, .. fn apply_unique_reply(&mut self, is_late: bool, sample: ReplySample) -> EventStatsUpdate {
} => { let mut update = EventStatsUpdate {
let sample = *sample; contributed_sample: true,
..EventStatsUpdate::default()
};
self.events.echo_replies += u64::from(!is_late); self.events.echo_replies += u64::from(!is_late);
self.events.late_unique_replies += u64::from(is_late); self.events.late_unique_replies += u64::from(is_late);
self.packets.packets_received += 1; self.packets.packets_received += 1;
@@ -300,7 +328,7 @@ impl CoreStats {
.packets .packets
.bytes_received .bytes_received
.saturating_add(sample.bytes as u64); .saturating_add(sample.bytes as u64);
update.contributed_sample = true;
if let Some(count) = sample.received_count { if let Some(count) = sample.received_count {
self.packets.server_packets_received = Some(u64::from(count)); self.packets.server_packets_received = Some(u64::from(count));
} }
@@ -324,8 +352,7 @@ impl CoreStats {
} }
for pair in self.ipdv_tracker.insert(sample.ipdv) { for pair in self.ipdv_tracker.insert(sample.ipdv) {
let Some(rtt_ipdv) = duration_from_non_negative_i128_ns(pair.rtt_ipdv_ns) let Some(rtt_ipdv) = duration_from_non_negative_i128_ns(pair.rtt_ipdv_ns) else {
else {
continue; continue;
}; };
let send_ipdv = pair let send_ipdv = pair
@@ -353,24 +380,27 @@ impl CoreStats {
receive_ipdv, receive_ipdv,
}); });
} }
update
} }
StatsEvent::DuplicateReply { .. } => {
fn apply_duplicate_reply(&mut self) {
self.events.duplicate_replies += 1; self.events.duplicate_replies += 1;
self.packets.packets_received += 1; self.packets.packets_received += 1;
self.packets.duplicates += 1; self.packets.duplicates += 1;
} }
StatsEvent::Loss { .. } => {
fn apply_loss(&mut self) {
self.events.loss_events += 1; self.events.loss_events += 1;
} }
StatsEvent::Warning { .. } => {
fn apply_warning(&mut self) {
self.events.warning_events += 1; self.events.warning_events += 1;
} }
StatsEvent::UntrackedLate { .. } => {
fn apply_untracked_late(&mut self) {
self.events.untracked_late_replies += 1; self.events.untracked_late_replies += 1;
} }
}
update
}
fn snapshot(&self) -> Snapshot { fn snapshot(&self) -> Snapshot {
let packets = self.packets; let packets = self.packets;