From 2f32d26129d401a5cc48b43cf5449f243a42cc6f Mon Sep 17 00:00:00 2001 From: Zoa Hickenlooper Date: Wed, 24 Jun 2026 21:45:04 -0400 Subject: [PATCH 1/2] EMRY-006: EventBus pub/sub fan-out :3c Multi-subscriber fan-out over crossbeam unbounded channels: subscribe() returns an independent Receiver; publish(&Event) clones to each live subscriber, prunes dropped ones, and never blocks on a slow observer. Send+Sync for sharing behind Arc. Tests: 2 subscribers x 100 events in order, no-sub no-op, dead-subscriber pruning, cross-thread delivery. --- crates/emry-core/src/bus.rs | 163 ++++++++++++++++++++++++++++++++++++ crates/emry-core/src/lib.rs | 2 + 2 files changed, 165 insertions(+) create mode 100644 crates/emry-core/src/bus.rs diff --git a/crates/emry-core/src/bus.rs b/crates/emry-core/src/bus.rs new file mode 100644 index 0000000..93d1cd9 --- /dev/null +++ b/crates/emry-core/src/bus.rs @@ -0,0 +1,163 @@ +//! Pub/sub event fan-out. +//! +//! The engine pipeline [`publish`](EventBus::publish)es events and derived state; +//! observers ([`subscribe`](EventBus::subscribe)rs such as the TUI and web +//! dashboard) each receive their own copy. Fan-out uses one unbounded +//! `crossbeam_channel` per subscriber, so publishing never blocks on a slow or +//! stalled observer — backpressure against *training* is handled upstream by the +//! event ring ([`crate::ring`]), not here. +//! +//! An [`EventBus`] is `Send + Sync`; share it across threads behind an `Arc`. + +use crate::types::Event; +use crossbeam_channel::{unbounded, Receiver, Sender}; +use std::sync::Mutex; + +/// A multi-subscriber event fan-out channel. +#[derive(Debug, Default)] +pub struct EventBus { + subscribers: Mutex>>, +} + +impl EventBus { + /// Creates an empty bus with no subscribers. + #[must_use] + pub fn new() -> Self { + Self::default() + } + + /// Registers a new subscriber and returns its receiver. + /// + /// Each subscriber gets an independent unbounded queue; events published + /// after this call are delivered to it. + #[must_use] + pub fn subscribe(&self) -> Receiver { + let (tx, rx) = unbounded(); + self.subscribers + .lock() + .expect("event bus mutex poisoned") + .push(tx); + rx + } + + /// Delivers a clone of `event` to every live subscriber. + /// + /// Never blocks: unbounded queues mean a send always succeeds unless the + /// subscriber's receiver has been dropped, in which case that subscriber is + /// pruned. Returns the number of subscribers the event was delivered to. + pub fn publish(&self, event: &Event) -> usize { + let mut subscribers = self.subscribers.lock().expect("event bus mutex poisoned"); + subscribers.retain(|tx| tx.send(event.clone()).is_ok()); + subscribers.len() + } + + /// Current number of live subscribers. + /// + /// Subscribers whose receivers were dropped are only pruned on the next + /// [`publish`](EventBus::publish), so this may briefly overcount. + #[must_use] + pub fn subscriber_count(&self) -> usize { + self.subscribers + .lock() + .expect("event bus mutex poisoned") + .len() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::{MetricId, Phase}; + + fn metric(step: u64) -> Event { + Event::Metric { + id: MetricId(0), + value: 0.0, + step, + epoch: 0, + phase: Phase::Train, + } + } + + fn step_of(event: &Event) -> u64 { + match event { + Event::Metric { step, .. } => *step, + other => panic!("unexpected {other:?}"), + } + } + + #[test] + fn subscribe_increments_count() { + let bus = EventBus::new(); + assert_eq!(bus.subscriber_count(), 0); + let _a = bus.subscribe(); + let _b = bus.subscribe(); + assert_eq!(bus.subscriber_count(), 2); + } + + #[test] + fn publish_with_no_subscribers_is_a_noop() { + let bus = EventBus::new(); + assert_eq!(bus.publish(&metric(0)), 0); + } + + #[test] + fn two_subscribers_each_receive_all_100_events_in_order() { + let bus = EventBus::new(); + let a = bus.subscribe(); + let b = bus.subscribe(); + + for step in 0..100 { + assert_eq!(bus.publish(&metric(step)), 2); + } + + for rx in [&a, &b] { + for expected in 0..100 { + let event = rx.try_recv().expect("event available"); + assert_eq!(step_of(&event), expected); + } + assert!(rx.try_recv().is_err(), "exactly 100 events per subscriber"); + } + } + + #[test] + fn dropped_subscriber_is_pruned_on_next_publish() { + let bus = EventBus::new(); + let live = bus.subscribe(); + let dead = bus.subscribe(); + assert_eq!(bus.subscriber_count(), 2); + + drop(dead); + // First publish prunes the dead subscriber; the live one still receives. + assert_eq!(bus.publish(&metric(7)), 1); + assert_eq!(bus.subscriber_count(), 1); + assert_eq!(step_of(&live.try_recv().unwrap()), 7); + } + + #[test] + fn delivers_across_threads() { + use std::sync::Arc; + + let bus = Arc::new(EventBus::new()); + let rx = bus.subscribe(); + + let producer = { + let bus = Arc::clone(&bus); + std::thread::spawn(move || { + for step in 0..100 { + bus.publish(&metric(step)); + } + }) + }; + + let mut received = 0u64; + while received < 100 { + if let Ok(event) = rx.recv() { + assert_eq!(step_of(&event), received); + received += 1; + } + } + producer.join().unwrap(); + assert_eq!(received, 100); + } +} diff --git a/crates/emry-core/src/lib.rs b/crates/emry-core/src/lib.rs index 66d8261..3ed9435 100644 --- a/crates/emry-core/src/lib.rs +++ b/crates/emry-core/src/lib.rs @@ -1,11 +1,13 @@ //! Core types, protocol, and ingest primitives for Emry. +pub mod bus; pub mod error; pub mod mode; pub mod registry; pub mod ring; pub mod types; +pub use bus::EventBus; pub use error::EmryError; pub use mode::{DeployEnv, DeployMode, ParseDeployModeError}; pub use registry::{MetricRegistry, MAX_METRICS}; From 2cb5c98a8d91a7968ff7efda68d87f467ddc5baa Mon Sep 17 00:00:00 2001 From: Zoa Hickenlooper Date: Wed, 24 Jun 2026 21:47:31 -0400 Subject: [PATCH 2/2] EMRY-006: bounded fan-out with drop-and-count (address review) Switch per-subscriber channels from unbounded to bounded(DEFAULT_SUBSCRIBER_ CAPACITY=16_384) with try_send: a stalled subscriber has events dropped and counted (dropped()) rather than growing memory without limit, matching the ring buffer's backpressure policy. Disconnected receivers still pruned. --- crates/emry-core/src/bus.rs | 87 ++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/crates/emry-core/src/bus.rs b/crates/emry-core/src/bus.rs index 93d1cd9..a723ba9 100644 --- a/crates/emry-core/src/bus.rs +++ b/crates/emry-core/src/bus.rs @@ -2,21 +2,37 @@ //! //! The engine pipeline [`publish`](EventBus::publish)es events and derived state; //! observers ([`subscribe`](EventBus::subscribe)rs such as the TUI and web -//! dashboard) each receive their own copy. Fan-out uses one unbounded -//! `crossbeam_channel` per subscriber, so publishing never blocks on a slow or -//! stalled observer — backpressure against *training* is handled upstream by the -//! event ring ([`crate::ring`]), not here. +//! dashboard) each receive their own copy. +//! +//! # Backpressure policy +//! +//! Fan-out uses one **bounded** `crossbeam_channel` per subscriber, mirroring the +//! event ring ([`crate::ring`]): publishing never blocks on a slow observer. If a +//! subscriber stops draining (e.g. a stalled TUI render), its queue fills and +//! further events for it are **dropped and counted** rather than buffered without +//! limit — a laggy observer must never grow memory unbounded on a long run. +//! Dropped-event counts are exposed via [`EventBus::dropped`]. A subscriber whose +//! receiver is dropped entirely is pruned on the next publish. //! //! An [`EventBus`] is `Send + Sync`; share it across threads behind an `Arc`. use crate::types::Event; -use crossbeam_channel::{unbounded, Receiver, Sender}; +use crossbeam_channel::{bounded, Receiver, Sender, TrySendError}; +use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Mutex; +/// Default per-subscriber queue capacity. +/// +/// Generous enough to absorb normal render/render-resize hitches at typical +/// logging rates, while still bounding memory for a wedged subscriber. +pub const DEFAULT_SUBSCRIBER_CAPACITY: usize = 16_384; + /// A multi-subscriber event fan-out channel. #[derive(Debug, Default)] pub struct EventBus { subscribers: Mutex>>, + // Standalone statistic; see `crate::ring` for why `Relaxed` is sufficient. + dropped: AtomicU64, } impl EventBus { @@ -26,13 +42,18 @@ impl EventBus { Self::default() } - /// Registers a new subscriber and returns its receiver. - /// - /// Each subscriber gets an independent unbounded queue; events published - /// after this call are delivered to it. + /// Registers a subscriber with [`DEFAULT_SUBSCRIBER_CAPACITY`]. #[must_use] pub fn subscribe(&self) -> Receiver { - let (tx, rx) = unbounded(); + self.subscribe_with_capacity(DEFAULT_SUBSCRIBER_CAPACITY) + } + + /// Registers a subscriber with an explicit queue `capacity`, returning its + /// receiver. Events published after this call are delivered to it until its + /// queue is full (then dropped) or its receiver is dropped (then pruned). + #[must_use] + pub fn subscribe_with_capacity(&self, capacity: usize) -> Receiver { + let (tx, rx) = bounded(capacity); self.subscribers .lock() .expect("event bus mutex poisoned") @@ -40,17 +61,37 @@ impl EventBus { rx } - /// Delivers a clone of `event` to every live subscriber. + /// Delivers a clone of `event` to every live subscriber, returning the number + /// of live subscribers afterwards. /// - /// Never blocks: unbounded queues mean a send always succeeds unless the - /// subscriber's receiver has been dropped, in which case that subscriber is - /// pruned. Returns the number of subscribers the event was delivered to. + /// Never blocks. A subscriber whose queue is full has the event dropped and + /// counted ([`EventBus::dropped`]) but stays subscribed; a subscriber whose + /// receiver was dropped is pruned. The lock is held across the fan-out, which + /// briefly serializes concurrent publishers — acceptable since each send is + /// wait-free `try_send`, and the engine drains the ring on a single thread. pub fn publish(&self, event: &Event) -> usize { let mut subscribers = self.subscribers.lock().expect("event bus mutex poisoned"); - subscribers.retain(|tx| tx.send(event.clone()).is_ok()); + let mut dropped_now: u64 = 0; + subscribers.retain(|tx| match tx.try_send(event.clone()) { + Ok(()) => true, + Err(TrySendError::Full(_)) => { + dropped_now += 1; + true + } + Err(TrySendError::Disconnected(_)) => false, + }); + if dropped_now > 0 { + self.dropped.fetch_add(dropped_now, Ordering::Relaxed); + } subscribers.len() } + /// Number of events dropped because a subscriber's queue was full. + #[must_use] + pub fn dropped(&self) -> u64 { + self.dropped.load(Ordering::Relaxed) + } + /// Current number of live subscribers. /// /// Subscribers whose receivers were dropped are only pruned on the next @@ -99,6 +140,7 @@ mod tests { fn publish_with_no_subscribers_is_a_noop() { let bus = EventBus::new(); assert_eq!(bus.publish(&metric(0)), 0); + assert_eq!(bus.dropped(), 0); } #[test] @@ -118,6 +160,21 @@ mod tests { } assert!(rx.try_recv().is_err(), "exactly 100 events per subscriber"); } + assert_eq!(bus.dropped(), 0); + } + + #[test] + fn full_subscriber_queue_drops_and_counts_without_blocking() { + let bus = EventBus::new(); + // Tiny queue; never drained. + let _slow = bus.subscribe_with_capacity(2); + assert_eq!(bus.publish(&metric(0)), 1); + assert_eq!(bus.publish(&metric(1)), 1); + // Queue full from here: events are dropped, subscriber stays live. + assert_eq!(bus.publish(&metric(2)), 1); + assert_eq!(bus.publish(&metric(3)), 1); + assert_eq!(bus.dropped(), 2); + assert_eq!(bus.subscriber_count(), 1); } #[test]