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

fix(relay): export RateLimiter type #3742

Merged
merged 20 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

## 0.15.2

- Export `rate-limiter` module. See [PR 3742].
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

- 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

Expand Down
26 changes: 26 additions & 0 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ pub struct Config {
pub circuit_src_rate_limiters: Vec<Box<dyn rate_limiter::RateLimiter>>,
}

impl Config {
pub fn per_peer(mut self, limit: NonZeroU32, interval: Duration) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want these function to be per reservation_rate_limiters and circuit_src_rate_limiters or do we want to have it so we populate both vectors per function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure I follow the question. I would expect a user to want to set a different limit for reservations than for circuits, with the rational that a reservation is less expensive than a circuit. Does that make sense? I am not sure though whether we really need to support this use-case via the easy-path, instead of deferring the user to the rate-limiter primitives directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mxinden see #3742 (comment).

I think splitting as better as per @mxinden's reasoning. We only provide these functions to be able to hide more details from the public API. The user should still be able to make fine-grained configuration changes otherwise there is no point in providing them I think.

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")
Expand Down
12 changes: 6 additions & 6 deletions protocols/relay/src/behaviour/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -80,9 +80,9 @@ 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.
// 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,
}

Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod proto {
};
}

pub use behaviour::{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.
Expand Down