1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Cooperative cancellation and process termination signals.
use crate::error::{Context, Error};
use std::io;
/// Cloneable cooperative shutdown signal.
#[derive(Clone)]
pub struct CancellationToken(tokio::sync::watch::Sender<bool>);
impl Default for CancellationToken {
fn default() -> Self {
Self::new()
}
}
impl CancellationToken {
/// A token that is not cancelled.
pub fn new() -> Self {
Self(tokio::sync::watch::channel(false).0)
}
/// Cancel this token and every clone of it. Cancelling again has no effect.
pub fn cancel(&self) {
self.0.send_replace(true);
}
/// Whether the token has been cancelled.
pub fn is_cancelled(&self) -> bool {
*self.0.borrow()
}
/// Wait until the token is cancelled.
pub async fn cancelled(&self) {
let mut receiver = self.0.subscribe();
let _ = receiver.wait_for(|cancelled| *cancelled).await;
}
}
pub(crate) async fn termination_signal() -> Result<(), Error> {
wait_for_signal()
.await
.context("listening for termination signals")
}
async fn wait_for_signal() -> io::Result<()> {
#[cfg(unix)]
{
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
tokio::select! { result = tokio::signal::ctrl_c() => { result?; }, _ = term.recv() => {} }
}
#[cfg(not(unix))]
tokio::signal::ctrl_c().await?;
Ok(())
}