|
| 1 | +use std::time::Duration; |
| 2 | +use std::{fmt::Display, sync::Arc}; |
| 3 | + |
| 4 | +use aimdb_core::{buffer::BufferCfg, AimDbBuilder, Producer, RuntimeContext}; |
| 5 | +use aimdb_core::{Consumer, DbError}; |
| 6 | +use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, PartialEq)] |
| 9 | +pub struct Temperature { |
| 10 | + /// Temperature in degrees Celsius |
| 11 | + pub celcius: f32, |
| 12 | + |
| 13 | + /// Unix timestamp (milliseconds) |
| 14 | + pub timestamp: u64, |
| 15 | +} |
| 16 | + |
| 17 | +impl Display for Temperature { |
| 18 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 19 | + write!(f, "At {}: {} celcius", self.timestamp, self.celcius) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Main function |
| 24 | +#[tokio::main] |
| 25 | +async fn main() -> Result<(), DbError> { |
| 26 | + println!("=== hello-spmc-ring-async: SPMC ring buffer demo ===\n"); |
| 27 | + println!("Ring size: 10"); |
| 28 | + println!("Producer at rate 20.0 messages/sec"); |
| 29 | + println!("Observer 01 at rate 25.0 messages/sec"); |
| 30 | + println!("Observer 02 at rate 6.7 messages/sec"); |
| 31 | + println!("\n"); |
| 32 | + |
| 33 | + // configuration |
| 34 | + let adapter = Arc::new(TokioAdapter); |
| 35 | + let mut builder = AimDbBuilder::new().runtime(adapter); |
| 36 | + |
| 37 | + // Two observers consuming at different rates |
| 38 | + builder.configure::<Temperature>("sensor.temp", |reg| { |
| 39 | + reg.buffer(BufferCfg::SpmcRing { capacity: 10 }) |
| 40 | + .source(rollout_source) |
| 41 | + .tap(rollout_observer01) |
| 42 | + .tap(rollout_observer02); |
| 43 | + }); |
| 44 | + |
| 45 | + let (_db, runner) = builder.build().await?; |
| 46 | + tokio::spawn(runner.run()); |
| 47 | + tokio::time::sleep(Duration::from_millis(6000)).await; |
| 48 | + |
| 49 | + Ok(()) |
| 50 | +} |
| 51 | + |
| 52 | +async fn rollout_source(ctx: RuntimeContext, producer: Producer<Temperature>) { |
| 53 | + let time = ctx.time(); |
| 54 | + let t0: f32 = -20.0; |
| 55 | + for i in 0..40 { |
| 56 | + time.sleep_millis(50).await; |
| 57 | + // Read wall-clock time through the runtime abstraction rather than |
| 58 | + // `SystemTime::now()`, so the example stays runtime/sim-friendly. |
| 59 | + let timestamp = time.unix_time().map(|(secs, _)| secs).unwrap_or(0); |
| 60 | + publich_rollout(&producer, t0 + (i as f32 + 1.0) * 2.0, timestamp).await; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +async fn publich_rollout(producer: &Producer<Temperature>, t: f32, timestamp: u64) { |
| 65 | + let temperature = Temperature { |
| 66 | + celcius: t, |
| 67 | + timestamp, |
| 68 | + }; |
| 69 | + producer.produce(temperature); |
| 70 | +} |
| 71 | + |
| 72 | +async fn rollout_observer01(ctx: RuntimeContext, consumer: Consumer<Temperature>) { |
| 73 | + let mut reader = consumer.subscribe(); |
| 74 | + let time = ctx.time(); |
| 75 | + |
| 76 | + while let Ok(t) = reader.recv().await { |
| 77 | + println!("Observer 01: {}", t); |
| 78 | + time.sleep_millis(40).await; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async fn rollout_observer02(ctx: RuntimeContext, consumer: Consumer<Temperature>) { |
| 83 | + let mut reader = consumer.subscribe(); |
| 84 | + let time = ctx.time(); |
| 85 | + |
| 86 | + // Consume at slower rate, must miss data |
| 87 | + loop { |
| 88 | + match reader.recv().await { |
| 89 | + Ok(t) => { |
| 90 | + println!("Observer 02: {}", t); |
| 91 | + time.sleep_millis(150).await; |
| 92 | + } |
| 93 | + Err(DbError::BufferLagged { lag_count, .. }) => { |
| 94 | + println!("Observer 02 lagged, dropped {lag_count}"); |
| 95 | + } |
| 96 | + Err(_) => break, |
| 97 | + }; |
| 98 | + } |
| 99 | +} |
0 commit comments