re_grpc_server/
shutdown.rsuse parking_lot::Mutex;
use tokio::sync::oneshot;
pub fn shutdown() -> (Signal, Shutdown) {
let (tx, rx) = oneshot::channel();
(Signal(Mutex::new(Some(tx))), Shutdown(Some(rx)))
}
pub fn never() -> Shutdown {
Shutdown(None)
}
pub struct Signal(Mutex<Option<oneshot::Sender<()>>>);
impl Signal {
pub fn stop(&self) {
if let Some(sender) = self.0.lock().take() {
sender.send(()).ok();
}
}
}
pub struct Shutdown(Option<oneshot::Receiver<()>>);
impl Shutdown {
pub async fn wait(self) {
if let Some(rx) = self.0 {
rx.await.ok();
} else {
std::future::pending::<()>().await;
}
}
}