pub struct TopicRouter { /* private fields */ }Expand description
Pub/sub topic router for kernel IPC.
Manages subscriptions as a mapping from topic name to the list of
subscribers. A subscriber can be an in-kernel PID inbox or an
external streaming client (SubscriberSink). Uses DashMap for
lock-free concurrent access.
§Dead subscriber cleanup
When a message is published, the router checks each PID subscriber’s state in the process table. PID subscribers whose processes have exited are automatically removed from the subscription list (lazy cleanup). External streams that have closed their receiver are removed on the next publish attempt.
Implementations§
Source§impl TopicRouter
impl TopicRouter
Sourcepub fn new(process_table: Arc<ProcessTable>) -> Self
pub fn new(process_table: Arc<ProcessTable>) -> Self
Create a new topic router.
Sourcepub fn subscribe_sink(&self, topic: &str, sink: SubscriberSink) -> SubscriberId
pub fn subscribe_sink(&self, topic: &str, sink: SubscriberSink) -> SubscriberId
Subscribe a sink to a topic.
Returns the assigned SubscriberId. For PID inbox sinks, if
the same PID is already subscribed to the topic the existing
subscription is kept and its id is returned (idempotent).
Sourcepub fn subscribe(&self, pid: Pid, topic: &str)
pub fn subscribe(&self, pid: Pid, topic: &str)
Subscribe a process to a topic (convenience for PID inboxes).
If the process is already subscribed, this is a no-op — the existing subscription is preserved.
Sourcepub fn unsubscribe(&self, pid: Pid, topic: &str)
pub fn unsubscribe(&self, pid: Pid, topic: &str)
Unsubscribe a process from a topic.
If the process is not subscribed, this is a no-op. Empty subscription lists are removed.
Sourcepub fn unsubscribe_id(&self, topic: &str, id: SubscriberId)
pub fn unsubscribe_id(&self, topic: &str, id: SubscriberId)
Unsubscribe a specific subscriber (by id) from a topic.
Use this to tear down external streaming subscriptions on disconnect without affecting other subscribers on the same topic.
Sourcepub fn live_sinks(&self, topic: &str) -> Vec<(SubscriberId, SubscriberSink)>
pub fn live_sinks(&self, topic: &str) -> Vec<(SubscriberId, SubscriberSink)>
Get the list of live sinks for a topic.
Performs lazy cleanup: removes PID sinks whose processes have
exited and SubscriberSink::ExternalStream sinks whose
receiver has been dropped. Returns a cloned list safe to use
outside the lock.
Sourcepub fn live_subscribers(&self, topic: &str) -> Vec<Pid> ⓘ
pub fn live_subscribers(&self, topic: &str) -> Vec<Pid> ⓘ
Get the list of running PID subscribers for a topic.
Convenience wrapper around Self::live_sinks that returns
only in-kernel agent PIDs. External streaming subscribers are
handled separately by the caller via Self::live_sinks.
Sourcepub fn subscribers(&self, topic: &str) -> Vec<Pid> ⓘ
pub fn subscribers(&self, topic: &str) -> Vec<Pid> ⓘ
Get all PID subscribers for a topic (including potentially dead ones).
Use TopicRouter::live_subscribers for a filtered list.
Sourcepub fn list_topics(&self) -> Vec<(String, usize)>
pub fn list_topics(&self) -> Vec<(String, usize)>
List all topics with their subscriber counts.
Sourcepub fn topics_for_pid(&self, pid: Pid) -> Vec<String>
pub fn topics_for_pid(&self, pid: Pid) -> Vec<String>
List all topics a specific PID is subscribed to.
Sourcepub fn topic_count(&self) -> usize
pub fn topic_count(&self) -> usize
Get the total number of active topics.
Sourcepub fn has_subscribers(&self, topic: &str) -> bool
pub fn has_subscribers(&self, topic: &str) -> bool
Check whether a topic has any subscribers.
Sourcepub fn unsubscribe_all(&self, pid: Pid)
pub fn unsubscribe_all(&self, pid: Pid)
Remove all subscriptions for a PID (used during process cleanup).
Auto Trait Implementations§
impl !RefUnwindSafe for TopicRouter
impl !UnwindSafe for TopicRouter
impl Freeze for TopicRouter
impl Send for TopicRouter
impl Sync for TopicRouter
impl Unpin for TopicRouter
impl UnsafeUnpin for TopicRouter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more