Skip to content

Commit

Permalink
Remove ReceivedPacket to simplify the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ackintosh committed Jul 3, 2024
1 parent 22dcab4 commit e35b8a1
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/socket/filter/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ use std::{
/// cache's `time_window`.
pub const ENFORCED_SIZE_TIME: u64 = 1;

pub struct ReceivedPacket {
/// The time the packet was received.
pub received: Instant,
}

pub struct ReceivedPacketCache {
/// The target number of entries per ENFORCED_SIZE_TIME before inserting new elements reports
/// failure. The maximum size of the cache is target*time_window
Expand All @@ -37,8 +32,8 @@ pub struct ReceivedPacketCache {
time_window: u64,
/// This stores the current number of messages that are within the `ENFORCED_SIZE_TIME`.
within_enforced_time: usize,
/// The underlying data structure.
inner: VecDeque<ReceivedPacket>,
/// The underlying data structure. It stores the time when a packet was received.
inner: VecDeque<Instant>,
}

impl ReceivedPacketCache {
Expand All @@ -54,21 +49,21 @@ impl ReceivedPacketCache {

/// Remove expired packets. We only keep, `CACHE_TIME` of data in the cache.
pub fn reset(&mut self) {
while let Some(packet) = self.inner.pop_front() {
if packet.received
while let Some(received_at) = self.inner.pop_front() {
if received_at
> Instant::now()
.checked_sub(Duration::from_secs(self.time_window))
.unwrap()
{
// add the packet back and end
self.inner.push_front(packet);
self.inner.push_front(received_at);
break;
}
}
// update the within_enforced_time
let mut count = 0;
for packet in self.inner.iter().rev() {
if packet.received
for received_at in self.inner.iter().rev() {
if *received_at
> Instant::now()
.checked_sub(Duration::from_secs(ENFORCED_SIZE_TIME))
.unwrap()
Expand All @@ -93,18 +88,15 @@ impl ReceivedPacketCache {
// Reached the target
false
} else {
let received_packet = ReceivedPacket {
received: Instant::now(),
};
self.inner.push_back(received_packet);
self.inner.push_back(Instant::now());
self.within_enforced_time += 1;
true
}
}
}

impl std::ops::Deref for ReceivedPacketCache {
type Target = VecDeque<ReceivedPacket>;
type Target = VecDeque<Instant>;

fn deref(&self) -> &Self::Target {
&self.inner
Expand Down

0 comments on commit e35b8a1

Please sign in to comment.