surrealdb_core/ctx/
cancellation.rs

1#![cfg(feature = "scripting")]
2
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::Arc;
5use trice::Instant;
6
7/// A 'static view into the cancellation status of a Context.
8#[derive(Clone, Debug, Default)]
9#[non_exhaustive]
10pub struct Cancellation {
11	deadline: Option<Instant>,
12	cancellations: Vec<Arc<AtomicBool>>,
13}
14
15impl Cancellation {
16	pub fn new(deadline: Option<Instant>, cancellations: Vec<Arc<AtomicBool>>) -> Cancellation {
17		Self {
18			deadline,
19			cancellations,
20		}
21	}
22
23	pub fn is_done(&self) -> bool {
24		self.deadline.map(|d| d <= Instant::now()).unwrap_or(false)
25			|| self.cancellations.iter().any(|c| c.load(Ordering::Relaxed))
26	}
27}