use dashmap::DashMap;
use once_cell::sync::Lazy;
use serde::Serialize;
use sigterm::ShutdownHandle;
use std::sync::Arc;
use tokio::time::Instant;
#[derive(Serialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
Tcp,
Udp,
}
pub enum ListenerState {
Active,
Draining { since: Instant },
}
pub struct RunningListener {
pub state: Arc<tokio::sync::Mutex<ListenerState>>,
pub shutdown: ShutdownHandle,
}
pub static TASK_REGISTRY: Lazy<DashMap<(u16, Protocol), RunningListener>> = Lazy::new(DashMap::new);
#[cfg(test)]
mod tests {
use super::*;
use serde_json;
#[test]
fn test_protocol_serialization() {
let tcp = Protocol::Tcp;
let udp = Protocol::Udp;
assert_eq!(serde_json::to_string(&tcp).unwrap(), "\"tcp\"");
assert_eq!(serde_json::to_string(&udp).unwrap(), "\"udp\"");
}
}