pub struct MemDdosProtection { /* private fields */ }Expand description
In-memory DDoS protection with linearly decaying per-IP scores.
Each allow_request adds 1.0 point, each report_bad_request adds
bad_request_penalty points. Between calls the score drains at
decay_per_second points/second, so sustained low-rate traffic stabilises
well below the threshold while bursts trigger quickly.
Scores are stored in a moka cache whose idle expiry is long enough for any maxed-out score to fully decay, keeping memory bounded.
Implementations§
Trait Implementations§
Source§impl DdosProtection for MemDdosProtection
impl DdosProtection for MemDdosProtection
Source§fn allow_request(&self, ip: &str) -> bool
fn allow_request(&self, ip: &str) -> bool
Returns
true if the request from ip should be processed, false if it should be
dropped immediately.Source§fn report_bad_request(&self, ip: &str)
fn report_bad_request(&self, ip: &str)
Notify the implementation that a request from
ip was rejected. Implementations
should use this to accumulate evidence and eventually ban repeat offenders.Source§fn try_acquire_connection(&self, ip: &str) -> bool
fn try_acquire_connection(&self, ip: &str) -> bool
Try to acquire a connection slot for
ip, checking both the ban score and the
per-IP connection cap. Returns true and increments the connection count on
success. Returns false if the IP is blocked or over the per-IP cap. Read moreSource§fn release_connection(&self, ip: &str)
fn release_connection(&self, ip: &str)
Release a connection slot previously acquired by
try_acquire_connection.
Called automatically by DdosConnectionGuard::drop.Auto Trait Implementations§
impl !Freeze for MemDdosProtection
impl !RefUnwindSafe for MemDdosProtection
impl Send for MemDdosProtection
impl Sync for MemDdosProtection
impl Unpin for MemDdosProtection
impl UnsafeUnpin for MemDdosProtection
impl !UnwindSafe for MemDdosProtection
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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