From a77fdcf171a5bb0a20287206e04032d05dab6752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Fri, 20 Oct 2023 09:19:11 +0100 Subject: [PATCH] address review --- Cargo.toml | 2 +- src/handler/mod.rs | 2 +- src/socket/filter/mod.rs | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2dd210ca..9bc784f93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ more-asserts = "0.3" [dev-dependencies] rand_07 = { package = "rand", version = "0.7" } -quickcheck = "1" +quickcheck = "0.9" tokio = { version = "1", features = ["full"] } rand_xorshift = "0.3" rand_core = "0.6" diff --git a/src/handler/mod.rs b/src/handler/mod.rs index 6df6ef835..c3c8f28f7 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -471,7 +471,7 @@ impl Handler { trace!("Request queued for node: {}", node_address); self.pending_requests .entry(node_address) - .or_insert_with(Vec::new) + .or_default() .push(PendingRequest { contact, request_id, diff --git a/src/socket/filter/mod.rs b/src/socket/filter/mod.rs index 817ad4d82..bdb8705f7 100644 --- a/src/socket/filter/mod.rs +++ b/src/socket/filter/mod.rs @@ -8,6 +8,7 @@ use std::{ collections::HashSet, convert::TryInto, net::{IpAddr, SocketAddr}, + num::NonZeroUsize, sync::atomic::Ordering, time::{Duration, Instant}, }; @@ -20,9 +21,16 @@ pub use config::FilterConfig; use rate_limiter::{LimitKind, RateLimiter}; /// The maximum number of IPs to retain when calculating the number of nodes per IP. -const KNOWN_ADDRS_SIZE: usize = 500; +const KNOWN_ADDRS_SIZE: NonZeroUsize = match NonZeroUsize::new(500) { + Some(non_zero) => non_zero, + None => unreachable!(), +}; /// The number of IPs to retain at any given time that have banned nodes. -const BANNED_NODES_SIZE: usize = 50; +const BANNED_NODES_SIZE: NonZeroUsize = match NonZeroUsize::new(50) { + Some(non_zero) => non_zero, + None => unreachable!(), +}; + /// The maximum number of packets to keep record of for metrics if the rate limiter is not /// specified. const DEFAULT_PACKETS_PER_SECOND: usize = 20; @@ -68,8 +76,8 @@ impl Filter { expected_packets_per_second, METRICS.moving_window, ), - known_addrs: LruCache::new(KNOWN_ADDRS_SIZE.try_into().unwrap()), - banned_nodes: LruCache::new(BANNED_NODES_SIZE.try_into().unwrap()), + known_addrs: LruCache::new(KNOWN_ADDRS_SIZE), + banned_nodes: LruCache::new(BANNED_NODES_SIZE), ban_duration, max_nodes_per_ip: config.max_nodes_per_ip, max_bans_per_ip: config.max_bans_per_ip,