use std::sync::Arc;
use super::{ClientRun, Shared};
use crate::CancellationToken;
use crate::wire::protocol::Failure;
use crate::wire::protocol::method::{self, Access};
impl Shared {
pub(super) fn cancel(self: &Arc<Self>, stream: u32) {
self.reads.cancel(&stream);
#[cfg(feature = "sql")]
if self.sql.cancel(stream) {
return;
}
#[cfg(feature = "blobs")]
if self.blobs.cancel(stream) || self.cancel_fill(stream) {
return;
}
#[cfg(feature = "records")]
if self.records.cancel(stream) {
return;
}
#[cfg(feature = "metrics")]
if self.metrics.cancel(stream) {
return;
}
#[cfg(feature = "config")]
if self.config.cancel(stream) {
return;
}
#[cfg(feature = "backup")]
if self.backups.cancel(stream) {
return;
}
#[cfg(feature = "channels")]
if self.channels.cancel(stream) {
return;
}
#[cfg(feature = "locks")]
if self.locks.cancel(stream) {
return self.answer(stream, Err(Failure::cancelled("the client cancelled its hold")));
}
#[cfg(feature = "jobs")]
if self.jobs.work(stream).is_some() {
return self.end_work(stream, Err(Failure::cancelled("the client cancelled its worker")));
}
#[cfg(feature = "jobs")]
if let Some(watch) = self.jobs.take_watch(stream) {
return watch.close(Err(Failure::cancelled("the client cancelled its watch")));
}
let mut runs = self.lock_runs();
let cancelled = Failure::cancelled("the client cancelled the run");
match runs.remove(&stream) {
Some(ClientRun::Asking(_)) => self.answer(stream, Err(cancelled)),
Some(ClientRun::Handed(handed)) => {
self.end_stream(stream, Err(cancelled));
drop(runs);
drop(handed);
}
None => {}
}
}
pub(super) fn reading(&self, stream: u32, method: u16) -> Option<CancellationToken> {
if method::access(method) != Some(Access::Read) {
return None;
}
Some(self.reads.start(stream))
}
pub(super) fn read_under<T>(&self, stream: u32, token: Option<CancellationToken>, read: impl FnOnce() -> T) -> T {
let Some(token) = token else { return read() };
let answered = token.run(read);
self.reads.finish(&stream, &token);
answered
}
}