mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 18:11:22 +00:00
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.
This commit is contained in:
@ -1,8 +1,12 @@
|
|||||||
## 0.16.1 - unreleased
|
## 0.16.1 - unreleased
|
||||||
|
|
||||||
|
- Export `RateLimiter` type.
|
||||||
|
See [PR 3742].
|
||||||
|
|
||||||
- Add functions to access data within `Limit`.
|
- Add functions to access data within `Limit`.
|
||||||
See [PR 4162].
|
See [PR 4162].
|
||||||
|
|
||||||
|
[PR 3742]: https://github.com/libp2p/rust-libp2p/pull/3742
|
||||||
[PR 4162]: https://github.com/libp2p/rust-libp2p/pull/4162
|
[PR 4162]: https://github.com/libp2p/rust-libp2p/pull/4162
|
||||||
|
|
||||||
## 0.16.0
|
## 0.16.0
|
||||||
|
@ -61,6 +61,40 @@ pub struct Config {
|
|||||||
pub circuit_src_rate_limiters: Vec<Box<dyn rate_limiter::RateLimiter>>,
|
pub circuit_src_rate_limiters: Vec<Box<dyn rate_limiter::RateLimiter>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
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 },
|
||||||
|
));
|
||||||
|
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 },
|
||||||
|
));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for Config {
|
impl std::fmt::Debug for Config {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("Config")
|
f.debug_struct("Config")
|
||||||
|
@ -30,10 +30,10 @@ use std::time::Duration;
|
|||||||
|
|
||||||
/// Allows rate limiting access to some resource based on the [`PeerId`] and
|
/// Allows rate limiting access to some resource based on the [`PeerId`] and
|
||||||
/// [`Multiaddr`] of a remote peer.
|
/// [`Multiaddr`] of a remote peer.
|
||||||
///
|
//
|
||||||
/// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use
|
// See [`new_per_peer`] and [`new_per_ip`] for precast implementations. Use
|
||||||
/// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system
|
// [`GenericRateLimiter`] to build your own, e.g. based on the autonomous system
|
||||||
/// number of a peers IP address.
|
// number of a peers IP address.
|
||||||
pub trait RateLimiter: Send {
|
pub trait RateLimiter: Send {
|
||||||
fn try_next(&mut self, peer: PeerId, addr: &Multiaddr, now: Instant) -> bool;
|
fn try_next(&mut self, peer: PeerId, addr: &Multiaddr, now: Instant) -> bool;
|
||||||
}
|
}
|
||||||
@ -80,9 +80,9 @@ pub(crate) struct GenericRateLimiter<Id> {
|
|||||||
/// Configuration for a [`GenericRateLimiter`].
|
/// Configuration for a [`GenericRateLimiter`].
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub(crate) struct GenericRateLimiterConfig {
|
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,
|
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,
|
pub(crate) interval: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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};
|
pub use protocol::{HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME};
|
||||||
|
|
||||||
/// Types related to the relay protocol inbound.
|
/// Types related to the relay protocol inbound.
|
||||||
|
Reference in New Issue
Block a user