From 22dcab4d6715ad03a41eaa308b9a62ba6da0b566 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Thu, 4 Jul 2024 07:15:04 +0900 Subject: [PATCH 1/3] Remove unused field from ReceivedPacket --- src/socket/filter/cache.rs | 23 ++++++++++------------- src/socket/filter/mod.rs | 4 ++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/socket/filter/cache.rs b/src/socket/filter/cache.rs index caab02231..f2a006324 100644 --- a/src/socket/filter/cache.rs +++ b/src/socket/filter/cache.rs @@ -22,14 +22,12 @@ use std::{ /// cache's `time_window`. pub const ENFORCED_SIZE_TIME: u64 = 1; -pub struct ReceivedPacket { - /// The source that sent us the packet. - pub content: T, +pub struct ReceivedPacket { /// The time the packet was received. pub received: Instant, } -pub struct ReceivedPacketCache { +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 target: usize, @@ -40,10 +38,10 @@ pub struct ReceivedPacketCache { /// This stores the current number of messages that are within the `ENFORCED_SIZE_TIME`. within_enforced_time: usize, /// The underlying data structure. - inner: VecDeque>, + inner: VecDeque, } -impl ReceivedPacketCache { +impl ReceivedPacketCache { /// Creates a new `ReceivedPacketCache` with a specified size from which no more can enter. pub fn new(target: usize, time_window: u64) -> Self { Self { @@ -84,19 +82,18 @@ impl ReceivedPacketCache { } /// Inserts an element into the cache, removing any expired elements. - pub fn cache_insert(&mut self, content: T) -> bool { + pub fn cache_insert(&mut self) -> bool { self.reset(); - self.internal_insert(content) + self.internal_insert() } /// Inserts an element into the cache without removing expired elements. - fn internal_insert(&mut self, content: T) -> bool { + fn internal_insert(&mut self) -> bool { if self.within_enforced_time >= self.target { // Reached the target false } else { let received_packet = ReceivedPacket { - content, received: Instant::now(), }; self.inner.push_back(received_packet); @@ -106,15 +103,15 @@ impl ReceivedPacketCache { } } -impl std::ops::Deref for ReceivedPacketCache { - type Target = VecDeque>; +impl std::ops::Deref for ReceivedPacketCache { + type Target = VecDeque; fn deref(&self) -> &Self::Target { &self.inner } } -impl std::ops::DerefMut for ReceivedPacketCache { +impl std::ops::DerefMut for ReceivedPacketCache { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } diff --git a/src/socket/filter/mod.rs b/src/socket/filter/mod.rs index 7bb54ad77..b88aa12a7 100644 --- a/src/socket/filter/mod.rs +++ b/src/socket/filter/mod.rs @@ -43,7 +43,7 @@ pub(crate) struct Filter { /// An ordered (by time) collection of recently seen packets by SocketAddr. The packet data is not /// stored here. This stores 5 seconds of history to calculate a 5 second moving average for /// the metrics. - raw_packets_received: ReceivedPacketCache, + raw_packets_received: ReceivedPacketCache, /// The duration that bans by this filter last. ban_duration: Option, /// Keep track of node ids per socket. If someone is using too many node-ids per IP, they can @@ -98,7 +98,7 @@ impl Filter { // Add the un-solicited request to the cache // If this is over the maximum requests per ENFORCED_SIZE_TIME, it will not be added, we // leave the rate limiter to enforce the rate limits.. - self.raw_packets_received.cache_insert(*src); + self.raw_packets_received.cache_insert(); // build the metrics METRICS From e35b8a1de6fa702f89431625f5f53cfd4697de00 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Thu, 4 Jul 2024 07:25:23 +0900 Subject: [PATCH 2/3] Remove `ReceivedPacket` to simplify the cache --- src/socket/filter/cache.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/socket/filter/cache.rs b/src/socket/filter/cache.rs index f2a006324..10cbaa211 100644 --- a/src/socket/filter/cache.rs +++ b/src/socket/filter/cache.rs @@ -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 @@ -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, + /// The underlying data structure. It stores the time when a packet was received. + inner: VecDeque, } impl ReceivedPacketCache { @@ -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() @@ -93,10 +88,7 @@ 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 } @@ -104,7 +96,7 @@ impl ReceivedPacketCache { } impl std::ops::Deref for ReceivedPacketCache { - type Target = VecDeque; + type Target = VecDeque; fn deref(&self) -> &Self::Target { &self.inner From 9bf230036d8b5da3dd5bfd3a33dbcd7383d960d3 Mon Sep 17 00:00:00 2001 From: ackintosh Date: Thu, 4 Jul 2024 07:27:33 +0900 Subject: [PATCH 3/3] Fix clippy errors --- src/socket/filter/mod.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/socket/filter/mod.rs b/src/socket/filter/mod.rs index b88aa12a7..695a6eed4 100644 --- a/src/socket/filter/mod.rs +++ b/src/socket/filter/mod.rs @@ -86,11 +86,11 @@ impl Filter { /// The first check. This determines if a new UDP packet should be decoded or dropped. /// Only unsolicited packets arrive here. pub fn initial_pass(&mut self, src: &SocketAddr) -> bool { - if PERMIT_BAN_LIST.read().permit_ips.get(&src.ip()).is_some() { + if PERMIT_BAN_LIST.read().permit_ips.contains(&src.ip()) { return true; } - if PERMIT_BAN_LIST.read().ban_ips.get(&src.ip()).is_some() { + if PERMIT_BAN_LIST.read().ban_ips.contains_key(&src.ip()) { debug!("Dropped unsolicited packet from banned src: {:?}", src); return false; } @@ -135,8 +135,7 @@ impl Filter { if PERMIT_BAN_LIST .read() .permit_nodes - .get(&node_address.node_id) - .is_some() + .contains(&node_address.node_id) { return true; } @@ -144,8 +143,7 @@ impl Filter { if PERMIT_BAN_LIST .read() .ban_nodes - .get(&node_address.node_id) - .is_some() + .contains_key(&node_address.node_id) { debug!( "Dropped unsolicited packet from banned node_id: {}",