use tokio::sync::watch;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) enum ShutdownKind {
#[default]
None,
Graceful,
Abort,
}
#[derive(Clone, Debug)]
pub struct ConnectionShutdown {
tx: watch::Sender<ShutdownKind>,
}
impl ConnectionShutdown {
pub(crate) fn new(tx: watch::Sender<ShutdownKind>) -> Self {
Self { tx }
}
pub fn shutdown(&self) {
let _ = self.tx.send_replace(ShutdownKind::Abort);
}
pub fn graceful_shutdown(&self) {
let _ = self.tx.send_if_modified(|kind| match kind {
ShutdownKind::None => {
*kind = ShutdownKind::Graceful;
true
}
ShutdownKind::Graceful | ShutdownKind::Abort => false,
});
}
}