Skip to content

Commit

Permalink
feat(relay): allow to configure default rate limiters
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Aug 30, 2024
1 parent 95f40ff commit 011ab6d
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 5 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

<!-- Update to libp2p-swarm v0.45.0 -->
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>", "Max Inden <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions protocols/relay/src/behaviour/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn RateLimiter> {
/// 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<dyn RateLimiter> {
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<dyn RateLimiter> {
/// 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<dyn RateLimiter> {
let mut limiter = GenericRateLimiter::new(config);
Box::new(move |_peer_id, addr: &Multiaddr, now| {
multiaddr_to_ip(addr)
Expand Down Expand Up @@ -78,11 +80,11 @@ pub(crate) struct GenericRateLimiter<Id> {

/// 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<Id: Eq + PartialEq + Hash + Clone> GenericRateLimiter<Id> {
Expand Down
5 changes: 4 additions & 1 deletion protocols/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 011ab6d

Please sign in to comment.