shutdown: permit ShutdownController to be cloned

When multiple tasks can ask to quit
This commit is contained in:
ppom 2025-11-20 12:00:00 +01:00
commit 2216edfba0
No known key found for this signature in database
2 changed files with 8 additions and 3 deletions

View file

@ -5,7 +5,7 @@ use tokio_util::{
/// Permits to keep track of ongoing tasks, ask them to shutdown and for all of them to quit.
/// Stupid wrapper around [`tokio_util::CancellationToken`] and [`tokio_util::task_tracker::TaskTracker`].
#[derive(Default)]
#[derive(Default, Clone)]
pub struct ShutdownController {
shutdown_notifyer: CancellationToken,
task_tracker: TaskTracker,
@ -29,7 +29,7 @@ impl ShutdownController {
/// Wait for all tasks to quit.
/// This task may return even without having called [`ShutdownController::ask_shutdown`]
/// first, if all tasks quit by themselves.
pub async fn wait_shutdown(self) {
pub async fn wait_all_task_shutdown(self) {
self.task_tracker.close();
self.task_tracker.wait().await;
}
@ -44,6 +44,11 @@ impl ShutdownController {
pub fn delegate(&self) -> ShutdownDelegate {
ShutdownDelegate(self.shutdown_notifyer.clone())
}
/// Returns a future that will resolve only when a shutdown request happened.
pub fn wait(&self) -> WaitForCancellationFuture<'_> {
self.shutdown_notifyer.cancelled()
}
}
/// Permits to ask for shutdown, without counting as a task that needs to be awaited.

View file

@ -82,7 +82,7 @@ pub async fn daemon(config_path: PathBuf, socket: PathBuf) -> i32 {
shutdown.ask_shutdown();
debug!("Waiting for all tasks to quit...");
shutdown.wait_shutdown().await;
shutdown.wait_all_task_shutdown().await;
let mut stop_ok = true;
if config_started {