8 unstable releases
1.0.0-rc.5 | Feb 7, 2024 |
---|---|
0.32.0 | May 23, 2022 |
0.8.1 | Jan 29, 2021 |
0.2.7 | Oct 12, 2020 |
0.0.7 | Jan 15, 2020 |
#3 in #shutdown-signal
83 downloads per month
Used in 10 crates
(9 directly)
14KB
232 lines
A convenient shutdown signal
ShutdownSignal
is a convenient wrapper around a one-shot channel that allows different threads to let each other know
that they should stop working.
Basic usage
First, create the shutdown signal.
let mut shutdown = Shutdown::new();
Use to_signal
to create a future which will resolve when Shutdown
is triggered.
let signal = shutdown.to_signal();
assert_eq!(shutdown.is_triggered(), false);
You can clone the signal and move it into threads that need to be informed of when to shut down. We're using tokio here, but this will work in any futures-based runtime:
tokio::spawn(async move {
signal.await.unwrap();
println!("Finished");
});
Then when you want to trigger the shutdown signal, call trigger
. All signals will resolve.
shutdown.trigger().unwrap(); // "Finished" is printed
// Shutdown::trigger is idempotent
shutdown.trigger().unwrap();
assert_eq!(shutdown.is_triggered(), true);
Note: If the ShutdownSignal instance is dropped, it will trigger the signal, so the Shutdown
instance should be held
as long as required by the application.
Dependencies
~1MB
~15K SLoC