pub struct Shutdown { /* private fields */ }
Expand description
Listens for a shutdown signal.
Shutdown is signalled using a broadcast::Receiver
. Only a single value is
ever sent. Once a value has been sent via the broadcast channel, shutdown
should occur.
The Shutdown
struct listens for the signal and tracks that the signal has
been received. Callers may query for whether the shutdown signal has been
received or not.
Implementations§
source§impl Shutdown
impl Shutdown
sourcepub fn new(notify: Receiver<()>) -> Shutdown
pub fn new(notify: Receiver<()>) -> Shutdown
Create a new Shutdown
backed by the given broadcast::Receiver
.
Examples
use tokio::sync::broadcast;
use lib_wc::sync::Shutdown;
let (tx, rx) = broadcast::channel(1);
let shutdown = Shutdown::new(rx);
sourcepub fn is_shutdown(&self) -> bool
pub fn is_shutdown(&self) -> bool
Returns true
if the shutdown signal has been received.
Examples
use tokio::sync::broadcast;
use lib_wc::sync::Shutdown;
let (tx, rx) = broadcast::channel(1);
let shutdown = Shutdown::new(rx);
assert!(!shutdown.is_shutdown());
sourcepub async fn recv(&mut self)
pub async fn recv(&mut self)
Receive the shutdown notice, waiting if necessary.
Examples
use tokio::task::spawn;
use tokio::sync::broadcast;
use lib_wc::sync::Shutdown;
#[tokio::main]
async fn main() {
let (tx, rx) = broadcast::channel(1);
let mut shutdown = Shutdown::new(rx);
assert!(!shutdown.is_shutdown());
let t = spawn(async move {
shutdown.recv().await;
assert!(shutdown.is_shutdown());
});
// Signal shutdown
drop(tx);
t.await;
}