-
Notifications
You must be signed in to change notification settings - Fork 9
docs: add async SingleLatest example #112
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 2 commits into
aimdb-dev:main
from
nvphungdev:examples/single-latest-async
May 18, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,12 @@ | ||
| [package] | ||
| name = "hello-single-latest-async" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| description = "AimDB minimal async 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"] } | ||
| 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,37 @@ | ||
| # hello-single-latest-async: SingleLatest buffer demo | ||
|
|
||
| The `SingleLatest` buffer stores the current value for a record. New writes replace the previous value, so subscribers read the latest state instead of replaying every intermediate update. Use it for feature flags, configuration, UI state, or other records where stale values should be skipped. | ||
|
|
||
| ## How it works | ||
|
|
||
| This example registers a `FeatureGate` record with: | ||
|
|
||
| - `BufferCfg::SingleLatest` | ||
| - an async `.source()` that publishes rollout percentages | ||
| - an async `.tap()` that observes the latest value whenever it changes | ||
|
|
||
| The source sends an initial burst of updates without waiting between writes. The tap prints the latest observed rollout, demonstrating that the buffer carries current state rather than a full event log. | ||
|
|
||
| ## How to run | ||
|
|
||
| From the workspace root, run: | ||
|
|
||
| ```bash | ||
| cargo run -p hello-single-latest-async | ||
| ``` | ||
|
|
||
| Expected output includes lines similar to: | ||
|
|
||
| ```text | ||
| === hello-single-latest-async: SingleLatest buffer demo === | ||
|
|
||
| source published rollout: 0% | ||
| source published rollout: 10% | ||
| source published rollout: 25% | ||
| tap observed current rollout: 25% | ||
| source published rollout: 50% | ||
| tap observed current rollout: 50% | ||
| source published rollout: 100% | ||
| tap observed current rollout: 100% | ||
| Done. SingleLatest keeps only the current value for each subscriber. | ||
| ``` |
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,85 @@ | ||
| use aimdb_core::{buffer::BufferCfg, AimDbBuilder, Consumer, Producer, RuntimeContext}; | ||
| use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| struct FeatureGate { | ||
| rollout_percent: u8, | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| println!("=== hello-single-latest-async: SingleLatest buffer demo ===\n"); | ||
|
|
||
| let adapter = Arc::new(TokioAdapter::new()?); | ||
| let mut builder = AimDbBuilder::new().runtime(adapter); | ||
|
|
||
| builder.configure::<FeatureGate>("config.checkout_rollout", |reg| { | ||
| reg.buffer(BufferCfg::SingleLatest) | ||
| .source(rollout_source) | ||
| .tap(rollout_observer); | ||
| }); | ||
|
|
||
| let _db = builder.build().await?; | ||
|
|
||
| tokio::time::sleep(Duration::from_millis(700)).await; | ||
| println!("Done. SingleLatest keeps only the current value for each subscriber."); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn rollout_source( | ||
| ctx: RuntimeContext<TokioAdapter>, | ||
| producer: Producer<FeatureGate, TokioAdapter>, | ||
| ) { | ||
| let time = ctx.time(); | ||
|
|
||
| time.sleep(time.millis(50)).await; | ||
|
|
||
| for rollout_percent in [0, 10, 25] { | ||
| publish_rollout(&producer, rollout_percent).await; | ||
| } | ||
|
|
||
| for rollout_percent in [50, 100] { | ||
| time.sleep(time.millis(120)).await; | ||
| publish_rollout(&producer, rollout_percent).await; | ||
| } | ||
| } | ||
|
|
||
| async fn publish_rollout(producer: &Producer<FeatureGate, TokioAdapter>, rollout_percent: u8) { | ||
| let gate = FeatureGate { rollout_percent }; | ||
| match producer.produce(gate).await { | ||
| Ok(()) => println!("source published rollout: {rollout_percent}%"), | ||
| Err(err) => eprintln!("failed to publish rollout {rollout_percent}%: {err}"), | ||
| } | ||
| } | ||
|
|
||
| async fn rollout_observer( | ||
| ctx: RuntimeContext<TokioAdapter>, | ||
| consumer: Consumer<FeatureGate, TokioAdapter>, | ||
| ) { | ||
| let Ok(mut reader) = consumer.subscribe() else { | ||
| eprintln!("failed to subscribe to config.checkout_rollout"); | ||
| return; | ||
| }; | ||
| let time = ctx.time(); | ||
|
|
||
| let mut first = true; | ||
| while let Ok(gate) = reader.recv().await { | ||
| if first { | ||
| first = false; | ||
| if gate.rollout_percent != 0 { | ||
| println!( | ||
| " (rollouts before {}% were overwritten before the tap could read them - SingleLatest keeps only the latest)", | ||
| gate.rollout_percent | ||
| ); | ||
| } | ||
| } | ||
| println!("tap observed current rollout: {}%", gate.rollout_percent); | ||
| if gate.rollout_percent == 100 { | ||
| break; | ||
| } | ||
| time.sleep(time.millis(90)).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.