pub struct EntityManager { /* private fields */ }Expand description
Thread-safe entity manager using DashMap.
Provides lock-free concurrent access to entity state for high-RPS WAF scenarios. Uses timestamp-based LRU eviction instead of ordered list for better concurrency.
SECURITY: Implements per-site entity quotas to prevent a single tenant from filling the global pool and degrading security for all tenants.
Implementations§
Source§impl EntityManager
impl EntityManager
Sourcepub fn new(config: EntityConfig) -> Self
pub fn new(config: EntityConfig) -> Self
Create a new entity manager with the given configuration.
Sourcepub fn config(&self) -> &EntityConfig
pub fn config(&self) -> &EntityConfig
Get the configuration.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Check if entity tracking is enabled.
Sourcepub fn metrics(&self) -> EntityMetrics
pub fn metrics(&self) -> EntityMetrics
Get metrics about the entity store.
Sourcepub fn touch_entity(&self, ip: &str) -> EntitySnapshot
pub fn touch_entity(&self, ip: &str) -> EntitySnapshot
Touch an entity (update last_seen, apply decay, increment request_count).
Creates the entity if it doesn’t exist. Returns a snapshot of the entity state.
Sourcepub fn touch_entity_with_fingerprint(
&self,
ip: &str,
ja4: Option<&str>,
combined: Option<&str>,
) -> EntitySnapshot
pub fn touch_entity_with_fingerprint( &self, ip: &str, ja4: Option<&str>, combined: Option<&str>, ) -> EntitySnapshot
Touch an entity and associate fingerprint.
Sourcepub fn touch_entity_for_site(
&self,
ip: &str,
site_id: &str,
) -> Option<EntitySnapshot>
pub fn touch_entity_for_site( &self, ip: &str, site_id: &str, ) -> Option<EntitySnapshot>
Touch an entity for a specific site/tenant.
SECURITY: Enforces per-site entity limits to prevent a single tenant from exhausting the global entity pool and degrading security for all tenants.
Returns None if the site has exceeded its quota and the entity doesn’t exist.
Sourcepub fn get_site_count(&self, site_id: &str) -> u64
pub fn get_site_count(&self, site_id: &str) -> u64
Get the current entity count for a site.
Sourcepub fn site_metrics(&self) -> Vec<SiteMetrics>
pub fn site_metrics(&self) -> Vec<SiteMetrics>
Get site metrics for monitoring.
Sourcepub fn get_entity(&self, ip: &str) -> Option<EntitySnapshot>
pub fn get_entity(&self, ip: &str) -> Option<EntitySnapshot>
Get an entity snapshot (read-only).
Sourcepub fn apply_rule_risk(
&self,
ip: &str,
rule_id: u32,
base_risk: f64,
enable_multiplier: bool,
) -> Option<RiskApplication>
pub fn apply_rule_risk( &self, ip: &str, rule_id: u32, base_risk: f64, enable_multiplier: bool, ) -> Option<RiskApplication>
Apply risk from a matched rule.
Returns the risk application result, or None if entity doesn’t exist.
Sourcepub fn apply_external_risk(&self, ip: &str, risk: f64, reason: &str) -> f64
pub fn apply_external_risk(&self, ip: &str, risk: f64, reason: &str) -> f64
Apply external risk (e.g., from anomaly detection).
Creates the entity if it doesn’t exist.
§Arguments
ip- Client IP addressrisk- Risk points to add (will be clamped to max_risk)reason- Reason for risk application (logged at debug level)
Sourcepub fn apply_anomaly_risk(
&self,
ip: &str,
anomaly_type: &str,
risk: f64,
details: Option<&str>,
) -> f64
pub fn apply_anomaly_risk( &self, ip: &str, anomaly_type: &str, risk: f64, details: Option<&str>, ) -> f64
Apply anomaly-based risk to an entity.
Used for behavioral anomalies like honeypot hits, rapid fingerprint changes, etc. Creates the entity if it doesn’t exist.
§Arguments
ip- Client IP addressanomaly_type- Type of anomaly detected (e.g., “honeypot_hit”, “ja4_rapid_change”)risk- Risk points to adddetails- Optional details about the anomaly
Sourcepub fn check_block(&self, ip: &str) -> BlockDecision
pub fn check_block(&self, ip: &str) -> BlockDecision
Check if an entity should be blocked based on risk threshold.
Returns the block decision.
Sourcepub fn manual_block(&self, ip: &str, reason: &str) -> bool
pub fn manual_block(&self, ip: &str, reason: &str) -> bool
Manually block an entity.
Sourcepub fn release_entity(&self, ip: &str) -> bool
pub fn release_entity(&self, ip: &str) -> bool
Release an entity (reset risk and unblock).
Sourcepub fn release_all(&self) -> usize
pub fn release_all(&self) -> usize
Release all entities (reset risk and unblock all).
Returns the number of entities released.
Sourcepub fn list_entity_ids(&self) -> Vec<String>
pub fn list_entity_ids(&self) -> Vec<String>
List all entity IDs.
Sourcepub fn list_top_risk(&self, limit: usize) -> Vec<EntitySnapshot>
pub fn list_top_risk(&self, limit: usize) -> Vec<EntitySnapshot>
Returns top N entities sorted by risk score (highest first)
Sourcepub fn check_ja4_reputation(
&self,
ip: &str,
current_ja4: &str,
now_ms: u64,
) -> Option<Ja4ReputationResult>
pub fn check_ja4_reputation( &self, ip: &str, current_ja4: &str, now_ms: u64, ) -> Option<Ja4ReputationResult>
Sourcepub fn snapshot(&self) -> Vec<EntityState>
pub fn snapshot(&self) -> Vec<EntityState>
Create a snapshot of all entity states for persistence.
Returns a Vec of cloned EntityState suitable for serialization.
Sourcepub fn restore(&self, entities: Vec<EntityState>)
pub fn restore(&self, entities: Vec<EntityState>)
Restore entity states from a persisted snapshot.
Clears existing entities and inserts the restored ones. Updates total_created counter to reflect restored count. Rebuilds site_counts from restored entity site_id fields.
Sourcepub fn merge_restore(&self, entities: Vec<EntityState>) -> usize
pub fn merge_restore(&self, entities: Vec<EntityState>) -> usize
Merge restored entities with existing ones (additive restore).
Only inserts entities that don’t already exist. Useful for partial recovery scenarios. Updates site_counts for newly merged entities.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for EntityManager
impl !RefUnwindSafe for EntityManager
impl Send for EntityManager
impl Sync for EntityManager
impl Unpin for EntityManager
impl UnsafeUnpin for EntityManager
impl UnwindSafe for EntityManager
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
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