-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: memory based connection limits #4281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mergify
merged 41 commits into
libp2p:master
from
hanabi1224:memory-based-connection-limits
Aug 8, 2023
Merged
Changes from 19 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
90cd7d6
feat: memory based connection limits
hanabi1224 5014500
sets system ram
hanabi1224 e5519d7
code golf
hanabi1224 024da42
fix(examples/file-sharing): set Kademlia `Mode::Server`
mxinden 065efb1
docs: fix last OutboundQueryCompleted in kad doc
thomas-senechal 2a8e371
fix(relay): remove unconditional `async-std` dependency
quininer 94dc11b
deps(yamux): update yamux to `v0.12`
thomaseizinger 51a91b7
deps: bump Swatinem/rust-cache from 2.5.1 to 2.6.0
dependabot[bot] 6581822
move to new crate
hanabi1224 1106ad7
Merge remote-tracking branch 'origin/master' into memory-based-connec…
hanabi1224 6066b2e
doc
hanabi1224 1fa32f0
lock
hanabi1224 05cbf5e
doc and tests
hanabi1224 e308be7
Merge branch 'master' into memory-based-connection-limits
hanabi1224 7d05ee5
Apply suggestions from code review
hanabi1224 9ba799e
hide MemoryUsageBasedConnectionLimits
hanabi1224 b8a9916
update MemoryUsageLimitExceeded fmt as suggested
hanabi1224 f43fed5
fix clippy
hanabi1224 9a66866
fix rustdoc
hanabi1224 7990be6
Update misc/memory-connection-limits/src/lib.rs
hanabi1224 07e30bb
resolve comments
hanabi1224 6adf21f
Make MemoryUsageLimitExceeded fields private
hanabi1224 a720f64
Merge branch 'master' into memory-based-connection-limits
hanabi1224 803d8f5
Pre-calculate max_allowed_bytes
hanabi1224 f60cfb0
refresh interval
hanabi1224 259f038
Don't use `update` functions in tests
thomaseizinger c5e103c
Remove inner `Option`
thomaseizinger 5c63b87
Don't make refresh interval configurable
thomaseizinger dee261c
Update docs
thomaseizinger 1b7fb24
Remove refresh-interval field
thomaseizinger 280d13a
Use early return to reduce indentation
thomaseizinger aadad16
Only set timestamp if we actually refreshed the data
thomaseizinger 2875817
Use early return instead of else
thomaseizinger 800d501
Make tests for stable
hanabi1224 7331b8f
Add docs to constant
thomaseizinger a1d0b55
Sort functions are per trait definition
thomaseizinger 9da8e1e
Merge branch 'memory-connection-limits-thomas' into memory-based-conn…
thomaseizinger b24fb86
Re-export from libp2p meta crate
hanabi1224 14f0866
Merge branch 'master' into memory-based-connection-limits
hanabi1224 9eeee38
Update libp2p/CHANGELOG.md
thomaseizinger 13f09a8
Merge branch 'master' into memory-based-connection-limits
hanabi1224 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 0.1.0 - unreleased | ||
|
|
||
| - Initial release. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [package] | ||
| name = "libp2p-memory-connection-limits" | ||
| edition = "2021" | ||
| rust-version = { workspace = true } | ||
| description = "Memory usage based connection limits for libp2p." | ||
| version = "0.1.0" | ||
| license = "MIT" | ||
| repository = "https://github.com/libp2p/rust-libp2p" | ||
| keywords = ["peer-to-peer", "libp2p", "networking"] | ||
| categories = ["network-programming", "asynchronous"] | ||
|
|
||
| [dependencies] | ||
| memory-stats = "1" | ||
| libp2p-core = { workspace = true } | ||
| libp2p-swarm = { workspace = true } | ||
| libp2p-identity = { workspace = true, features = ["peerid"] } | ||
| log = "0.4" | ||
| sysinfo = "0.29" | ||
| void = "1" | ||
|
|
||
| [dev-dependencies] | ||
| async-std = { version = "1.12.0", features = ["attributes"] } | ||
| libp2p-identify = { workspace = true } | ||
| libp2p-swarm-derive = { path = "../../swarm-derive" } | ||
| libp2p-swarm-test = { path = "../../swarm-test" } | ||
| rand = "0.8.5" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2023 Protocol Labs. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a | ||
| // copy of this software and associated documentation files (the "Software"), | ||
| // to deal in the Software without restriction, including without limitation | ||
| // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| // and/or sell copies of the Software, and to permit persons to whom the | ||
| // Software is furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| // DEALINGS IN THE SOFTWARE. | ||
|
|
||
| use super::*; | ||
| use libp2p_swarm::ConnectionDenied; | ||
|
|
||
| /// The configurable memory usage based connection limits. | ||
| #[derive(Debug)] | ||
| pub(crate) struct MemoryUsageBasedConnectionLimits { | ||
| max_process_memory_usage_bytes: Option<usize>, | ||
| max_process_memory_usage_percentage: Option<f64>, | ||
| system_physical_memory_bytes: usize, | ||
| } | ||
|
|
||
| impl MemoryUsageBasedConnectionLimits { | ||
| pub(crate) fn new() -> Self { | ||
| use sysinfo::{RefreshKind, SystemExt}; | ||
|
|
||
| let system_info = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory()); | ||
|
|
||
| MemoryUsageBasedConnectionLimits { | ||
| max_process_memory_usage_bytes: None, | ||
| max_process_memory_usage_percentage: None, | ||
| system_physical_memory_bytes: system_info.total_memory() as usize, | ||
| } | ||
| } | ||
|
|
||
| /// Sets the process memory usage threshold in bytes, | ||
| /// all pending connections will be dropped when the threshold is exeeded | ||
| pub(crate) fn with_max_bytes(mut self, bytes: usize) -> Self { | ||
| self.max_process_memory_usage_bytes = Some(bytes); | ||
| self | ||
| } | ||
|
|
||
| /// Sets the process memory usage threshold in the percentage of the total physical memory, | ||
| /// all pending connections will be dropped when the threshold is exeeded | ||
| pub(crate) fn with_max_percentage(mut self, percentage: f64) -> Self { | ||
| self.max_process_memory_usage_percentage = Some(percentage); | ||
| self | ||
| } | ||
|
|
||
| pub(crate) fn check_limit( | ||
| &self, | ||
| mem_tracker: &mut ProcessMemoryUsageTracker, | ||
| ) -> Result<(), ConnectionDenied> { | ||
| if let Some(max_allowed_bytes) = self.max_allowed_bytes() { | ||
| mem_tracker.refresh_if_needed(); | ||
| if mem_tracker.physical_bytes > max_allowed_bytes { | ||
| return Err(ConnectionDenied::new(MemoryUsageLimitExceeded { | ||
| process_physical_memory_bytes: mem_tracker.physical_bytes, | ||
| max_allowed_bytes, | ||
| })); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn max_allowed_bytes(&self) -> Option<usize> { | ||
| let max_process_memory_usage_percentage = self | ||
| .max_process_memory_usage_percentage | ||
| .map(|p| (self.system_physical_memory_bytes as f64 * p).round() as usize); | ||
| match ( | ||
| self.max_process_memory_usage_bytes, | ||
| max_process_memory_usage_percentage, | ||
| ) { | ||
| (None, None) => None, | ||
| (Some(a), Some(b)) => Some(a.min(b)), | ||
| (Some(a), None) => Some(a), | ||
| (None, Some(b)) => Some(b), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for MemoryUsageBasedConnectionLimits { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright 2023 Protocol Labs. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a | ||
| // copy of this software and associated documentation files (the "Software"), | ||
| // to deal in the Software without restriction, including without limitation | ||
| // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| // and/or sell copies of the Software, and to permit persons to whom the | ||
| // Software is furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| // DEALINGS IN THE SOFTWARE. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A connection limit has been exceeded. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct MemoryUsageLimitExceeded { | ||
| pub process_physical_memory_bytes: usize, | ||
| pub max_allowed_bytes: usize, | ||
| } | ||
|
|
||
| impl std::error::Error for MemoryUsageLimitExceeded {} | ||
|
|
||
| impl fmt::Display for MemoryUsageLimitExceeded { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!( | ||
| f, | ||
| "process physical memory usage limit exceeded: process memory: {} bytes, max allowed: {} bytes", | ||
| self.process_physical_memory_bytes, | ||
| self.max_allowed_bytes, | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.