Expand description
Priority queue for message processing
Enables quality-of-service (QoS) by prioritizing critical messages over low-priority ones. Useful for safety-critical systems where some messages (e.g., emergency stop) must be processed before others (e.g., telemetry).
§Features
- Priority Levels: Define message importance (Critical, High, Normal, Low)
- Fair Processing: Round-robin within same priority level
- Starvation Prevention: Ensures low-priority messages eventually process
- Backpressure Support: Integrates with bounded channels
- Zero-Copy: Messages moved, not copied
§Example
use mecha10_core::prelude::*;
use mecha10_core::priority_queue::{PriorityReceiver, Priority};
use mecha10_core::topics::Topic;
#[derive(Debug, Deserialize)]
struct Event {
value: f32,
}
impl Message for Event {}
const EMERGENCY: Topic<Event> = Topic::new("/safety/emergency");
const COMMAND: Topic<Event> = Topic::new("/motor/command");
const TELEMETRY: Topic<Event> = Topic::new("/sensor/telemetry");
// Create priority receiver
let mut prio_rx = PriorityReceiver::new();
// Subscribe to multiple topics with different priorities
let emergency = ctx.subscribe(EMERGENCY).await?;
let commands = ctx.subscribe(COMMAND).await?;
let telemetry = ctx.subscribe(TELEMETRY).await?;
// Add with priorities
prio_rx.add_source(emergency, Priority::Critical);
prio_rx.add_source(commands, Priority::High);
prio_rx.add_source(telemetry, Priority::Low);
// Process messages in priority order
while let Some((msg, priority)) = prio_rx.recv().await {
match priority {
Priority::Critical => handle_emergency(msg).await?,
Priority::High => handle_command(msg).await?,
_ => handle_telemetry(msg).await?,
}
}