Skip to main content

alopex_cli/streaming/
cancel.rs

1//! Cancellation utilities for streaming queries.
2//!
3//! Provides a lightweight cancellation signal that can be triggered
4//! by Ctrl-C (via config::is_interrupted) or manually in tests.
5
6use std::sync::atomic::{AtomicBool, Ordering};
7use std::sync::Arc;
8use std::time::Duration;
9
10use crate::config;
11
12#[derive(Clone, Debug, Default)]
13pub struct CancelSignal {
14    manual: Arc<AtomicBool>,
15}
16
17impl CancelSignal {
18    pub fn new() -> Self {
19        Self {
20            manual: Arc::new(AtomicBool::new(false)),
21        }
22    }
23
24    #[allow(dead_code)]
25    pub fn cancel(&self) {
26        self.manual.store(true, Ordering::SeqCst);
27    }
28
29    pub fn is_cancelled(&self) -> bool {
30        config::is_interrupted() || self.manual.load(Ordering::SeqCst)
31    }
32
33    pub async fn wait(&self) {
34        while !self.is_cancelled() {
35            tokio::time::sleep(Duration::from_millis(50)).await;
36        }
37    }
38}