pub struct MessageRouter {
pub stats: Arc<RouterStats>,
/* private fields */
}Expand description
Priority-based message router with per-handler queues and a dead-letter queue.
§Thread safety
All public methods are safe to call from multiple threads simultaneously.
Handlers are protected by a RwLock; per-handler queues and the dead-letter
queue are protected by a Mutex.
Fields§
§stats: Arc<RouterStats>Aggregate statistics.
Implementations§
Source§impl MessageRouter
impl MessageRouter
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty MessageRouter.
Sourcepub fn register_handler(
&self,
reg: HandlerRegistration,
) -> Result<(), RouterError>
pub fn register_handler( &self, reg: HandlerRegistration, ) -> Result<(), RouterError>
Register a new handler.
Returns RouterError::DuplicateHandler if a handler with the same
handler_id is already registered.
Sourcepub fn unregister_handler(&self, handler_id: &str)
pub fn unregister_handler(&self, handler_id: &str)
Unregister a handler by handler_id.
This is a no-op if the handler does not exist (no error is returned so callers can safely call this during shutdown without worrying about race conditions with double-unregister).
Sourcepub fn handler_count(&self) -> usize
pub fn handler_count(&self) -> usize
Return the number of currently registered handlers.
Sourcepub fn route(&self, msg: RoutedMessage) -> usize
pub fn route(&self, msg: RoutedMessage) -> usize
Route msg to all matching handlers.
Returns the number of handlers the message was dispatched to. When no handler matches, the message is pushed onto the dead-letter queue (evicting the oldest entry if the queue is full).
Messages that are already expired on arrival are counted as
total_expired_dropped and discarded immediately.
Sourcepub fn drain_for_handler(&self, handler_id: &str) -> Vec<RoutedMessage>
pub fn drain_for_handler(&self, handler_id: &str) -> Vec<RoutedMessage>
Drain all queued messages for handler_id in priority order (highest first).
Returns an empty Vec if the handler has no queued messages or is unknown.
Sourcepub fn dead_letter_count(&self) -> usize
pub fn dead_letter_count(&self) -> usize
Return the current number of messages in the dead-letter queue.
Sourcepub fn drain_dead_letter(&self) -> Vec<RoutedMessage>
pub fn drain_dead_letter(&self) -> Vec<RoutedMessage>
Drain and return all messages from the dead-letter queue.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MessageRouter
impl RefUnwindSafe for MessageRouter
impl Send for MessageRouter
impl Sync for MessageRouter
impl Unpin for MessageRouter
impl UnsafeUnpin for MessageRouter
impl UnwindSafe for MessageRouter
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