use std::sync::Arc;
use std::time::Duration;
use flowscope::{FlowTrackerConfig, L4Proto};
use crate::dedup::Dedup;
pub type SharedIdleTimeoutFn<K> =
Arc<dyn Fn(&K, Option<L4Proto>) -> Option<Duration> + Send + Sync + 'static>;
pub struct MultiStreamConfig<K> {
pub tracker_config: FlowTrackerConfig,
pub dedup: Option<Dedup>,
pub idle_timeout_fn: Option<SharedIdleTimeoutFn<K>>,
pub monotonic_ts: bool,
}
impl<K> Default for MultiStreamConfig<K> {
fn default() -> Self {
Self {
tracker_config: FlowTrackerConfig::default(),
dedup: None,
idle_timeout_fn: None,
monotonic_ts: false,
}
}
}
impl<K> Clone for MultiStreamConfig<K> {
fn clone(&self) -> Self {
Self {
tracker_config: self.tracker_config.clone(),
dedup: self.dedup.clone(),
idle_timeout_fn: self.idle_timeout_fn.clone(),
monotonic_ts: self.monotonic_ts,
}
}
}
impl<K> std::fmt::Debug for MultiStreamConfig<K> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MultiStreamConfig")
.field("tracker_config", &self.tracker_config)
.field("has_dedup", &self.dedup.is_some())
.field("has_idle_timeout_fn", &self.idle_timeout_fn.is_some())
.field("monotonic_ts", &self.monotonic_ts)
.finish()
}
}
impl<K> MultiStreamConfig<K> {
pub fn new() -> Self {
Self::default()
}
pub fn with_tracker_config(mut self, c: FlowTrackerConfig) -> Self {
self.tracker_config = c;
self
}
pub fn with_infer_tcp_initiator(mut self, enable: bool) -> Self {
self.tracker_config.infer_tcp_initiator = enable;
self
}
pub fn with_dedup(mut self, d: Dedup) -> Self {
self.dedup = Some(d);
self
}
pub fn with_idle_timeout_fn<F>(mut self, f: F) -> Self
where
F: Fn(&K, Option<L4Proto>) -> Option<Duration> + Send + Sync + 'static,
{
self.idle_timeout_fn = Some(Arc::new(f));
self
}
pub fn with_monotonic_timestamps(mut self, enable: bool) -> Self {
self.monotonic_ts = enable;
self
}
}