From e463108cc1cb3449a0b665d43863970391e0ff9f Mon Sep 17 00:00:00 2001 From: Darius Date: Wed, 5 Apr 2023 23:27:15 -0400 Subject: [PATCH 1/9] fix(relay): Export `rate-limiter` module --- protocols/relay/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/relay/src/lib.rs b/protocols/relay/src/lib.rs index 05b6292c9e2..a9c5a5a6b08 100644 --- a/protocols/relay/src/lib.rs +++ b/protocols/relay/src/lib.rs @@ -37,7 +37,7 @@ mod proto { pub use self::message_v2::pb::{HopMessage, Limit, Peer, Reservation, Status, StopMessage}; } -pub use behaviour::{Behaviour, CircuitId, Config, Event}; +pub use behaviour::{Behaviour, CircuitId, Config, Event, rate_limiter}; pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME}; /// Types related to the relay protocol inbound. From 8691090b0745f2022d45246e855bebb91db83fc3 Mon Sep 17 00:00:00 2001 From: Darius Date: Wed, 5 Apr 2023 23:35:04 -0400 Subject: [PATCH 2/9] chore: Update CHANGELOG.md and Cargo.toml --- Cargo.lock | 2 +- protocols/relay/CHANGELOG.md | 6 ++++++ protocols/relay/Cargo.toml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7baeaf5a902..b70e7f5b714 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2773,7 +2773,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.15.1" +version = "0.15.2" dependencies = [ "asynchronous-codec", "bytes", diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index c39e7014bb6..f6b32681acc 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.15.2 - unreleased + +- Export `rate-limiter` module. See [PR 3742]. + +[PR 3742]: https://github.com/libp2p/rust-libp2p/pull/3742 + ## 0.15.1 - Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312]. diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 357b1b65174..9bb92bb8961 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition = "2021" rust-version = "1.62.0" description = "Communications relaying for libp2p" -version = "0.15.1" +version = "0.15.2" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 44e997cf308097af7f7fe7508b3f0b6b86df1ff7 Mon Sep 17 00:00:00 2001 From: Darius Date: Thu, 6 Apr 2023 01:17:32 -0400 Subject: [PATCH 3/9] chore: Formatted code --- protocols/relay/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/relay/src/lib.rs b/protocols/relay/src/lib.rs index a9c5a5a6b08..2b4b83fc1fc 100644 --- a/protocols/relay/src/lib.rs +++ b/protocols/relay/src/lib.rs @@ -37,7 +37,7 @@ mod proto { pub use self::message_v2::pb::{HopMessage, Limit, Peer, Reservation, Status, StopMessage}; } -pub use behaviour::{Behaviour, CircuitId, Config, Event, rate_limiter}; +pub use behaviour::{rate_limiter, Behaviour, CircuitId, Config, Event}; pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME}; /// Types related to the relay protocol inbound. From 76342443a2c6911a88d4703e35c8217dad14943f Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 13 May 2023 15:23:13 -0400 Subject: [PATCH 4/9] chore: Re-export RateLimiter --- protocols/relay/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/relay/src/lib.rs b/protocols/relay/src/lib.rs index 266d5c20f4c..221265ac951 100644 --- a/protocols/relay/src/lib.rs +++ b/protocols/relay/src/lib.rs @@ -40,7 +40,7 @@ mod proto { }; } -pub use behaviour::{rate_limiter, Behaviour, CircuitId, Config, Event}; +pub use behaviour::{rate_limiter::RateLimiter, Behaviour, CircuitId, Config, Event}; pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME}; /// Types related to the relay protocol inbound. From ac4dd9a18e96be9ceaeeb78a4c892c151fd5c48e Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 13 May 2023 15:39:55 -0400 Subject: [PATCH 5/9] chore: Add `Config::per_peer` and `Config::per_ip` --- protocols/relay/src/behaviour.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index b2d6538fc04..abc99ac1b0f 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -61,6 +61,32 @@ pub struct Config { pub circuit_src_rate_limiters: Vec>, } +impl Config { + pub fn per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self { + self.reservation_rate_limiters + .push(rate_limiter::new_per_peer( + rate_limiter::GenericRateLimiterConfig { limit, interval }, + )); + self.circuit_src_rate_limiters + .push(rate_limiter::new_per_peer( + rate_limiter::GenericRateLimiterConfig { limit, interval }, + )); + self + } + + pub fn per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self { + self.reservation_rate_limiters + .push(rate_limiter::new_per_ip( + rate_limiter::GenericRateLimiterConfig { limit, interval }, + )); + self.circuit_src_rate_limiters + .push(rate_limiter::new_per_ip( + rate_limiter::GenericRateLimiterConfig { limit, interval }, + )); + self + } +} + impl std::fmt::Debug for Config { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Config") From 89751d7101e9c5cf97325a242e9d1950a4fd457a Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 13 May 2023 20:59:17 -0400 Subject: [PATCH 6/9] chore: Update comments --- protocols/relay/src/behaviour/rate_limiter.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/protocols/relay/src/behaviour/rate_limiter.rs b/protocols/relay/src/behaviour/rate_limiter.rs index 31223c309f2..a4a127e1253 100644 --- a/protocols/relay/src/behaviour/rate_limiter.rs +++ b/protocols/relay/src/behaviour/rate_limiter.rs @@ -30,10 +30,10 @@ use std::time::Duration; /// Allows rate limiting access to some resource based on the [`PeerId`] and /// [`Multiaddr`] of a remote peer. -/// -/// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use -/// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system -/// number of a peers IP address. +// +// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use +// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system +// number of a peers IP address. pub trait RateLimiter: Send { fn try_next(&mut self, peer: PeerId, addr: &Multiaddr, now: Instant) -> bool; } @@ -80,9 +80,9 @@ 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. + // 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. + // The interval at which a single token is added to the bucket. pub(crate) interval: Duration, } From 2dc4652276eef3edf2739ba7831e6b37d0ec383a Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 29 Jul 2023 10:59:14 -0400 Subject: [PATCH 7/9] chore: Split Config functions per reservation and circuit-src --- protocols/relay/src/behaviour.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 55404d72a18..8fcfa103c4f 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -62,11 +62,15 @@ pub struct Config { } impl Config { - pub fn per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self { + pub fn reservation_rate_per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self { self.reservation_rate_limiters .push(rate_limiter::new_per_peer( rate_limiter::GenericRateLimiterConfig { limit, interval }, )); + self + } + + pub fn circuit_src_per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self { self.circuit_src_rate_limiters .push(rate_limiter::new_per_peer( rate_limiter::GenericRateLimiterConfig { limit, interval }, @@ -74,11 +78,15 @@ impl Config { self } - pub fn per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self { + pub fn reservation_rate_per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self { self.reservation_rate_limiters .push(rate_limiter::new_per_ip( rate_limiter::GenericRateLimiterConfig { limit, interval }, )); + self + } + + pub fn circuit_src_per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self { self.circuit_src_rate_limiters .push(rate_limiter::new_per_ip( rate_limiter::GenericRateLimiterConfig { limit, interval }, From d8032385c818dda18f86922be3ec36bf26168662 Mon Sep 17 00:00:00 2001 From: Darius Date: Sat, 29 Jul 2023 19:41:36 -0400 Subject: [PATCH 8/9] chore: Update CHANGELOG.md --- protocols/relay/CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 378ada58445..155ce6f85d1 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,8 +1,12 @@ ## 0.16.1 - unreleased +- Export `rate-limiter` module. + See [PR 3742]. + - Add functions to access data within `Limit`. See [PR 4162]. +[PR 3742]: https://github.com/libp2p/rust-libp2p/pull/3742 [PR 4162]: https://github.com/libp2p/rust-libp2p/pull/4162 ## 0.16.0 @@ -26,15 +30,12 @@ ## 0.15.2 -- Export `rate-limiter` module. See [PR 3742]. - - Send correct `PeerId` in outbound STOP message to client. See [PR 3767]. - As a relay, when forwarding data between relay-connection-source and -destination and vice versa, flush write side when read currently has no more data available. See [PR 3765]. -[PR 3742]: https://github.com/libp2p/rust-libp2p/pull/3742 [PR 3767]: https://github.com/libp2p/rust-libp2p/pull/3767 [PR 3765]: https://github.com/libp2p/rust-libp2p/pull/3765 From 0c07632e899affd80e86d537cd2ffc40d85ccaac Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 31 Jul 2023 15:25:27 +0200 Subject: [PATCH 9/9] Update protocols/relay/CHANGELOG.md --- protocols/relay/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 155ce6f85d1..46e27bc7bf9 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.16.1 - unreleased -- Export `rate-limiter` module. +- Export `RateLimiter` type. See [PR 3742]. - Add functions to access data within `Limit`.