Skip to content
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

feat: memory based connection limits #4281

Merged
merged 41 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
90cd7d6
feat: memory based connection limits
hanabi1224 Aug 1, 2023
5014500
sets system ram
hanabi1224 Aug 1, 2023
e5519d7
code golf
hanabi1224 Aug 1, 2023
024da42
fix(examples/file-sharing): set Kademlia `Mode::Server`
mxinden Aug 1, 2023
065efb1
docs: fix last OutboundQueryCompleted in kad doc
thomas-senechal Aug 2, 2023
2a8e371
fix(relay): remove unconditional `async-std` dependency
quininer Aug 2, 2023
94dc11b
deps(yamux): update yamux to `v0.12`
thomaseizinger Aug 2, 2023
51a91b7
deps: bump Swatinem/rust-cache from 2.5.1 to 2.6.0
dependabot[bot] Aug 3, 2023
6581822
move to new crate
hanabi1224 Aug 3, 2023
1106ad7
Merge remote-tracking branch 'origin/master' into memory-based-connec…
hanabi1224 Aug 3, 2023
6066b2e
doc
hanabi1224 Aug 3, 2023
1fa32f0
lock
hanabi1224 Aug 3, 2023
05cbf5e
doc and tests
hanabi1224 Aug 3, 2023
e308be7
Merge branch 'master' into memory-based-connection-limits
hanabi1224 Aug 3, 2023
7d05ee5
Apply suggestions from code review
hanabi1224 Aug 3, 2023
9ba799e
hide MemoryUsageBasedConnectionLimits
hanabi1224 Aug 3, 2023
b8a9916
update MemoryUsageLimitExceeded fmt as suggested
hanabi1224 Aug 3, 2023
f43fed5
fix clippy
hanabi1224 Aug 3, 2023
9a66866
fix rustdoc
hanabi1224 Aug 3, 2023
7990be6
Update misc/memory-connection-limits/src/lib.rs
hanabi1224 Aug 4, 2023
07e30bb
resolve comments
hanabi1224 Aug 4, 2023
6adf21f
Make MemoryUsageLimitExceeded fields private
hanabi1224 Aug 4, 2023
a720f64
Merge branch 'master' into memory-based-connection-limits
hanabi1224 Aug 7, 2023
803d8f5
Pre-calculate max_allowed_bytes
hanabi1224 Aug 7, 2023
f60cfb0
refresh interval
hanabi1224 Aug 7, 2023
259f038
Don't use `update` functions in tests
thomaseizinger Aug 7, 2023
c5e103c
Remove inner `Option`
thomaseizinger Aug 7, 2023
5c63b87
Don't make refresh interval configurable
thomaseizinger Aug 7, 2023
dee261c
Update docs
thomaseizinger Aug 7, 2023
1b7fb24
Remove refresh-interval field
thomaseizinger Aug 7, 2023
280d13a
Use early return to reduce indentation
thomaseizinger Aug 7, 2023
aadad16
Only set timestamp if we actually refreshed the data
thomaseizinger Aug 7, 2023
2875817
Use early return instead of else
thomaseizinger Aug 7, 2023
800d501
Make tests for stable
hanabi1224 Aug 7, 2023
7331b8f
Add docs to constant
thomaseizinger Aug 7, 2023
a1d0b55
Sort functions are per trait definition
thomaseizinger Aug 7, 2023
9da8e1e
Merge branch 'memory-connection-limits-thomas' into memory-based-conn…
thomaseizinger Aug 7, 2023
b24fb86
Re-export from libp2p meta crate
hanabi1224 Aug 7, 2023
14f0866
Merge branch 'master' into memory-based-connection-limits
hanabi1224 Aug 7, 2023
9eeee38
Update libp2p/CHANGELOG.md
thomaseizinger Aug 7, 2023
13f09a8
Merge branch 'master' into memory-based-connection-limits
hanabi1224 Aug 8, 2023
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
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"misc/allow-block-list",
"misc/connection-limits",
"misc/keygen",
"misc/memory-connection-limits",
"misc/metrics",
"misc/multistream-select",
"misc/quick-protobuf-codec",
Expand Down Expand Up @@ -75,6 +76,7 @@ libp2p-identify = { version = "0.43.0", path = "protocols/identify" }
libp2p-identity = { version = "0.2.2" }
libp2p-kad = { version = "0.44.4", path = "protocols/kad" }
libp2p-mdns = { version = "0.44.0", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.1.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.13.1", path = "misc/metrics" }
libp2p-mplex = { version = "0.40.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
Expand Down
3 changes: 3 additions & 0 deletions misc/memory-connection-limits/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0 - unreleased

- Initial release.
26 changes: 26 additions & 0 deletions misc/memory-connection-limits/Cargo.toml
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 = { version = "1", features = ["always_use_statm"] }
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"
231 changes: 231 additions & 0 deletions misc/memory-connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// 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 libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use void::Void;

use std::{
fmt,
task::{Context, Poll},
};

/// A [`NetworkBehaviour`] that enforces a set of memory usage based limits.
///
/// For these limits to take effect, this needs to be composed into the behaviour tree of your application.
///
/// If a connection is denied due to a limit, either a [`SwarmEvent::IncomingConnectionError`](libp2p_swarm::SwarmEvent::IncomingConnectionError)
/// or [`SwarmEvent::OutgoingConnectionError`](libp2p_swarm::SwarmEvent::OutgoingConnectionError) will be emitted.
/// The [`ListenError::Denied`](libp2p_swarm::ListenError::Denied) and respectively the [`DialError::Denied`](libp2p_swarm::DialError::Denied) variant
/// contain a [`ConnectionDenied`](libp2p_swarm::ConnectionDenied) type that can be downcast to [`MemoryUsageLimitExceeded`] error if (and only if) **this**
/// behaviour denied the connection.
///
/// If you employ multiple [`NetworkBehaviour`]s that manage connections, it may also be a different error.
///
/// # Example
///
/// ```rust
/// # use libp2p_identify as identify;
/// # use libp2p_swarm_derive::NetworkBehaviour;
/// # use libp2p_memory_connection_limits as memory_connection_limits;
///
/// #[derive(NetworkBehaviour)]
/// # #[behaviour(prelude = "libp2p_swarm::derive_prelude")]
/// struct MyBehaviour {
/// identify: identify::Behaviour,
/// limits: memory_connection_limits::Behaviour
/// }
/// ```
pub struct Behaviour {
max_process_memory_usage_bytes: Option<usize>,
max_process_memory_usage_percentage: Option<f64>,
system_physical_memory_bytes: usize,
}

impl Behaviour {
/// Sets the process memory usage threshold in absolute bytes.
///
/// New inbound and outbound connections will be denied when the threshold is reached.
pub fn with_max_bytes(bytes: usize) -> Self {
let mut b = Self::new();
b.update_max_bytes(bytes);
b
}

/// 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 fn with_max_percentage(percentage: f64) -> Self {
let mut b = Self::new();
b.update_max_percentage(percentage);
b
}

/// Updates the process memory usage threshold in bytes,
pub fn update_max_bytes(&mut self, bytes: usize) {
self.max_process_memory_usage_bytes = Some(bytes);
}

/// Updates the process memory usage threshold in the percentage of the total physical memory,
pub fn update_max_percentage(&mut self, percentage: f64) {
self.max_process_memory_usage_percentage = Some(percentage);
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

fn new() -> Self {
use sysinfo::{RefreshKind, SystemExt};

let system_info = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());

Self {
max_process_memory_usage_bytes: None,
max_process_memory_usage_percentage: None,
system_physical_memory_bytes: system_info.total_memory() as usize,
}
}

fn check_limit(&self) -> Result<(), ConnectionDenied> {
if let Some(max_allowed_bytes) = self.max_allowed_bytes() {
if let Some(stats) = memory_stats::memory_stats() {
if stats.physical_mem > max_allowed_bytes {
return Err(ConnectionDenied::new(MemoryUsageLimitExceeded {
process_physical_memory_bytes: stats.physical_mem,
max_allowed_bytes,
}));
}
} else {
log::warn!("Failed to retrive process memory stats");
}
}

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,
) {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
(None, None) => None,
(Some(a), Some(b)) => Some(a.min(b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
}
}
}

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = dummy::ConnectionHandler;
type ToSwarm = Void;

fn handle_pending_inbound_connection(
&mut self,
_: ConnectionId,
_: &Multiaddr,
_: &Multiaddr,
) -> Result<(), ConnectionDenied> {
self.check_limit()
}

fn handle_pending_outbound_connection(
&mut self,
_: ConnectionId,
_: Option<PeerId>,
_: &[Multiaddr],
_: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
self.check_limit()?;
Ok(vec![])
}

fn handle_established_inbound_connection(
&mut self,
_: ConnectionId,
_: PeerId,
_: &Multiaddr,
_: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}

fn handle_established_outbound_connection(
&mut self,
_: ConnectionId,
_: PeerId,
_: &Multiaddr,
_: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, _: FromSwarm<Self::ConnectionHandler>) {}

fn on_connection_handler_event(
&mut self,
_id: PeerId,
_: ConnectionId,
event: THandlerOutEvent<Self>,
) {
void::unreachable(event)
}

fn poll(
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
Poll::Pending
}
}

/// A connection limit has been exceeded.
#[derive(Debug, Clone, Copy)]
pub struct MemoryUsageLimitExceeded {
process_physical_memory_bytes: usize,
max_allowed_bytes: usize,
}

impl MemoryUsageLimitExceeded {
pub fn process_physical_memory_bytes(&self) -> usize {
self.process_physical_memory_bytes
}

pub fn max_allowed_bytes(&self) -> usize {
self.max_allowed_bytes
}
}

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,
)
}
}
Loading