-
Notifications
You must be signed in to change notification settings - Fork 16
add hello-single-latest sync example #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lxsaah
merged 4 commits into
aimdb-dev:main
from
harunugurlu:feat/93-hello-single-latest-example
Jul 16, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79351f3
add hello-single-latest sync example
harunugurlu 6b188bb
update sinle latest example to demonstrate SingleLatest with slow and…
harunugurlu a97d4be
Merge branch 'main' into feat/93-hello-single-latest-example
harunugurlu 3e21ada
fix: adjust sleep duration for slow tap observation in hello-single-l…
harunugurlu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "hello-single-latest" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "AimDB minimal sync example demonstrating SingleLatest buffer semantics" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| aimdb-core = { path = "../../aimdb-core", features = ["std"] } | ||
| aimdb-tokio-adapter = { path = "../../aimdb-tokio-adapter", features = [ | ||
| "tokio-runtime", | ||
| ] } | ||
| aimdb-sync = { path = "../../aimdb-sync" } | ||
|
|
||
| # Runtime for initialization | ||
| tokio = { workspace = true, features = ["time"] } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # hello-single-latest: SingleLatest buffer demo | ||
|
|
||
| The `SingleLatest` buffer stores the current value of a record. Each new write replaces the previous value, so subscribers read the latest value instead of replaying every intermediate update. This is useful for feature flags, configuration, UI state, and other records where stale values can be skipped. | ||
|
|
||
| ## How it works | ||
|
|
||
| This example registers a `FeatureFlag` record with: | ||
| - `BufferCfg::SingleLatest` | ||
| - 2 async `.tap()` that observe rollout updates | ||
| - One of the tap observers is "fast", that doesn't wait between receives | ||
| - One of the tap observers is "slow", that waits for 2000 ms between receives | ||
|
|
||
| The program attaches AimDB through its sync API, and publishes rollout updates using the sync, blocking `producer.set()` method. The taps are managed by AimDB's internal async runtime, so the application can use a synchronous `main` function without `async` or `#[tokio::main]`. | ||
|
|
||
| The synchronous producer publishes one update per second. If multiple values are written before the tap reads them, SingleLatest retains only the newest value. In the example, while the fast tap reads the value in the single latest slot, the slow one skips overwritten intermediate values. | ||
|
|
||
| ## How to run | ||
|
|
||
| From the workspace root, run: | ||
|
|
||
| ```bash | ||
| cargo run -p hello-single-latest | ||
| ``` | ||
|
|
||
| Expected output includes lines similar to: | ||
|
|
||
| ```text | ||
| === AimDB SingleLatest Sync Demo === | ||
|
|
||
| 1. Building database and attaching for sync API... | ||
| 2. Creating producer and producing values... | ||
| 3. Producing rollout percentage values | ||
| Main: Setting FeatureFlag 0% | ||
| [info] [fast] rollout: 0% | ||
| [info] [slow] rollout: 0% | ||
| Main: Setting FeatureFlag 25% | ||
| [info] [fast] rollout: 25% | ||
| Main: Setting FeatureFlag 50% | ||
| [info] [fast] rollout: 50% | ||
| [info] [slow] rollout: 50% | ||
| Main: Setting FeatureFlag 75% | ||
| [info] [fast] rollout: 75% | ||
| Main: Setting FeatureFlag 100% | ||
| [info] [fast] rollout: 100% | ||
| [info] [slow] rollout: 100% | ||
| 4. Shutting down... | ||
| Done. The sync producer published rollout updates observed by a SingleLatest tap. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| //! # AimDB SingleLatest Sync Demo | ||
| //! This example demonstrates the `SingleLatest` buffer primitive using AimDB's synchronous API. | ||
| //! | ||
| //! ## What This Demo Shows | ||
| //! | ||
| //! 1. Building an AimDB instance with a typed record and 2 tap observers (slow and fast) | ||
| //! 2. Attaching the database to get a sync handle | ||
| //! 3. Creating a producer in sync context | ||
| //! 4. Setting values using blocking operations | ||
| //! 5. Clean shutdown with detach() | ||
|
|
||
| use aimdb_core::{buffer::BufferCfg, AimDbBuilder, Consumer, RuntimeContext}; | ||
| use aimdb_sync::AimDbBuilderSyncExt; // Extension trait for .attach() | ||
| use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; // Extension for .buffer() | ||
| use std::{sync::Arc, thread, time::Duration}; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct FeatureFlag { | ||
| rollout_percent: f32, | ||
| } | ||
|
|
||
| const KEY: &str = "config.checkout_rollout"; | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| println!("=== AimDB SingleLatest Sync Demo ===\n"); | ||
|
|
||
| // Step 1: Build the database and attach it for sync API usage | ||
| println!("1. Building database and attaching for sync API..."); | ||
|
|
||
| let adapter = Arc::new(TokioAdapter); | ||
| let mut builder = AimDbBuilder::new().runtime(adapter); | ||
|
|
||
| builder.configure::<FeatureFlag>(KEY, |reg| { | ||
| // Configure a SingleLatest buffer | ||
| reg.buffer(BufferCfg::SingleLatest) | ||
| // Register 2 taps that observe rollout percentage values | ||
| .tap(rollout_observer_fast) | ||
| .tap(rollout_observer_slow); | ||
| }); | ||
|
|
||
| // Build happens inside the runtime thread where Tokio context exists | ||
| let handle = builder.attach()?; | ||
|
|
||
| // Step 2: Create a synchronous producer | ||
| println!("2. Creating producer and producing values..."); | ||
|
|
||
| let producer = handle.producer::<FeatureFlag>(KEY)?; | ||
|
|
||
| // Step 3: Produce values | ||
| println!("3. Producing rollout percentage values"); | ||
|
|
||
| for i in 0..5 { | ||
| let feature_flag = FeatureFlag { | ||
| rollout_percent: i as f32 * 25.0, | ||
| }; | ||
| println!( | ||
| " Main: Setting FeatureFlag {}%", | ||
| feature_flag.rollout_percent | ||
| ); | ||
|
|
||
| // Use blocking send | ||
| producer.set(feature_flag)?; | ||
|
|
||
| // Producer publishes new value every second | ||
| if i < 4 { | ||
| thread::sleep(Duration::from_millis(1000)); | ||
| } | ||
| } | ||
|
|
||
| // Give the slow tap time to observe the final update before shutdown. | ||
| thread::sleep(Duration::from_millis(1000)); | ||
|
harunugurlu marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Step 4: Clean shutdown | ||
| // Detach the handle to gracefully shut down the runtime thread | ||
|
|
||
| println!("4. Shutting down..."); | ||
|
|
||
| handle.detach()?; | ||
|
|
||
| println!("Done. The sync producer published rollout updates observed by fast and slow SingleLatest taps"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| // A fast tap observer | ||
| async fn rollout_observer_fast(ctx: RuntimeContext, consumer: Consumer<FeatureFlag>) { | ||
| let mut reader = consumer.subscribe(); | ||
|
|
||
| while let Ok(feature_flag) = reader.recv().await { | ||
| ctx.log().info(&format!( | ||
| "[fast] rollout: {}%", | ||
| feature_flag.rollout_percent | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // A slow tap observer | ||
| async fn rollout_observer_slow(ctx: RuntimeContext, consumer: Consumer<FeatureFlag>) { | ||
| let mut reader = consumer.subscribe(); | ||
| let time = ctx.time(); | ||
|
|
||
| while let Ok(feature_flag) = reader.recv().await { | ||
| ctx.log().info(&format!( | ||
| "[slow] rollout: {}%", | ||
| feature_flag.rollout_percent | ||
| )); | ||
|
|
||
| time.sleep_millis(2000).await; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.