pub struct SubscriptionRouter {
pub total_routed: u64,
pub total_delivered: u64,
pub total_dropped: u64,
/* private fields */
}Expand description
Topic/type-based message routing engine.
Manages subscriber registrations, evaluates SubscriptionFilter predicates,
logs delivery attempts to a bounded ring-buffer, and exposes rich query and
eviction helpers.
§Design
- Messages with
ttl == 0on arrival are immediately dropped (counted intotal_dropped); their TTL is not decremented because the router does not modify the caller-owned message. - The delivery log is a
VecDequecapped atmax_log_size. Oldest entries are evicted when the cap is reached. - All statistics counters are plain
u64fields (not atomic) because the router is designed for single-threaded or externally-synchronized use. Wrap inArc<Mutex<_>>if concurrent access is required.
Fields§
§total_routed: u64Total messages passed to route.
total_delivered: u64Total successful deliveries.
total_dropped: u64Total dropped messages.
Implementations§
Source§impl SubscriptionRouter
impl SubscriptionRouter
Sourcepub fn new(max_log_size: usize) -> Self
pub fn new(max_log_size: usize) -> Self
Create a new SubscriptionRouter with a delivery log bounded at
max_log_size entries.
Sourcepub fn subscribe(
&mut self,
peer_id: String,
topic: MessageTopic,
filter: SubscriptionFilter,
now: u64,
) -> u64
pub fn subscribe( &mut self, peer_id: String, topic: MessageTopic, filter: SubscriptionFilter, now: u64, ) -> u64
Register a subscription and return its stable ID.
If a subscription with the same derived ID already exists it is silently replaced.
Sourcepub fn unsubscribe(&mut self, subscription_id: u64) -> bool
pub fn unsubscribe(&mut self, subscription_id: u64) -> bool
Remove the subscription with the given subscription_id.
Returns true if a subscription was found and removed, false if none
existed with that ID.
Sourcepub fn route(&mut self, message: &RoutingMessage, now: u64) -> Vec<u64>
pub fn route(&mut self, message: &RoutingMessage, now: u64) -> Vec<u64>
Route message to all matching subscriptions.
Rules applied in order:
- If
message.ttl == 0the message is immediately dropped (counted intotal_dropped); an emptyVecis returned. - For every subscription,
Self::matchesis called. Matching subscriptions have theirmessage_countincremented and receive a successfulDeliveryRecord. - If no subscriptions match,
total_droppedis incremented and a failingDeliveryRecordwithsubscription_id = 0is appended.
Returns the list of subscription IDs that were delivered to.
Sourcepub fn matches(sub: &Subscription, msg: &RoutingMessage) -> bool
pub fn matches(sub: &Subscription, msg: &RoutingMessage) -> bool
Return true when sub’s topic matches msg’s topic and the
subscription’s filter accepts the message.
This is a pub method as required by the specification.
Sourcepub fn filter_matches(filter: &SubscriptionFilter, msg: &RoutingMessage) -> bool
pub fn filter_matches(filter: &SubscriptionFilter, msg: &RoutingMessage) -> bool
Recursively evaluate filter against msg.
This is a pub method as required by the specification.
Sourcepub fn subscriptions_for_topic(
&self,
topic: &MessageTopic,
) -> Vec<&Subscription>
pub fn subscriptions_for_topic( &self, topic: &MessageTopic, ) -> Vec<&Subscription>
Return references to all active subscriptions for topic.
Sourcepub fn subscriptions_for_peer(&self, peer_id: &str) -> Vec<&Subscription>
pub fn subscriptions_for_peer(&self, peer_id: &str) -> Vec<&Subscription>
Return references to all subscriptions owned by peer_id.
Sourcepub fn delivery_rate(&self, since: u64) -> f64
pub fn delivery_rate(&self, since: u64) -> f64
Compute the delivery rate over records whose delivered_at >= since.
Returns 1.0 when there are no matching records.
Sourcepub fn recent_deliveries(&self, n: usize) -> Vec<&DeliveryRecord>
pub fn recent_deliveries(&self, n: usize) -> Vec<&DeliveryRecord>
Return references to the n most-recent delivery records.
If fewer than n records exist, all are returned.
Sourcepub fn evict_stale_subscriptions(&mut self, max_age_ms: u64, now: u64) -> usize
pub fn evict_stale_subscriptions(&mut self, max_age_ms: u64, now: u64) -> usize
Remove subscriptions that have never delivered a message and were
created more than max_age_ms milliseconds before now.
Returns the number of subscriptions evicted.
Sourcepub fn stats(&self, now: u64) -> SubRouterStats
pub fn stats(&self, now: u64) -> SubRouterStats
Return a SubRouterStats snapshot computed at the given logical
now timestamp (used only for the delivery_rate calculation).
Auto Trait Implementations§
impl Freeze for SubscriptionRouter
impl RefUnwindSafe for SubscriptionRouter
impl Send for SubscriptionRouter
impl Sync for SubscriptionRouter
impl Unpin for SubscriptionRouter
impl UnsafeUnpin for SubscriptionRouter
impl UnwindSafe for SubscriptionRouter
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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