pub struct RouterScopedParser<K: Hash + Eq> { /* private fields */ }Expand description
A parser that maintains separate template caches for each NetFlow source.
This is the recommended pattern for multi-source deployments where different routers may use the same template IDs for different field definitions. By keeping separate parser instances per source, templates are properly isolated.
Uses an LRU cache internally for O(1) eviction of the least-recently-used source when at capacity, preventing DoS attacks via source address flooding.
§Type Parameter
K- The key type used to identify sources. CommonlySocketAddrfor UDP sources, but can be any hashable type (e.g.,Stringfor named sources,u32for observation domain IDs, etc.)
§Examples
§Basic usage with SocketAddr
use netflow_parser::RouterScopedParser;
use std::net::SocketAddr;
let mut scoped_parser = RouterScopedParser::<SocketAddr>::new();
// Parse packet from source 1
let source1 = "192.168.1.1:2055".parse().unwrap();
let data1 = vec![/* netflow data */];
let packets = scoped_parser.parse_from_source(source1, &data1);
// Parse packet from source 2 (separate template cache)
let source2 = "192.168.1.2:2055".parse().unwrap();
let data2 = vec![/* netflow data */];
let packets = scoped_parser.parse_from_source(source2, &data2);§Custom source keys
use netflow_parser::RouterScopedParser;
// Use string identifiers for sources
let mut scoped_parser = RouterScopedParser::<String>::new();
let packets = scoped_parser.parse_from_source(
"router-nyc-01".to_string(),
&data
);Implementations§
Source§impl<K: Hash + Eq> RouterScopedParser<K>
impl<K: Hash + Eq> RouterScopedParser<K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new scoped parser with default configuration.
Each new source will get a parser with default settings.
Sourcepub fn try_with_builder(
builder: NetflowParserBuilder,
) -> Result<Self, ConfigError>
pub fn try_with_builder( builder: NetflowParserBuilder, ) -> Result<Self, ConfigError>
Create a new scoped parser with a custom parser builder.
Validates the builder configuration eagerly. Returns an error if the builder configuration is invalid (e.g., zero cache size).
§Errors
Returns ConfigError if the builder configuration is invalid.
§Examples
use netflow_parser::{RouterScopedParser, NetflowParser};
use netflow_parser::variable_versions::ttl::TtlConfig;
use std::time::Duration;
use std::net::SocketAddr;
let builder = NetflowParser::builder()
.with_cache_size(5000)
.with_ttl(TtlConfig::new(Duration::from_secs(3600)));
let scoped_parser = RouterScopedParser::<SocketAddr>::try_with_builder(builder)
.expect("valid config");Sourcepub fn with_max_sources(self, max: usize) -> Result<Self, ConfigError>
pub fn with_max_sources(self, max: usize) -> Result<Self, ConfigError>
Set the maximum number of sources to track.
When at capacity and a new source arrives, the least-recently-used source is automatically evicted to make room. Default: 10,000.
§Errors
Returns ConfigError::InvalidMaxSources if max is 0.
Sourcepub fn with_max_sources_and_reporter<F>(
self,
max: usize,
reporter: &mut F,
) -> Result<Self, ConfigError>
pub fn with_max_sources_and_reporter<F>( self, max: usize, reporter: &mut F, ) -> Result<Self, ConfigError>
Set the maximum number of sources and report every source removed by reducing the capacity.
Reporting completes synchronously before this method returns.
Reporter errors and panics are isolated and counted in
source_removal_metrics.
§Errors
Returns ConfigError::InvalidMaxSources if max is 0.
Sourcepub fn parse_from_source(&mut self, source: K, data: &[u8]) -> ParseResultwhere
K: Clone,
pub fn parse_from_source(&mut self, source: K, data: &[u8]) -> ParseResultwhere
K: Clone,
Parse NetFlow data from a specific source.
This will automatically create a new parser instance for new sources, or reuse the existing parser for known sources. When at capacity, the least-recently-used source is automatically evicted by the LRU cache.
§Arguments
source- The source identifier (e.g., SocketAddr)data- The raw NetFlow packet data
§Returns
A ParseResult containing successfully parsed packets and an optional error.
Sourcepub fn parse_from_source_with_reporter<F>(
&mut self,
source: K,
data: &[u8],
reporter: &mut F,
) -> ParseResult
pub fn parse_from_source_with_reporter<F>( &mut self, source: K, data: &[u8], reporter: &mut F, ) -> ParseResult
Parse data and synchronously report any source displaced by capacity pressure.
Sourcepub fn iter_packets_from_source<'a>(
&'a mut self,
source: K,
data: &'a [u8],
) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K>, NetflowError>where
K: Clone,
pub fn iter_packets_from_source<'a>(
&'a mut self,
source: K,
data: &'a [u8],
) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K>, NetflowError>where
K: Clone,
Sourcepub fn iter_packets_from_source_with_reporter<'a, F>(
&'a mut self,
source: K,
data: &'a [u8],
reporter: &mut F,
) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K, F>, NetflowError>
pub fn iter_packets_from_source_with_reporter<'a, F>( &'a mut self, source: K, data: &'a [u8], reporter: &mut F, ) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K, F>, NetflowError>
Iterate over packets and synchronously report any source displaced by capacity pressure before the iterator is returned.
Sourcepub fn prune_idle_sources(&mut self, older_than: Duration) -> usizewhere
K: Clone,
pub fn prune_idle_sources(&mut self, older_than: Duration) -> usizewhere
K: Clone,
Remove sources not seen within the given duration.
Returns the number of sources pruned.
Sourcepub fn prune_idle_sources_with_reporter<F>(
&mut self,
older_than: Duration,
reporter: &mut F,
) -> usize
pub fn prune_idle_sources_with_reporter<F>( &mut self, older_than: Duration, reporter: &mut F, ) -> usize
Remove idle sources and synchronously report every removal.
Sourcepub fn source_removal_metrics(&self) -> SourceRemovalMetrics
pub fn source_removal_metrics(&self) -> SourceRemovalMetrics
Return aggregate implicit source-removal metrics.
Sourcepub fn get_source_info(&mut self, source: &K) -> Option<ParserCacheInfo>
pub fn get_source_info(&mut self, source: &K) -> Option<ParserCacheInfo>
Get statistics for a specific source’s template cache.
Returns None if the source hasn’t sent any packets yet.
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Get the number of registered sources.
Sourcepub fn all_info(&self) -> Vec<(&K, ParserCacheInfo)>
pub fn all_info(&self) -> Vec<(&K, ParserCacheInfo)>
Get statistics for all sources.
Sourcepub fn clear_source_templates(&mut self, source: &K)
pub fn clear_source_templates(&mut self, source: &K)
Clear templates for a specific source.
This is useful for testing or when you need to force template re-learning for a specific source.
Sourcepub fn clear_all_templates(&mut self)
pub fn clear_all_templates(&mut self)
Clear templates for all sources.
Sourcepub fn remove_source(&mut self, source: &K) -> Option<NetflowParser>
pub fn remove_source(&mut self, source: &K) -> Option<NetflowParser>
Remove a source and its parser.
This is useful for cleaning up parsers for sources that are no longer active.
Sourcepub fn get_parser(&mut self, source: &K) -> Option<&NetflowParser>
pub fn get_parser(&mut self, source: &K) -> Option<&NetflowParser>
Get a reference to a specific source’s parser.
Returns None if the source hasn’t sent any packets yet.
Note: This does not promote the entry in the LRU cache.
Sourcepub fn get_parser_mut(&mut self, source: &K) -> Option<&mut NetflowParser>
pub fn get_parser_mut(&mut self, source: &K) -> Option<&mut NetflowParser>
Get a mutable reference to a specific source’s parser.
Returns None if the source hasn’t sent any packets yet.
Note: This does not promote the entry in the LRU cache.