protohackers/problem_06/src/shutdown.rs

32 lines
522 B
Rust
Raw Normal View History

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 {
shutdown: bool,
notify: broadcast::Receiver<()>,
2023-05-06 05:41:24 +00:00
}
impl Shutdown {
pub(crate) fn new(notify: broadcast::Receiver<()>) -> Shutdown {
Shutdown {
shutdown: false,
notify,
}
}
2023-05-06 05:41:24 +00:00
pub(crate) fn is_shutdown(&self) -> bool {
self.shutdown
}
2023-05-06 05:41:24 +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");
let _ = self.notify.recv().await;
2023-05-06 05:41:24 +00:00
self.shutdown = true;
}
2023-05-06 05:41:24 +00:00
}