From 011ab6d1a979a0b7dbf2fa685b911b817b58ce3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?= Date: Fri, 30 Aug 2024 17:19:33 +0200 Subject: [PATCH] feat(relay): allow to configure default rate limiters --- Cargo.lock | 2 +- Cargo.toml | 2 +- protocols/relay/CHANGELOG.md | 5 +++++ protocols/relay/Cargo.toml | 2 +- protocols/relay/src/behaviour.rs | 2 +- protocols/relay/src/behaviour/rate_limiter.rs | 16 +++++++++------- protocols/relay/src/lib.rs | 5 ++++- 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7f37b652c30..2be3418f718 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3206,7 +3206,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.18.0" +version = "0.18.1" dependencies = [ "asynchronous-codec", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 8d63ac3ee1e..51947ff5f19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ libp2p-ping = { version = "0.45.0", path = "protocols/ping" } libp2p-plaintext = { version = "0.42.0", path = "transports/plaintext" } libp2p-pnet = { version = "0.25.0", path = "transports/pnet" } libp2p-quic = { version = "0.11.1", path = "transports/quic" } -libp2p-relay = { version = "0.18.0", path = "protocols/relay" } +libp2p-relay = { version = "0.18.1", path = "protocols/relay" } libp2p-rendezvous = { version = "0.15.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.27.0", path = "protocols/request-response" } libp2p-server = { version = "0.12.7", path = "misc/server" } diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index fc71ccedad5..69071dee6ce 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.18.1 + +- Allow to configure default rate limiters. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). + ## 0.18.0 diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 084fec07efd..16449f57df5 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition = "2021" rust-version = { workspace = true } description = "Communications relaying for libp2p" -version = "0.18.0" +version = "0.18.1" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 463febf9f2f..5f9ee3caec8 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -21,7 +21,7 @@ //! [`NetworkBehaviour`] to act as a circuit relay v2 **relay**. pub(crate) mod handler; -pub(crate) mod rate_limiter; +pub mod rate_limiter; use crate::behaviour::handler::Handler; use crate::multiaddr_ext::MultiaddrExt; use crate::proto; diff --git a/protocols/relay/src/behaviour/rate_limiter.rs b/protocols/relay/src/behaviour/rate_limiter.rs index 45b701c1b50..feaaca8206c 100644 --- a/protocols/relay/src/behaviour/rate_limiter.rs +++ b/protocols/relay/src/behaviour/rate_limiter.rs @@ -37,12 +37,14 @@ pub trait RateLimiter: Send { fn try_next(&mut self, peer: PeerId, addr: &Multiaddr, now: Instant) -> bool; } -pub(crate) fn new_per_peer(config: GenericRateLimiterConfig) -> Box { +/// For each peer ID one reservation/circuit every `config.interval` minutes with up to `config.limit` reservations per hour. +pub fn new_per_peer(config: GenericRateLimiterConfig) -> Box { let mut limiter = GenericRateLimiter::new(config); Box::new(move |peer_id, _addr: &Multiaddr, now| limiter.try_next(peer_id, now)) } -pub(crate) fn new_per_ip(config: GenericRateLimiterConfig) -> Box { +/// For each source IP address one reservation/circuit every `config.interval` minutes with up to `config.limit` reservations per hour. +pub fn new_per_ip(config: GenericRateLimiterConfig) -> Box { let mut limiter = GenericRateLimiter::new(config); Box::new(move |_peer_id, addr: &Multiaddr, now| { multiaddr_to_ip(addr) @@ -78,11 +80,11 @@ pub(crate) struct GenericRateLimiter { /// Configuration for a [`GenericRateLimiter`]. #[derive(Debug, Clone, Copy)] -pub(crate) struct GenericRateLimiterConfig { - // The maximum number of tokens in the bucket at any point in time. - pub(crate) limit: NonZeroU32, - // The interval at which a single token is added to the bucket. - pub(crate) interval: Duration, +pub struct GenericRateLimiterConfig { + /// The maximum number of tokens in the bucket at any point in time. + pub limit: NonZeroU32, + /// The interval at which a single token is added to the bucket. + pub interval: Duration, } impl GenericRateLimiter { diff --git a/protocols/relay/src/lib.rs b/protocols/relay/src/lib.rs index eca3578d599..f5ab5808071 100644 --- a/protocols/relay/src/lib.rs +++ b/protocols/relay/src/lib.rs @@ -39,7 +39,10 @@ mod proto { }; } -pub use behaviour::{rate_limiter::RateLimiter, Behaviour, CircuitId, Config, Event}; +pub use behaviour::{ + rate_limiter::{self, RateLimiter}, + Behaviour, CircuitId, Config, Event, +}; pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME}; /// Types related to the relay protocol inbound.