diff --git a/misc/memory-connection-limits/src/lib.rs b/misc/memory-connection-limits/src/lib.rs index 7905e39ac18..b19a1573fba 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}; diff --git a/misc/memory-connection-limits/tests/max_bytes.rs b/misc/memory-connection-limits/tests/max_bytes.rs index 442e38bdf1b..16ff8ed3ea0 100644 --- a/misc/memory-connection-limits/tests/max_bytes.rs +++ b/misc/memory-connection-limits/tests/max_bytes.rs @@ -31,8 +31,11 @@ use util::*; #[tokio::test] async fn max_bytes() { + // These tests use connections as unit to test the memory limit. + // Each connection consumes approximately 1MB of memory, so we give + // one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35. 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..de1585eb279 100644 --- a/misc/memory-connection-limits/tests/max_percentage.rs +++ b/misc/memory-connection-limits/tests/max_percentage.rs @@ -59,7 +59,10 @@ 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; + // These tests use connections as unit to test the memory limit. + // Each connection consumes approximately 1MB of memory, so we give + // one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35. + 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, );