Skip to main content

EventBus

Struct EventBus 

Source
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

Source

pub fn new() -> Self

Create a new event bus with default capacity.

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new event bus with specified capacity.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn routed_subscription_count(&self) -> usize

Number of active routed subscriptions (diagnostic).

Source

pub fn subscriber_count(&self) -> usize

Get the current number of active subscribers (both async and synchronous).

Source

pub fn capacity(&self) -> usize

Get the channel capacity.

Trait Implementations§

Source§

impl Clone for EventBus

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EventBus

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EventBus

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more