2023-05-06 05:41:24 +00:00
|
|
|
use tokio::sync::broadcast;
|
2023-05-21 18:27:51 +00:00
|
|
|
use tracing::debug;
|
2023-05-06 05:41:24 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct Shutdown {
|
2023-05-19 13:01:12 +00:00
|
|
|
shutdown: bool,
|
|
|
|
|
notify: broadcast::Receiver<()>,
|
2023-05-06 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Shutdown {
|
2023-05-19 13:01:12 +00:00
|
|
|
pub(crate) fn new(notify: broadcast::Receiver<()>) -> Shutdown {
|
|
|
|
|
Shutdown {
|
|
|
|
|
shutdown: false,
|
|
|
|
|
notify,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-06 05:41:24 +00:00
|
|
|
|
2023-05-19 13:01:12 +00:00
|
|
|
pub(crate) fn is_shutdown(&self) -> bool {
|
|
|
|
|
self.shutdown
|
|
|
|
|
}
|
2023-05-06 05:41:24 +00:00
|
|
|
|
2023-05-19 13:01:12 +00:00
|
|
|
pub(crate) async fn recv(&mut self) {
|
|
|
|
|
if self.shutdown {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-21 18:27:51 +00:00
|
|
|
debug!("waiting for shutdown");
|
2023-05-19 13:01:12 +00:00
|
|
|
let _ = self.notify.recv().await;
|
2023-05-06 05:41:24 +00:00
|
|
|
|
2023-05-19 13:01:12 +00:00
|
|
|
self.shutdown = true;
|
|
|
|
|
}
|
2023-05-06 05:41:24 +00:00
|
|
|
}
|