Compare commits
1 Commits
renovate/c
...
eda50d290a
Author | SHA1 | Date | |
---|---|---|---|
|
eda50d290a |
@@ -11,9 +11,6 @@ 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
|
||||||
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -645,7 +645,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "noleader"
|
name = "noleader"
|
||||||
version = "0.1.1"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-nats",
|
"async-nats",
|
||||||
|
31
README.md
31
README.md
@@ -71,28 +71,31 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
// Ensure the KV bucket exists
|
// Ensure the KV bucket exists
|
||||||
leader.create_bucket().await?;
|
leader.create_bucket().await?;
|
||||||
|
|
||||||
// Attempts to acquire election loop, will call inner function if it wins, if it loses it will retry over again.
|
// Spawn the election loop
|
||||||
// Will block until either the inner function returns and error, or the election processes crashes, intended to allow the application to properly restart
|
tokio::spawn({
|
||||||
leader
|
let leader = leader.clone();
|
||||||
.acquire_and_run({
|
|
||||||
move |token| {
|
|
||||||
let leader_id = leader_id.clone();
|
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
loop {
|
leader
|
||||||
if token.is_cancelled() {
|
.start(CancellationToken::default())
|
||||||
return Ok(());
|
.await
|
||||||
|
.expect("leadership loop failed");
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
tracing::info!(leader_id, "do work as leader");
|
// Do work while we are the leader
|
||||||
|
leader
|
||||||
|
.do_while_leader(|cancel_token| async move {
|
||||||
|
loop {
|
||||||
|
if cancel_token.is_cancelled() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tracing::info!("🔑 I am the leader—doing work");
|
||||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
}
|
Ok(())
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -1,12 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "noleader"
|
name = "noleader"
|
||||||
edition = "2024"
|
version = "0.1.0"
|
||||||
readme = "../../README.md"
|
edition = "2021"
|
||||||
version.workspace = true
|
|
||||||
license.workspace = true
|
|
||||||
repository = "https://git.front.kjuulh.io/kjuulh/noleader"
|
|
||||||
authors = ["kjuulh <contact@kasperhermansen.com>"]
|
|
||||||
description = "A small leader election package using NATS keyvalue store as the distributed locking mechanism. Does not require a min / max set of nodes"
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
|
||||||
}
|
|
Reference in New Issue
Block a user