pub struct FloodProtection {
pub config: FloodConfig,
pub global_tokens: f64,
pub global_last_refill: u64,
pub peers: HashMap<String, PeerState>,
pub seen_messages: VecDeque<(MessageId, u64)>,
pub violations: VecDeque<ViolationRecord>,
pub total_allowed: u64,
pub total_blocked: u64,
}Expand description
Multi-layer flood/DoS protection for P2P networks.
See the module-level documentation for a detailed description of each protection layer and usage examples.
Fields§
§config: FloodConfigImmutable configuration.
global_tokens: f64Global token-bucket state.
global_last_refill: u64Timestamp of last global token refill.
peers: HashMap<String, PeerState>Per-peer token-bucket and ban state.
seen_messages: VecDeque<(MessageId, u64)>Sliding-window deduplication cache: (message_id, insertion_timestamp_ms).
violations: VecDeque<ViolationRecord>Ring buffer of recent violations (capped at 1 000).
total_allowed: u64Monotonically increasing count of allowed messages.
total_blocked: u64Monotonically increasing count of blocked messages.
Implementations§
Source§impl FloodProtection
impl FloodProtection
Sourcepub fn new(config: FloodConfig) -> Self
pub fn new(config: FloodConfig) -> Self
Create a new FloodProtection instance with the given configuration.
Sourcepub fn check(
&mut self,
peer_id: &str,
message_id: MessageId,
now: u64,
) -> CheckResult
pub fn check( &mut self, peer_id: &str, message_id: MessageId, now: u64, ) -> CheckResult
Run all protection checks in priority order.
The checks are applied in this order:
- Peer ban check.
- Global rate limit.
- Per-peer rate limit.
- Message deduplication.
This method is read-only: it does not consume tokens or register the
message. Call record_allowed after receiving
CheckResult::Allow to commit the state change.
Violations triggered here are recorded automatically.
Sourcepub fn record_allowed(&mut self, peer_id: &str, message_id: MessageId, now: u64)
pub fn record_allowed(&mut self, peer_id: &str, message_id: MessageId, now: u64)
Consume one token from the global and per-peer buckets and record the message in the deduplication cache.
This must be called after check returns
CheckResult::Allow and before the next call to check for the same
peer, otherwise the token counts will be stale.
Sourcepub fn record_violation(
&mut self,
peer_id: &str,
vtype: ViolationType,
msg_id: Option<MessageId>,
now: u64,
)
pub fn record_violation( &mut self, peer_id: &str, vtype: ViolationType, msg_id: Option<MessageId>, now: u64, )
Record a violation for peer_id and ban them if they hit the threshold.
The violations ring-buffer is capped at 1 000 entries; oldest entries are silently dropped when the buffer is full.
Sourcepub fn is_banned(&mut self, peer_id: &str, now: u64) -> bool
pub fn is_banned(&mut self, peer_id: &str, now: u64) -> bool
Return true if peer_id is currently banned.
If a ban has expired at now the peer’s banned_until field is cleared
automatically (lazy unban).
Sourcepub fn evict_expired_dedup(&mut self, now: u64) -> usize
pub fn evict_expired_dedup(&mut self, now: u64) -> usize
Remove deduplication entries older than FloodConfig::dedup_window_ms.
Returns the number of entries removed.
Sourcepub fn evict_stale_peers(&mut self, max_age_ms: u64, now: u64) -> usize
pub fn evict_stale_peers(&mut self, max_age_ms: u64, now: u64) -> usize
Remove unbanned peers whose last_refill is older than max_age_ms.
Banned peers are never evicted regardless of age. Returns the number of peer entries removed.
Sourcepub fn violation_summary(&self, since: u64) -> HashMap<String, usize>
pub fn violation_summary(&self, since: u64) -> HashMap<String, usize>
Summarise violations recorded since since (ms timestamp).
Returns a map from violation-type name to occurrence count.
Sourcepub fn stats(&self, now: u64) -> FloodStats
pub fn stats(&self, now: u64) -> FloodStats
Return a point-in-time statistics snapshot.
Auto Trait Implementations§
impl Freeze for FloodProtection
impl RefUnwindSafe for FloodProtection
impl Send for FloodProtection
impl Sync for FloodProtection
impl Unpin for FloodProtection
impl UnsafeUnpin for FloodProtection
impl UnwindSafe for FloodProtection
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
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>
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