Skip to main content

MessageRateMonitor

Struct MessageRateMonitor 

Source
pub struct MessageRateMonitor { /* private fields */ }
Expand description

Message rate monitor for detecting high-frequency message scenarios.

This utility helps backends detect when message rates exceed a threshold and log warnings as specified in FR-016. It does not perform any automatic throttling or backpressure - it only logs warnings for user awareness.

§Usage

Backends can use this to monitor receive rates:

use canlink_hal::backend::MessageRateMonitor;

let mut monitor = MessageRateMonitor::new(1000); // Warn above 1000 msg/s

// In receive loop:
if let Some(msg) = backend.receive_message()? {
    monitor.record_message();
    process(msg);
}

§Thread Safety

This struct is not thread-safe. Each thread should have its own monitor or use external synchronization.

Implementations§

Source§

impl MessageRateMonitor

Source

pub fn new(threshold_per_second: u32) -> Self

Create a new message rate monitor.

§Arguments
  • threshold_per_second - Message rate threshold that triggers warnings
§Examples
use canlink_hal::backend::MessageRateMonitor;

// Warn when rate exceeds 1000 messages/second
let monitor = MessageRateMonitor::new(1000);
Source

pub fn record_message(&mut self) -> bool

Record a message and check if rate exceeds threshold.

Returns true if the rate exceeds the threshold (warning should be logged). Only returns true once per measurement window to avoid log spam.

§Examples
use canlink_hal::backend::MessageRateMonitor;

let mut monitor = MessageRateMonitor::new(1000);

// Record messages
if monitor.record_message() {
    // Rate exceeded threshold - handle warning
    eprintln!("Warning: High message rate detected");
}
Source

pub fn current_count(&self) -> u32

Get the current message count in this window.

Source

pub fn threshold(&self) -> u32

Get the configured threshold.

Source

pub fn reset(&mut self)

Reset the monitor state.

Trait Implementations§

Source§

impl Debug for MessageRateMonitor

Source§

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

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

impl Default for MessageRateMonitor

Source§

fn default() -> Self

Create a monitor with default threshold of 10000 messages/second.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.