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(relay): allow to configure default rate limiters #5584

Closed
Changes from all commits
Commits
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
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
@@ -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" }
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 5584](https://github.com/libp2p/rust-libp2p/pull/5584).

## 0.18.0

<!-- Update to libp2p-swarm v0.45.0 -->
2 changes: 1 addition & 1 deletion protocols/relay/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <admin@parity.io>", "Max Inden <mail@max-inden.de>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
2 changes: 1 addition & 1 deletion protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 10 additions & 8 deletions protocols/relay/src/behaviour/rate_limiter.rs
Original file line number Diff line number Diff line change
@@ -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)
@@ -76,13 +78,13 @@ pub(crate) struct GenericRateLimiter<Id> {
buckets: HashMap<Id, u32>,
}

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