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

Download history 9/week @ 2024-06-28 4/week @ 2024-07-05 28/week @ 2024-07-12 29/week @ 2024-07-19 18/week @ 2024-07-26 18/week @ 2024-08-02 13/week @ 2024-08-09 10/week @ 2024-08-16 12/week @ 2024-08-23 10/week @ 2024-08-30 5/week @ 2024-09-06 15/week @ 2024-09-13 39/week @ 2024-09-20 28/week @ 2024-09-27 9/week @ 2024-10-04 4/week @ 2024-10-11

83 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
~15K SLoC