Add bounded streaming execution kernel
This commit is contained in:
parent
ac2289bd0a
commit
f569707f97
1 changed files with 2384 additions and 1 deletions
51
crates/remora-stream/src/cancel.rs
Normal file
51
crates/remora-stream/src/cancel.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use tokio::sync::watch;
|
||||
|
||||
/// Cooperative cancellation token for the copy loop and bounded I/O.
|
||||
///
|
||||
/// Cancel wakes waiters so in-flight operations bounded by
|
||||
/// [`crate::copy_stream`] can return without waiting for their full
|
||||
/// no-progress timeout.
|
||||
///
|
||||
/// A receiver is retained so `watch::Sender::send` always has a live
|
||||
/// subscriber and the cancelled flag is stored.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CancelToken {
|
||||
tx: watch::Sender<bool>,
|
||||
rx: watch::Receiver<bool>,
|
||||
}
|
||||
|
||||
impl Default for CancelToken {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl CancelToken {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
let (tx, rx) = watch::channel(false);
|
||||
Self { tx, rx }
|
||||
}
|
||||
|
||||
pub fn cancel(&self) {
|
||||
let _ = self.tx.send(true);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
*self.rx.borrow()
|
||||
}
|
||||
|
||||
/// Completes when [`Self::cancel`] has been called.
|
||||
pub async fn cancelled(&self) {
|
||||
let mut rx = self.rx.clone();
|
||||
if *rx.borrow_and_update() {
|
||||
return;
|
||||
}
|
||||
while rx.changed().await.is_ok() {
|
||||
if *rx.borrow_and_update() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue