use dashmap::DashMap;
use query_runtime::attempt::AttemptCancellation;
use tokio_util::sync::CancellationToken;
#[derive(Debug, Default)]
pub struct QueryCancellation {
tokens: DashMap<String, QueryCancellationEntry>,
}
#[derive(Debug)]
struct QueryCancellationEntry {
flight_token: CancellationToken,
attempt_cancellation: Option<AttemptCancellation>,
}
impl QueryCancellation {
pub fn insert(
&self,
query_id: String,
flight_token: CancellationToken,
attempt_cancellation: Option<AttemptCancellation>,
) {
self.tokens.insert(query_id, QueryCancellationEntry { flight_token, attempt_cancellation });
}
pub fn cancel(&self, query_id: &str) -> bool {
self.tokens
.get(query_id)
.map(|entry| {
if let Some(attempt_cancellation) = &entry.attempt_cancellation {
attempt_cancellation.cancel();
}
entry.flight_token.cancel();
})
.is_some()
}
pub fn remove(&self, query_id: &str) {
self.tokens.remove(query_id);
}
pub fn len(&self) -> usize {
self.tokens.len()
}
}