pub struct EventBus { /* private fields */ }Expand description
Event bus for broadcasting events to all subscribers.
The event bus uses a broadcast channel to deliver events to all connected receivers. Events are delivered asynchronously and in order.
WARNING: Synchronous subscribers (SubscriberRegistry) are shared
across clones. Storing a cloned EventBus inside a synchronous subscriber
will create a memory leak via an Arc reference cycle. If a synchronous
subscriber needs to publish events, store a std::sync::Weak<EventBus>
or communicate via a separate channel.
Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new event bus with specified capacity.
Sourcepub fn publish(&self, event: AstridEvent) -> usize
pub fn publish(&self, event: AstridEvent) -> usize
Publish an event to all subscribers.
This method broadcasts the event to all async subscribers and notifies all synchronous subscribers in the registry.
Returns the number of async receivers that received the event.
Sourcepub fn subscribe(&self) -> EventReceiver
pub fn subscribe(&self) -> EventReceiver
Subscribe to events.
Returns a receiver that will receive all published events. The
receiver’s lag is attributed to the "untagged" subscriber in
[METRIC_BUS_RECEIVER_LAGGED_TOTAL]; use subscribe_as to give a
long-lived consumer a stable label.
Sourcepub fn subscribe_as(&self, subscriber: &'static str) -> EventReceiver
pub fn subscribe_as(&self, subscriber: &'static str) -> EventReceiver
Subscribe to all events, attributing this receiver’s lag to a
stable subscriber label. Pass a fixed &'static str (never
caller/remote text) so the lag-counter cardinality stays bounded.
Sourcepub fn subscribe_topic(&self, topic_pattern: impl Into<String>) -> EventReceiver
pub fn subscribe_topic(&self, topic_pattern: impl Into<String>) -> EventReceiver
Subscribe to IPC events matching a specific topic pattern.
The pattern can be an exact match (e.g. astrid.cli.input)
or end with a trailing * (e.g. astrid.v1.request.*) which matches
one or more remaining dot-separated segments up to a maximum depth of 20.
Middle wildcards (e.g. astrid.*.event) match exactly one segment.
Lag is attributed to "untagged"; use subscribe_topic_as for a
long-lived consumer.
Sourcepub fn subscribe_topic_as(
&self,
topic_pattern: impl Into<String>,
subscriber: &'static str,
) -> EventReceiver
pub fn subscribe_topic_as( &self, topic_pattern: impl Into<String>, subscriber: &'static str, ) -> EventReceiver
Topic subscription that attributes this receiver’s lag to a stable
subscriber label. Pass a fixed &'static str (never the topic
pattern itself, which can be capsule-supplied) so the lag-counter
cardinality stays bounded.
Sourcepub fn subscribe_topic_routed(
&self,
capsule_uuid: Uuid,
topic_pattern: impl Into<String>,
capsule_id_label: impl Into<String>,
subscriber: &'static str,
) -> RoutedEventReceiver
pub fn subscribe_topic_routed( &self, capsule_uuid: Uuid, topic_pattern: impl Into<String>, capsule_id_label: impl Into<String>, subscriber: &'static str, ) -> RoutedEventReceiver
Subscribe with publish-side per-(capsule, topic, principal) routing.
Allocates a [RouteEntry] in the bus’s routes table and
returns a RoutedEventReceiver that drains its own queues
with deficit-round-robin fairness across principals. Two
receivers of the same (capsule_uuid, topic_pattern) get
distinct routes — each receives its own copy of every matching
event, unlike the broadcast channel which shares one queue.
Dropping the receiver removes its route from the bus.
Sourcepub fn subscribe_topic_routed_scoped(
&self,
capsule_uuid: Uuid,
topic_pattern: impl Into<String>,
capsule_id_label: impl Into<String>,
subscriber: &'static str,
scope: Option<PrincipalKey>,
) -> RoutedEventReceiver
pub fn subscribe_topic_routed_scoped( &self, capsule_uuid: Uuid, topic_pattern: impl Into<String>, capsule_id_label: impl Into<String>, subscriber: &'static str, scope: Option<PrincipalKey>, ) -> RoutedEventReceiver
Routed subscription self-scoped to a single publisher principal.
Identical to subscribe_topic_routed
except the route only ever admits events whose publisher
PrincipalKey equals scope; foreign-principal events are dropped
at enqueue so they never enter this route’s byte budget (see
RouteEntry::accepts). Pass
scope == None for the unscoped, all-principals behaviour —
subscribe_topic_routed is exactly that delegation.
The scope is the authorization seam for capability-gated firehose
topics (e.g. the audit feed): a non-privileged subscriber is scoped
to its own principal so it can never observe another principal’s
events, while a privileged firehose holder subscribes with
scope == None.
Dropping the receiver removes its route from the bus.
Sourcepub fn routed_subscription_count(&self) -> usize
pub fn routed_subscription_count(&self) -> usize
Number of active routed subscriptions (diagnostic).
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Get the current number of active subscribers (both async and synchronous).