{"id":30249188,"url":"https://github.com/flowerinthenight/spindle-rs","last_synced_at":"2026-01-04T13:12:54.031Z","repository":{"id":266206879,"uuid":"897695686","full_name":"flowerinthenight/spindle-rs","owner":"flowerinthenight","description":"A distributed locking crate built on Cloud Spanner and TrueTime.","archived":false,"fork":false,"pushed_at":"2025-04-11T06:39:06.000Z","size":93,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-15T18:03:12.515Z","etag":null,"topics":["cloud-spanner","distributed-lock","distributed-locking","gcp","rust","spanner","truetime"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flowerinthenight.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-03T04:30:51.000Z","updated_at":"2025-04-11T06:39:01.000Z","dependencies_parsed_at":"2024-12-03T05:29:09.868Z","dependency_job_id":"216db7ec-6ad2-4dd1-a4db-bfc498094c51","html_url":"https://github.com/flowerinthenight/spindle-rs","commit_stats":null,"previous_names":["flowerinthenight/spindle-rs"],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/flowerinthenight/spindle-rs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowerinthenight%2Fspindle-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowerinthenight%2Fspindle-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowerinthenight%2Fspindle-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowerinthenight%2Fspindle-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flowerinthenight","download_url":"https://codeload.github.com/flowerinthenight/spindle-rs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowerinthenight%2Fspindle-rs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270546174,"owners_count":24604455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-spanner","distributed-lock","distributed-locking","gcp","rust","spanner","truetime"],"created_at":"2025-08-15T08:38:52.361Z","updated_at":"2026-01-04T13:12:53.998Z","avatar_url":"https://github.com/flowerinthenight.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![main](https://github.com/flowerinthenight/spindle-rs/actions/workflows/main.yml/badge.svg)](https://github.com/flowerinthenight/spindle-rs/actions/workflows/main.yml)\n[![crates.io](https://img.shields.io/crates/v/spindle_rs)](https://crates.io/crates/spindle_rs)\n[![docs-rs](https://img.shields.io/docsrs/spindle_rs)](https://docs.rs/spindle_rs/latest/spindle_rs/)\n\n## Overview\n\nThis crate implements distributed locking using [Cloud Spanner](https://cloud.google.com/spanner/). It relies on Spanner's [TrueTime](https://cloud.google.com/spanner/docs/true-time-external-consistency) and [transactions](https://cloud.google.com/spanner/docs/transactions) support to achieve its locking mechanism. It's a port of the original [spindle](https://github.com/flowerinthenight/spindle), which is written in Go.\n\n## Use cases\nOne use case for this library is [leader election](https://en.wikipedia.org/wiki/Leader_election). If you want one host/node/pod to be the leader within a cluster/group, you can achieve that with this crate. When the leader fails, it will fail over to another host/node/pod within a specific timeout. That said, you might want to check out [hedge-rs](https://github.com/flowerinthenight/hedge-rs/), which is a memberlist tracking library built on top of this crate.\n\n## Usage\nAt the moment, the table needs to be created beforehand using the following DDL (`locktable` is just an example):\n```sql\nCREATE TABLE locktable (\n    name STRING(MAX) NOT NULL,\n    heartbeat TIMESTAMP OPTIONS (allow_commit_timestamp=true),\n    token TIMESTAMP OPTIONS (allow_commit_timestamp=true),\n    writer STRING(MAX),\n) PRIMARY KEY (name)\n```\n\nAfter creating the lock object, you will call the `run()` function which will attempt to acquire a named lock at a regular interval (lease duration). A `has_lock()` function is provided which returns true (along with the lock token) if the lock is successfully acquired. Something like:\n\n```rust\nuse spindle_rs::*;\n...\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn Error\u003e\u003e {\n    let (tx, rx) = channel();\n    ctrlc::set_handler(move || tx.send(()).unwrap())?;\n    let mut lock = LockBuilder::new()\n        .db(\"projects/p/instances/i/databases/db\".to_string())\n        .table(\"locktable\".to_string()) // see CREATE TABLE\n        .name(\"spindle-rs\".to_string()) // lock name\n        .duration_ms(3000)\n        .build();\n\n    lock.run()?;\n\n    // Wait for a bit before calling has_lock().\n    thread::sleep(Duration::from_secs(10));\n    let (locked, node, token) = lock.has_lock();\n    println!(\"has_lock: {locked}, {node}, {token}\");\n\n    // Wait for Ctrl-C.\n    rx.recv()?;\n    lock.close();\n\n    Ok(())\n}\n```\n\n## Example\n\nA sample [code](./example/src/main.rs) is provided to demonstrate the mechanism through logs. You can try running multiple processes in multiple terminals.\n\n```bash\n$ git clone https://github.com/flowerinthenight/spindle-rs\n$ cd spindle-rs/\n$ cargo build\n# Update args with your actual values:\n#   args[1] = database string\n#   args[2] = table name\n$ RUST_LOG=info ./target/debug/example \\\n  projects/p/instances/i/databases/db \\\n  locktable\n```\n\nThe leader process should output something like `leader active (me)`. You can then try to stop (Ctrl+C) that process and observe another one taking over as leader.\n\n## License\n\nThis library is licensed under the [Apache 2.0 License](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflowerinthenight%2Fspindle-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflowerinthenight%2Fspindle-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflowerinthenight%2Fspindle-rs/lists"}