Skip to content

Commit 8ff2cf3

Browse files
authored
fix(relay): export RateLimiter type
The `rate-limiter` module was not made publicly available. This change exports the `RateLimiter` trait defined within that module which allows users to define their own rate limiters. To make common usecases easy, we also provide a set of constructor functions for common rate limiters. Resolves #3741. Pull-Request: #3742.
1 parent 3af3d5d commit 8ff2cf3

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

protocols/relay/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
## 0.16.1 - unreleased
22

3+
- Export `RateLimiter` type.
4+
See [PR 3742].
5+
36
- Add functions to access data within `Limit`.
47
See [PR 4162].
58

9+
[PR 3742]: https://github.com/libp2p/rust-libp2p/pull/3742
610
[PR 4162]: https://github.com/libp2p/rust-libp2p/pull/4162
711

812
## 0.16.0

protocols/relay/src/behaviour.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,40 @@ pub struct Config {
6161
pub circuit_src_rate_limiters: Vec<Box<dyn rate_limiter::RateLimiter>>,
6262
}
6363

64+
impl Config {
65+
pub fn reservation_rate_per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self {
66+
self.reservation_rate_limiters
67+
.push(rate_limiter::new_per_peer(
68+
rate_limiter::GenericRateLimiterConfig { limit, interval },
69+
));
70+
self
71+
}
72+
73+
pub fn circuit_src_per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self {
74+
self.circuit_src_rate_limiters
75+
.push(rate_limiter::new_per_peer(
76+
rate_limiter::GenericRateLimiterConfig { limit, interval },
77+
));
78+
self
79+
}
80+
81+
pub fn reservation_rate_per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self {
82+
self.reservation_rate_limiters
83+
.push(rate_limiter::new_per_ip(
84+
rate_limiter::GenericRateLimiterConfig { limit, interval },
85+
));
86+
self
87+
}
88+
89+
pub fn circuit_src_per_ip(mut self, limit: NonZeroU32, interval: Duration) -> Self {
90+
self.circuit_src_rate_limiters
91+
.push(rate_limiter::new_per_ip(
92+
rate_limiter::GenericRateLimiterConfig { limit, interval },
93+
));
94+
self
95+
}
96+
}
97+
6498
impl std::fmt::Debug for Config {
6599
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66100
f.debug_struct("Config")

protocols/relay/src/behaviour/rate_limiter.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ use std::time::Duration;
3030

3131
/// Allows rate limiting access to some resource based on the [`PeerId`] and
3232
/// [`Multiaddr`] of a remote peer.
33-
///
34-
/// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use
35-
/// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system
36-
/// number of a peers IP address.
33+
//
34+
// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use
35+
// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system
36+
// number of a peers IP address.
3737
pub trait RateLimiter: Send {
3838
fn try_next(&mut self, peer: PeerId, addr: &Multiaddr, now: Instant) -> bool;
3939
}
@@ -80,9 +80,9 @@ pub(crate) struct GenericRateLimiter<Id> {
8080
/// Configuration for a [`GenericRateLimiter`].
8181
#[derive(Debug, Clone, Copy)]
8282
pub(crate) struct GenericRateLimiterConfig {
83-
/// The maximum number of tokens in the bucket at any point in time.
83+
// The maximum number of tokens in the bucket at any point in time.
8484
pub(crate) limit: NonZeroU32,
85-
/// The interval at which a single token is added to the bucket.
85+
// The interval at which a single token is added to the bucket.
8686
pub(crate) interval: Duration,
8787
}
8888

protocols/relay/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod proto {
3939
};
4040
}
4141

42-
pub use behaviour::{Behaviour, CircuitId, Config, Event};
42+
pub use behaviour::{rate_limiter::RateLimiter, Behaviour, CircuitId, Config, Event};
4343
pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME};
4444

4545
/// Types related to the relay protocol inbound.

0 commit comments

Comments
 (0)