Compare commits
2 Commits
eda50d290a
...
c2c8c6b554
Author | SHA1 | Date | |
---|---|---|---|
|
c2c8c6b554 | ||
21c1507ebe |
@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Added
|
### Added
|
||||||
- update basic example with a more simple acquire and run function
|
- update basic example with a more simple acquire and run function
|
||||||
|
|
||||||
|
### Docs
|
||||||
|
- update master
|
||||||
|
|
||||||
## [0.1.1] - 2025-07-04
|
## [0.1.1] - 2025-07-04
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
35
README.md
35
README.md
@ -71,31 +71,28 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
// Ensure the KV bucket exists
|
// Ensure the KV bucket exists
|
||||||
leader.create_bucket().await?;
|
leader.create_bucket().await?;
|
||||||
|
|
||||||
// Spawn the election loop
|
// Attempts to acquire election loop, will call inner function if it wins, if it loses it will retry over again.
|
||||||
tokio::spawn({
|
// Will block until either the inner function returns and error, or the election processes crashes, intended to allow the application to properly restart
|
||||||
let leader = leader.clone();
|
|
||||||
async move {
|
|
||||||
leader
|
|
||||||
.start(CancellationToken::default())
|
|
||||||
.await
|
|
||||||
.expect("leadership loop failed");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Do work while we are the leader
|
|
||||||
leader
|
leader
|
||||||
.do_while_leader(|cancel_token| async move {
|
.acquire_and_run({
|
||||||
loop {
|
move |token| {
|
||||||
if cancel_token.is_cancelled() {
|
let leader_id = leader_id.clone();
|
||||||
break;
|
|
||||||
|
async move {
|
||||||
|
loop {
|
||||||
|
if token.is_cancelled() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::info!(leader_id, "do work as leader");
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tracing::info!("🔑 I am the leader—doing work");
|
|
||||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user