From a129a6fbd4a851c1c208cc5b2f01b20c9ae82fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 12 Aug 2025 16:50:26 +0100 Subject: [PATCH 1/4] chore: add #[allow(allow_deadcode)] --- libp2p/src/builder/phase.rs | 4 ++-- libp2p/src/builder/select_muxer.rs | 1 + libp2p/src/builder/select_security.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libp2p/src/builder/phase.rs b/libp2p/src/builder/phase.rs index 78ff49724c0..fa378273630 100644 --- a/libp2p/src/builder/phase.rs +++ b/libp2p/src/builder/phase.rs @@ -35,7 +35,7 @@ use super::{ select_muxer::SelectMuxerUpgrade, select_security::SelectSecurityUpgrade, SwarmBuilder, }; -#[allow(unreachable_pub)] +#[allow(unreachable_pub, dead_code)] pub trait IntoSecurityUpgrade { type Upgrade; type Error; @@ -77,7 +77,7 @@ where } } -#[allow(unreachable_pub)] +#[allow(unreachable_pub, dead_code)] pub trait IntoMultiplexerUpgrade { type Upgrade; diff --git a/libp2p/src/builder/select_muxer.rs b/libp2p/src/builder/select_muxer.rs index 93ae0547269..c60798a1ee1 100644 --- a/libp2p/src/builder/select_muxer.rs +++ b/libp2p/src/builder/select_muxer.rs @@ -34,6 +34,7 @@ use libp2p_core::{ pub struct SelectMuxerUpgrade(A, B); impl SelectMuxerUpgrade { + #[allow(dead_code)] pub fn new(a: A, b: B) -> Self { SelectMuxerUpgrade(a, b) } diff --git a/libp2p/src/builder/select_security.rs b/libp2p/src/builder/select_security.rs index 1ed760feb1b..e28083f73dc 100644 --- a/libp2p/src/builder/select_security.rs +++ b/libp2p/src/builder/select_security.rs @@ -42,6 +42,7 @@ impl SelectSecurityUpgrade { /// Combines two upgrades into an `SelectUpgrade`. /// /// The protocols supported by the first element have a higher priority. + #[allow(dead_code)] pub fn new(a: A, b: B) -> Self { SelectSecurityUpgrade(a, b) } From 6187f13e90174400633f75e9aeb9409dd8300d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 12 Aug 2025 19:26:00 +0100 Subject: [PATCH 2/4] fix(memory-connection-limits): make memory limit exclusive --- Cargo.lock | 2 +- Cargo.toml | 2 +- misc/memory-connection-limits/CHANGELOG.md | 4 ++++ misc/memory-connection-limits/Cargo.toml | 2 +- misc/memory-connection-limits/src/lib.rs | 2 +- misc/memory-connection-limits/tests/max_bytes.rs | 2 +- misc/memory-connection-limits/tests/max_percentage.rs | 2 +- 7 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25b477e92b5..604cb5addcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2773,7 +2773,7 @@ dependencies = [ [[package]] name = "libp2p-memory-connection-limits" -version = "0.5.0" +version = "0.5.1" dependencies = [ "libp2p-core", "libp2p-identify", diff --git a/Cargo.toml b/Cargo.toml index feaf87d306d..f30eb1f3ae9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,7 +87,7 @@ libp2p-identify = { version = "0.47.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.12" } libp2p-kad = { version = "0.49.0", path = "protocols/kad" } libp2p-mdns = { version = "0.48.0", path = "protocols/mdns" } -libp2p-memory-connection-limits = { version = "0.5.0", path = "misc/memory-connection-limits" } +libp2p-memory-connection-limits = { version = "0.5.1", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.17.0", path = "misc/metrics" } libp2p-mplex = { version = "0.43.1", path = "muxers/mplex" } libp2p-noise = { version = "0.46.1", path = "transports/noise" } diff --git a/misc/memory-connection-limits/CHANGELOG.md b/misc/memory-connection-limits/CHANGELOG.md index f854edba6a4..a46c140672c 100644 --- a/misc/memory-connection-limits/CHANGELOG.md +++ b/misc/memory-connection-limits/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.1 + +- fix limit checking according to documentation, limit should be reached not exceeded. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). ## 0.5.0 diff --git a/misc/memory-connection-limits/Cargo.toml b/misc/memory-connection-limits/Cargo.toml index da4df0c5244..eb53db3499e 100644 --- a/misc/memory-connection-limits/Cargo.toml +++ b/misc/memory-connection-limits/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-memory-connection-limits" edition.workspace = true rust-version = { workspace = true } description = "Memory usage based connection limits for libp2p." -version = "0.5.0" +version = "0.5.1" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] diff --git a/misc/memory-connection-limits/src/lib.rs b/misc/memory-connection-limits/src/lib.rs index 7905e39ac18..d7db1e9c32b 100644 --- a/misc/memory-connection-limits/src/lib.rs +++ b/misc/memory-connection-limits/src/lib.rs @@ -115,7 +115,7 @@ impl Behaviour { fn check_limit(&mut self) -> Result<(), ConnectionDenied> { self.refresh_memory_stats_if_needed(); - if self.process_physical_memory_bytes > self.max_allowed_bytes { + if self.process_physical_memory_bytes >= self.max_allowed_bytes { return Err(ConnectionDenied::new(MemoryUsageLimitExceeded { process_physical_memory_bytes: self.process_physical_memory_bytes, max_allowed_bytes: self.max_allowed_bytes, diff --git a/misc/memory-connection-limits/tests/max_bytes.rs b/misc/memory-connection-limits/tests/max_bytes.rs index 442e38bdf1b..1f38913515d 100644 --- a/misc/memory-connection-limits/tests/max_bytes.rs +++ b/misc/memory-connection-limits/tests/max_bytes.rs @@ -32,7 +32,7 @@ use util::*; #[tokio::test] async fn max_bytes() { const CONNECTION_LIMIT: usize = 20; - let max_allowed_bytes = CONNECTION_LIMIT * 1024 * 1024; + let max_allowed_bytes = (CONNECTION_LIMIT - 1) * 1024 * 1024; let mut network = Swarm::new_ephemeral_tokio(|_| TestBehaviour { connection_limits: Behaviour::with_max_bytes(max_allowed_bytes), diff --git a/misc/memory-connection-limits/tests/max_percentage.rs b/misc/memory-connection-limits/tests/max_percentage.rs index cddb29d6964..6b3cedb8523 100644 --- a/misc/memory-connection-limits/tests/max_percentage.rs +++ b/misc/memory-connection-limits/tests/max_percentage.rs @@ -59,7 +59,7 @@ async fn max_percentage() { // Adds current mem usage to the limit and update let current_mem = memory_stats::memory_stats().unwrap().physical_mem; - let max_allowed_bytes = current_mem + CONNECTION_LIMIT * 1024 * 1024; + let max_allowed_bytes = current_mem + (CONNECTION_LIMIT - 1) * 1024 * 1024; network.behaviour_mut().connection_limits = Behaviour::with_max_percentage( max_allowed_bytes as f64 / system_info.total_memory() as f64, ); From de4fe2f2963110985c6a66849ee2726b9bbd2f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 12 Aug 2025 19:40:44 +0100 Subject: [PATCH 3/4] Update misc/memory-connection-limits/CHANGELOG.md --- misc/memory-connection-limits/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/memory-connection-limits/CHANGELOG.md b/misc/memory-connection-limits/CHANGELOG.md index a46c140672c..eeea1860fda 100644 --- a/misc/memory-connection-limits/CHANGELOG.md +++ b/misc/memory-connection-limits/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.5.1 - fix limit checking according to documentation, limit should be reached not exceeded. - See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). + See [PR 6134](https://github.com/libp2p/rust-libp2p/pull/6134). ## 0.5.0 From 4531898b13db77010f4201a3c7fd95d86b8db686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Tue, 12 Aug 2025 20:55:06 +0100 Subject: [PATCH 4/4] address Elena's review --- Cargo.lock | 2 +- Cargo.toml | 2 +- misc/memory-connection-limits/CHANGELOG.md | 4 ---- misc/memory-connection-limits/Cargo.toml | 2 +- misc/memory-connection-limits/src/lib.rs | 4 ++-- 5 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 604cb5addcb..25b477e92b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2773,7 +2773,7 @@ dependencies = [ [[package]] name = "libp2p-memory-connection-limits" -version = "0.5.1" +version = "0.5.0" dependencies = [ "libp2p-core", "libp2p-identify", diff --git a/Cargo.toml b/Cargo.toml index f30eb1f3ae9..feaf87d306d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,7 +87,7 @@ libp2p-identify = { version = "0.47.0", path = "protocols/identify" } libp2p-identity = { version = "0.2.12" } libp2p-kad = { version = "0.49.0", path = "protocols/kad" } libp2p-mdns = { version = "0.48.0", path = "protocols/mdns" } -libp2p-memory-connection-limits = { version = "0.5.1", path = "misc/memory-connection-limits" } +libp2p-memory-connection-limits = { version = "0.5.0", path = "misc/memory-connection-limits" } libp2p-metrics = { version = "0.17.0", path = "misc/metrics" } libp2p-mplex = { version = "0.43.1", path = "muxers/mplex" } libp2p-noise = { version = "0.46.1", path = "transports/noise" } diff --git a/misc/memory-connection-limits/CHANGELOG.md b/misc/memory-connection-limits/CHANGELOG.md index a46c140672c..f854edba6a4 100644 --- a/misc/memory-connection-limits/CHANGELOG.md +++ b/misc/memory-connection-limits/CHANGELOG.md @@ -1,7 +1,3 @@ -## 0.5.1 - -- fix limit checking according to documentation, limit should be reached not exceeded. - See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). ## 0.5.0 diff --git a/misc/memory-connection-limits/Cargo.toml b/misc/memory-connection-limits/Cargo.toml index eb53db3499e..da4df0c5244 100644 --- a/misc/memory-connection-limits/Cargo.toml +++ b/misc/memory-connection-limits/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-memory-connection-limits" edition.workspace = true rust-version = { workspace = true } description = "Memory usage based connection limits for libp2p." -version = "0.5.1" +version = "0.5.0" license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" keywords = ["peer-to-peer", "libp2p", "networking"] diff --git a/misc/memory-connection-limits/src/lib.rs b/misc/memory-connection-limits/src/lib.rs index d7db1e9c32b..03eb985caba 100644 --- a/misc/memory-connection-limits/src/lib.rs +++ b/misc/memory-connection-limits/src/lib.rs @@ -82,7 +82,7 @@ const MAX_STALE_DURATION: Duration = Duration::from_millis(100); impl Behaviour { /// Sets the process memory usage threshold in absolute bytes. /// - /// New inbound and outbound connections will be denied when the threshold is reached. + /// New inbound and outbound connections will be denied when the threshold is exceeded. pub fn with_max_bytes(max_allowed_bytes: usize) -> Self { Self { max_allowed_bytes, @@ -95,7 +95,7 @@ impl Behaviour { /// Sets the process memory usage threshold in the percentage of the total physical memory. /// - /// New inbound and outbound connections will be denied when the threshold is reached. + /// New inbound and outbound connections will be denied when the threshold is exceeded. pub fn with_max_percentage(percentage: f64) -> Self { use sysinfo::{RefreshKind, System};