expression

Detect a lagging broadcast receiver

A receiver that falls behind the retained history receives an explicit lag error. Its next read can continue from retained events. Decide whether to refresh a snapshot, report loss, or terminate; do not pretend missed events were delivered. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): missed 1 2 3 Common mistake: Ignoring Lagged in a stream whose correctness depends on receiving every event.

Formal Syntax & Structure

rust syntaxrust
broadcast::error::RecvError::Lagged(count)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio::sync::broadcast::{channel, error::RecvError};    let (tx, mut rx) = channel(2);    for n in 1..=3 { tx.send(n).unwrap(); }    match rx.recv().await {        Err(RecvError::Lagged(n)) => println!("missed {n}"),        _ => unreachable!(),    }    println!("{} {}", rx.recv().await.unwrap(), rx.recv().await.unwrap());}
Rust Reference