#distributed-lock #etcd

elock

A lightweight distributed lock implementation built on top of etcd using leases and watch

2 releases

Uses new Rust 2024

0.1.1 Jul 10, 2025
0.1.0 Jul 9, 2025

#1245 in Concurrency

29 downloads per month

MIT license

15KB
170 lines

Elock

License Rust Kubernetes etcd

A lightweight distributed lock implementation built on top of etcd using leases and watch

Install

cargo add elock

Example

You can new a LockManager to manage etcd locks.


use elock::LockManager;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let mut manager = LockManager::new("127.0.0.1:2379")
        .with_tls("/etc/etcd/ssl/etcd-ca.pem", "/etc/etcd/ssl/etcd-client.pem", "/etc/etcd/ssl/etcd-client-key.pem")
        .init()
        .await;
    let mut locker = manager.create_lock("/test/lock").await;
    
    // Duration sets the timeout for acquiring a lock, similar to context.WithTimeout in Golang.
    locker.lock(Duration::from_secs(30)).await.unwrap();

    // Do something

    locker.unlock().await.unwrap();
    
    tracing::info!("Unlock successful");
}

Dependencies

~20–37MB
~527K SLoC