File tree Expand file tree Collapse file tree 3 files changed +10
-4
lines changed
misc/memory-connection-limits Expand file tree Collapse file tree 3 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ const MAX_STALE_DURATION: Duration = Duration::from_millis(100);
82
82
impl Behaviour {
83
83
/// Sets the process memory usage threshold in absolute bytes.
84
84
///
85
- /// New inbound and outbound connections will be denied when the threshold is reached .
85
+ /// New inbound and outbound connections will be denied when the threshold is exceeded .
86
86
pub fn with_max_bytes ( max_allowed_bytes : usize ) -> Self {
87
87
Self {
88
88
max_allowed_bytes,
@@ -95,7 +95,7 @@ impl Behaviour {
95
95
96
96
/// Sets the process memory usage threshold in the percentage of the total physical memory.
97
97
///
98
- /// New inbound and outbound connections will be denied when the threshold is reached .
98
+ /// New inbound and outbound connections will be denied when the threshold is exceeded .
99
99
pub fn with_max_percentage ( percentage : f64 ) -> Self {
100
100
use sysinfo:: { RefreshKind , System } ;
101
101
Original file line number Diff line number Diff line change @@ -31,8 +31,11 @@ use util::*;
31
31
32
32
#[ tokio:: test]
33
33
async fn max_bytes ( ) {
34
+ // These tests use connections as unit to test the memory limit.
35
+ // Each connection consumes approximately 1MB of memory, so we give
36
+ // one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35.
34
37
const CONNECTION_LIMIT : usize = 20 ;
35
- let max_allowed_bytes = CONNECTION_LIMIT * 1024 * 1024 ;
38
+ let max_allowed_bytes = ( CONNECTION_LIMIT - 1 ) * 1024 * 1024 ;
36
39
37
40
let mut network = Swarm :: new_ephemeral_tokio ( |_| TestBehaviour {
38
41
connection_limits : Behaviour :: with_max_bytes ( max_allowed_bytes) ,
Original file line number Diff line number Diff line change @@ -59,7 +59,10 @@ async fn max_percentage() {
59
59
60
60
// Adds current mem usage to the limit and update
61
61
let current_mem = memory_stats:: memory_stats ( ) . unwrap ( ) . physical_mem ;
62
- let max_allowed_bytes = current_mem + CONNECTION_LIMIT * 1024 * 1024 ;
62
+ // These tests use connections as unit to test the memory limit.
63
+ // Each connection consumes approximately 1MB of memory, so we give
64
+ // one connection as buffer for test stability (CONNECTION_LIMIT - 1) on line 35.
65
+ let max_allowed_bytes = current_mem + ( CONNECTION_LIMIT - 1 ) * 1024 * 1024 ;
63
66
network. behaviour_mut ( ) . connection_limits = Behaviour :: with_max_percentage (
64
67
max_allowed_bytes as f64 / system_info. total_memory ( ) as f64 ,
65
68
) ;
You can’t perform that action at this time.
0 commit comments