pub struct RateLimiter {
pub buckets: HashMap<HostName, TokenBucket>,
pub bypass: HashSet<NodeId>,
}Expand description
Peer rate limiter.
Uses a token bucket algorithm, where each address starts with a certain amount of tokens, and every request from that address consumes one token. Tokens refill at a predefined rate. This mechanism allows for consistent request rates with potential bursts up to the bucket’s capacity.
Fields§
§buckets: HashMap<HostName, TokenBucket>§bypass: HashSet<NodeId>Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(bypass: impl IntoIterator<Item = NodeId>) -> Self
pub fn new(bypass: impl IntoIterator<Item = NodeId>) -> Self
Create a new rate limiter with a bypass list. Nodes in the bypass list are not limited.
Sourcepub fn limit<T: AsTokens>(
&mut self,
addr: HostName,
nid: Option<&NodeId>,
tokens: &T,
now: LocalTime,
) -> bool
pub fn limit<T: AsTokens>( &mut self, addr: HostName, nid: Option<&NodeId>, tokens: &T, now: LocalTime, ) -> bool
Call this when the address has performed some rate-limited action. Returns whether the action is rate-limited or not.
Supplying a different amount of tokens per address is useful if for eg. a peer is outbound vs. inbound.
Trait Implementations§
Source§impl Debug for RateLimiter
impl Debug for RateLimiter
Source§impl Default for RateLimiter
impl Default for RateLimiter
Source§fn default() -> RateLimiter
fn default() -> RateLimiter
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more