Skip to content

Commit

Permalink
Remove unused field from ReceivedPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
ackintosh committed Jul 3, 2024
1 parent 567da5b commit 22dcab4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 10 additions & 13 deletions src/socket/filter/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ use std::{
/// cache's `time_window`.
pub const ENFORCED_SIZE_TIME: u64 = 1;

pub struct ReceivedPacket<T> {
/// 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<T> {
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,
Expand All @@ -40,10 +38,10 @@ pub struct ReceivedPacketCache<T> {
/// 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<T>>,
inner: VecDeque<ReceivedPacket>,
}

impl<T> ReceivedPacketCache<T> {
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 {
Expand Down Expand Up @@ -84,19 +82,18 @@ impl<T> ReceivedPacketCache<T> {
}

/// 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);
Expand All @@ -106,15 +103,15 @@ impl<T> ReceivedPacketCache<T> {
}
}

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

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

impl<T> std::ops::DerefMut for ReceivedPacketCache<T> {
impl std::ops::DerefMut for ReceivedPacketCache {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
Expand Down
4 changes: 2 additions & 2 deletions src/socket/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr>,
raw_packets_received: ReceivedPacketCache,
/// The duration that bans by this filter last.
ban_duration: Option<Duration>,
/// Keep track of node ids per socket. If someone is using too many node-ids per IP, they can
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 22dcab4

Please sign in to comment.