Skip to main content

FloodProtection

Struct FloodProtection 

Source
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: FloodConfig

Immutable configuration.

§global_tokens: f64

Global token-bucket state.

§global_last_refill: u64

Timestamp 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: u64

Monotonically increasing count of allowed messages.

§total_blocked: u64

Monotonically increasing count of blocked messages.

Implementations§

Source§

impl FloodProtection

Source

pub fn new(config: FloodConfig) -> Self

Create a new FloodProtection instance with the given configuration.

Source

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:

  1. Peer ban check.
  2. Global rate limit.
  3. Per-peer rate limit.
  4. 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.

Source

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.

Source

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.

Source

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).

Source

pub fn unban(&mut self, peer_id: &str)

Unconditionally lift the ban on peer_id.

Source

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.

Source

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.

Source

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.

Source

pub fn stats(&self, now: u64) -> FloodStats

Return a point-in-time statistics snapshot.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more