surrealdb_core/ctx/
cancellation.rs

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
#![cfg(feature = "scripting")]

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use trice::Instant;

/// A 'static view into the cancellation status of a Context.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct Cancellation {
	deadline: Option<Instant>,
	cancellations: Vec<Arc<AtomicBool>>,
}

impl Cancellation {
	pub fn new(deadline: Option<Instant>, cancellations: Vec<Arc<AtomicBool>>) -> Cancellation {
		Self {
			deadline,
			cancellations,
		}
	}

	pub fn is_done(&self) -> bool {
		self.deadline.map(|d| d <= Instant::now()).unwrap_or(false)
			|| self.cancellations.iter().any(|c| c.load(Ordering::Relaxed))
	}
}