4 Commits

Author SHA1 Message Date
a3f2c33d04 Add renovate.json
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2025-07-05 00:51:05 +00:00
aaf3a72d3b feat: allow readme
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-04 13:28:28 +02:00
643d87895b chore(release): v0.1.2 (#2)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.1.2

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/noleader/pulls/2
2025-07-04 13:25:21 +02:00
21c1507ebe docs: update master
All checks were successful
continuous-integration/drone/push Build is passing
2025-07-04 13:24:24 +02:00
6 changed files with 36 additions and 23 deletions

View File

@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.1.2] - 2025-07-04
### Added
- update basic example with a more simple acquire and run function
### Docs
- update master
## [0.1.1] - 2025-07-04
### Added

2
Cargo.lock generated
View File

@@ -645,7 +645,7 @@ dependencies = [
[[package]]
name = "noleader"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"anyhow",
"async-nats",

View File

@@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"
[workspace.package]
version = "0.1.1"
version = "0.1.2"
license = "MIT"
[workspace.dependencies]

View File

@@ -71,31 +71,28 @@ async fn main() -> anyhow::Result<()> {
// Ensure the KV bucket exists
leader.create_bucket().await?;
// Spawn the election loop
tokio::spawn({
let leader = leader.clone();
async move {
leader
.start(CancellationToken::default())
.await
.expect("leadership loop failed");
}
});
// Do work while we are the leader
// Attempts to acquire election loop, will call inner function if it wins, if it loses it will retry over again.
// Will block until either the inner function returns and error, or the election processes crashes, intended to allow the application to properly restart
leader
.do_while_leader(|cancel_token| async move {
loop {
if cancel_token.is_cancelled() {
break;
.acquire_and_run({
move |token| {
let leader_id = leader_id.clone();
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?;
Ok(())
}
```

View File

@@ -1,7 +1,12 @@
[package]
name = "noleader"
version = "0.1.0"
edition = "2021"
edition = "2024"
readme = "../../README.md"
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]
anyhow.workspace = true

3
renovate.json Normal file
View File

@@ -0,0 +1,3 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
}