Add listener closing when the target set has changed.
All checks were successful
CI / build (push) Successful in 22m46s
All checks were successful
CI / build (push) Successful in 22m46s
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
use crate::shutdown::ShutdownReceiver;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub(crate) struct Listener {
|
||||
pub(crate) shutdown: ShutdownReceiver,
|
||||
pub(crate) source: String,
|
||||
pub(crate) targets: Arc<RwLock<Vec<String>>>,
|
||||
pub(crate) targets_changed: broadcast::Receiver<()>,
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
||||
struct ActiveListener {
|
||||
notify_shutdown: broadcast::Sender<()>,
|
||||
targets: Arc<RwLock<Vec<String>>>,
|
||||
notify_targets: broadcast::Sender<()>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -119,19 +120,24 @@ async fn run_loop(
|
||||
warn!("Found invalid targets! Adjusting!");
|
||||
let mut w = listener.targets.write().await;
|
||||
*w = target.targets;
|
||||
|
||||
_ = listener.notify_targets.send(())?; //TODO: Maybe ignore this error
|
||||
}
|
||||
} else {
|
||||
let (notify_shutdown, _) = broadcast::channel(1);
|
||||
let (notify_targets, _) = broadcast::channel(1);
|
||||
|
||||
let listener = ActiveListener {
|
||||
notify_shutdown: notify_shutdown,
|
||||
targets: Arc::new(RwLock::new(target.targets)),
|
||||
notify_targets: notify_targets,
|
||||
};
|
||||
|
||||
let l = listener::Listener {
|
||||
shutdown: shutdown::ShutdownReceiver::new(listener.notify_shutdown.subscribe()),
|
||||
source: target.source.clone(),
|
||||
targets: listener.targets.clone(),
|
||||
targets_changed: listener.notify_targets.subscribe(),
|
||||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
@ -168,6 +174,8 @@ async fn run_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Maybe Wait for the listener to shut down
|
||||
|
||||
debug!("Removing listener!");
|
||||
listeners.remove(&del_key);
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ pub(crate) async fn start_tcp_listener(
|
||||
loop {
|
||||
let (next_socket, _) = tokio::select! {
|
||||
res = listener.accept() => res?,
|
||||
_ = listener_config.targets_changed.recv() => {
|
||||
info!("Targets changed!");
|
||||
continue;
|
||||
}
|
||||
_ = listener_config.shutdown.recv() => {
|
||||
info!("Exiting listener!");
|
||||
return Ok(());
|
||||
|
11
src/udp.rs
11
src/udp.rs
@ -180,6 +180,15 @@ pub(crate) async fn start_udp_listener(mut listener_config: Listener) -> Result<
|
||||
trace!("Waiting for packet or shutdown");
|
||||
let (num_bytes, src_addr) = tokio::select! {
|
||||
res = listener.recv_from(&mut buf) => res?,
|
||||
_ = listener_config.targets_changed.recv() => {
|
||||
info!("Targets changed!");
|
||||
info!("Closing all connections!");
|
||||
for (_, handler) in connections.lock().await.iter_mut() {
|
||||
handler.close().await;
|
||||
}
|
||||
info!("Closed all connections!");
|
||||
continue;
|
||||
}
|
||||
_ = listener_config.shutdown.recv() => {
|
||||
info!("Exiting listener!");
|
||||
break;
|
||||
@ -220,5 +229,7 @@ pub(crate) async fn start_udp_listener(mut listener_config: Listener) -> Result<
|
||||
handler.close().await;
|
||||
}
|
||||
|
||||
println!("Listener closed.");
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
Reference in New Issue
Block a user