Skip to main content

with_payload/
with_payload.rs

1//! Beat loop that packs queue depth and last error code into the 32-bit payload.
2//!
3//! Layout: high 16 bits = queue_depth (capped), low 16 bits = last_error_code.
4//! The observer carries the payload opaquely; decoding is the agent's concern.
5//!
6//! VLP v0.2 shrunk `payload` from `u64` to `u32` to make room for the
7//! CRC-32C wire-integrity trailer. Agents that need more than 4 bytes of
8//! context should externalize the data and reference it from the payload
9//! (e.g. as a slot index into a shared ring buffer).
10//!
11//! Run alongside varta-watch:
12//!
13//! ```sh
14//! varta-watch --socket /tmp/varta.sock --threshold-ms 2000 &
15//! cargo run --example with_payload
16//! ```
17
18use std::sync::atomic::{AtomicU16, Ordering};
19
20static QUEUE_DEPTH: AtomicU16 = AtomicU16::new(0);
21static LAST_ERROR: AtomicU16 = AtomicU16::new(0);
22
23fn main() -> std::io::Result<()> {
24    let mut agent = varta_client::Varta::connect("/tmp/varta.sock")?;
25    loop {
26        let depth = QUEUE_DEPTH.load(Ordering::Relaxed);
27        let err = LAST_ERROR.load(Ordering::Relaxed);
28        let payload = ((depth as u32) << 16) | (err as u32);
29        match agent.beat(varta_client::Status::Ok, payload) {
30            varta_client::BeatOutcome::Sent => {}
31            varta_client::BeatOutcome::Dropped(_) => {
32                eprintln!("varta: beat dropped (observer down or queue full)");
33            }
34            varta_client::BeatOutcome::Failed(e) => {
35                eprintln!("varta: beat failed: {e}");
36            }
37        }
38        std::thread::sleep(std::time::Duration::from_millis(500));
39    }
40}