Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions misc/memory-connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};

Expand Down
5 changes: 4 additions & 1 deletion misc/memory-connection-limits/tests/max_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 4 additions & 1 deletion misc/memory-connection-limits/tests/max_percentage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Loading