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

#78 in Concurrency

Download history 3/week @ 2023-12-04 5/week @ 2023-12-11 10/week @ 2023-12-18 8/week @ 2023-12-25 8/week @ 2024-01-08 13/week @ 2024-01-15 3/week @ 2024-01-22 15/week @ 2024-02-05 27/week @ 2024-02-12 35/week @ 2024-02-19 50/week @ 2024-02-26 23/week @ 2024-03-04 19/week @ 2024-03-11 48/week @ 2024-03-18

151 downloads per month
Used in 10 crates (9 directly)

BSD-3-Clause

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
~16K SLoC