open-standards/eoc/eoc-rs/crates/eoc-streaming/src/cancel.rs
Transaction Science 7ef14c2ea9 ingest wave B: 3 crates · op-payouts + smart-byte-nostr + eoc-streaming
OpenPay (1 crate, 2174 LOC, 14 tests)
  op-payouts          Multi-rail payout engine. Visa Direct, Mastercard Send,
                      ACH credits (NACHA), Fedwire + SWIFT MT103 + ISO 20022
                      pacs.008, SEPA SCT + SCT Inst (mod-97 IBAN), UK FPS,
                      FedNow, RTP, PayPal, Wise. Crypto: USDC/USDT on
                      ETH/Polygon/Base/Solana + BTC + Lightning with real
                      ERC-20 transfer(address,uint256) calldata. Offline-pure
                      drivers (emit wire payloads — operators transmit).

Smart Byte (1 crate, 2605 LOC, 38 tests)
  smart-byte-nostr    Nostr adapter. NIP-01 canonical event id + BIP-340
                      Schnorr sign/verify; from-scratch bech32 + NIP-19 TLV
                      (npub/nsec/note/nprofile/nevent/nrelay); NIP-04 legacy
                      AES-256-CBC DMs; NIP-44 v2 HKDF-SHA256→ChaCha20+HMAC
                      with bucketed padding; NIP-17 gift-wrap (kind 1059) +
                      seal (kind 13) + rumor (kind 14); NIP-05/11/13/26/65.
                      Relay client message vocabulary (REQ/EVENT/CLOSE/AUTH/
                      EOSE/OK/NOTICE/CLOSED) + in-memory router.

EOC (1 crate, 2024 LOC, 30 tests)
  eoc-streaming       Token-by-token + tool-call streaming primitives.
                      SSE framing (data: lines); Anthropic event mapper
                      (message_start/content_block_*/message_delta/stop/ping);
                      OpenAI delta mapper; Vercel AI SDK mapper. Bounded-channel
                      backpressure with watermarks; CancelToken propagation;
                      Last-Event-ID resumable streams; per-stream joule meter.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 13:39:09 -04:00

86 lines
2 KiB
Rust

//! Stream cancellation.
//!
//! A [`CancelToken`] is a cheap, clonable handle on a shared
//! cancellation flag. The HTTP/WS bridge holds one; when a client
//! disconnects, it flips the flag and notifies waiters. Upstream
//! mappers and the cascade observe the flag between events and abort
//! the in-flight request.
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Notify;
/// Cancellation handle for a single stream.
#[derive(Clone, Debug)]
pub struct CancelToken {
flag: Arc<AtomicBool>,
notify: Arc<Notify>,
}
impl CancelToken {
/// Construct a fresh, un-cancelled token.
pub fn new() -> Self {
Self {
flag: Arc::new(AtomicBool::new(false)),
notify: Arc::new(Notify::new()),
}
}
/// Flip to cancelled and wake any waiters.
pub fn cancel(&self) {
if !self.flag.swap(true, Ordering::AcqRel) {
self.notify.notify_waiters();
}
}
/// Snapshot the cancelled state.
pub fn is_cancelled(&self) -> bool {
self.flag.load(Ordering::Acquire)
}
/// Wait until cancelled.
pub async fn cancelled(&self) {
if self.is_cancelled() {
return;
}
let notified = self.notify.notified();
if self.is_cancelled() {
return;
}
notified.await;
}
}
impl Default for CancelToken {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn cancel_signals_waiter() {
let tok = CancelToken::new();
let tok2 = tok.clone();
let h = tokio::spawn(async move {
tok2.cancelled().await;
true
});
assert!(!tok.is_cancelled());
tok.cancel();
assert!(h.await.unwrap());
assert!(tok.is_cancelled());
}
#[tokio::test]
async fn cancel_is_idempotent() {
let tok = CancelToken::new();
tok.cancel();
tok.cancel();
assert!(tok.is_cancelled());
}
}