event_bus
event_bus
¶
Async pub/sub event bus for the Marianne daemon.
Routes ObserverEvents from the baton and observer to downstream consumers (SSE dashboard, learning hub, future webhooks).
Architecture invariant 5 (load-bearing): the EventBus never blocks publishers. A slow or hung subscriber must never stall the baton/observer that publishes events. EventBus events are observability — unlike the baton's execution inbox (where dropping an event strands a sheet), dropping an event here under overload is acceptable.
To honour that invariant in practice (not just in the letter of a non-blocking
publish), this bus isolates subscribers from each other and from the
publisher:
_pendingis a bounded queue.publish()usesput_nowaitand, on overflow, evicts the oldest in-flight event before enqueuing the new one (drop-oldest backpressure — slow consumers lose old events, per architecture.yaml), so a publisher can never block and memory can never grow without bound.- The single drain loop only filters and enqueues events into each
subscriber's own bounded
deque(drop-oldest preserves recency for live streams) — it never awaits a subscriber callback. - Each subscriber owns a worker task that runs its callback under a per-callback timeout. A slow/hung callback blocks only that subscriber.
- A subscriber is auto-evicted after
_MAX_CONSECUTIVE_FAILURESconsecutive callback exceptions, callback timeouts, or filter exceptions — preventing the subscriber dict from leaking dead SSE clients. shutdown()is time-bounded: it cancels the workers and drops any remaining observability events rather than awaiting a hung callback (which would force SIGKILL and risk state corruption).
Classes¶
EventBus
¶
EventBus(*, max_queue_size=1000, max_pending_size=_DEFAULT_PENDING_MAXSIZE, callback_timeout=_DEFAULT_CALLBACK_TIMEOUT, shutdown_timeout=_DEFAULT_SHUTDOWN_TIMEOUT)
Async pub/sub event bus with per-subscriber isolation.
Subscribers receive events via callbacks executed in their own worker task. A bounded central queue plus per-subscriber bounded deques guarantee that a slow or hung subscriber can neither block the publisher nor stall other subscribers.
Usage::
bus = EventBus(max_queue_size=1000)
await bus.start()
sub_id = bus.subscribe(callback=my_handler)
sub_id = bus.subscribe(
callback=my_handler,
event_filter=lambda e: e["event"].startswith("sheet."),
)
await bus.publish(event)
bus.unsubscribe(sub_id)
await bus.shutdown()
Source code in src/marianne/daemon/event_bus.py
Attributes¶
Methods:¶
start
async
¶
Start the background drain loop and any pending subscriber workers.
Source code in src/marianne/daemon/event_bus.py
publish
async
¶
Publish an event to all matching subscribers.
Non-blocking for the publisher (invariant 5). On a full central queue,
the oldest event is dropped to make room — matching the EventBus's
spec'd "drop-oldest backpressure: slow consumers lose old events"
(architecture.yaml) and the per-subscriber deques. The
get_nowait()+put_nowait() pair is atomic here: there is no
await between them, so the single-threaded drain loop cannot
interleave (and the queue is full, so no getter is waiting).
Source code in src/marianne/daemon/event_bus.py
subscribe
¶
Register a subscriber.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
EventCallback
|
Async or sync callable receiving an ObserverEvent. Sync
callbacks MUST be non-blocking (they run in the event loop);
async callbacks are bounded by |
required |
event_filter
|
EventFilter
|
Optional predicate. If provided, the subscriber only receives events where it returns True. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Subscription ID for later unsubscribe. |
Source code in src/marianne/daemon/event_bus.py
unsubscribe
¶
Remove a subscriber and cancel its worker.
Returns:
| Type | Description |
|---|---|
bool
|
True if the subscriber existed and was removed. |
Source code in src/marianne/daemon/event_bus.py
shutdown
async
¶
Stop the bus within a bounded time, dropping remaining events.
Observability events are droppable; shutdown must never await a hung subscriber callback (which would force SIGKILL).