Lockserver
Lockserver is a distributed lock server for coordinating access to shared resources across multiple workers or processes. It provides an HTTP API and a Rust client library with ergonomic macros for easy distributed locking.
Features
- Simple API for acquiring and releasing locks
- HTTP API only (no TCP service)
- Client library with ergonomic macros (
lock_scope!) - Blocking and non-blocking lock acquisition
- Ready for publishing to crates.io
Usage
Start the server
Security: Shared Secret Authorization
All client requests must include a shared secret for authorization. The server and client must agree on the same secret, set via the LOCKSERVER_SECRET environment variable or .env file. The client sends this secret in the X-LOCKSERVER-SECRET HTTP header.
Example .env:
LOCKSERVER_SECRET=your-strong-secret
Server:
LOCKSERVER_SECRET=your-strong-secret
cargo run --release
Client:
LOCKSERVER_SECRET=your-strong-secret
Or pass the secret directly to the client constructor.
You can configure the bind IP and HTTP port using CLI arguments, environment variables, or a .env file (using dotenvy):
CLI arguments (override env/.env):
# or short form:
Environment variables or .env file:
LOCKSERVER_BIND_IP=127.0.0.1
LOCKSERVER_PORT=9000
Then just run:
HTTP API (default port 8080)
- Acquire a lock:
POST /acquirewith JSON{ "resource": "myres", "owner": "worker1" } - Release a lock:
POST /releasewith JSON{ "resource": "myres", "owner": "worker1" }
Example using curl (with secret):
Rust Client Library & Macro
Add to your Cargo.toml:
[]
= "0.1"
Client configuration
The client can load the server address and owner from environment variables or a .env file:
LOCKSERVER_ADDR=127.0.0.1:8080
LOCKSERVER_OWNER=worker1
Or pass them directly:
use ;
// Loads from env/.env if None (including secret)
let client = new_with_env;
lock_scope!;
// Override address, owner, or secret:
let client = new_with_env;
// Non-blocking mode:
if let Ok = client.acquire_with_mode
For more advanced usage, see the integration tests in tests/lock_scope_macro.rs.
License
Licensed under the MIT License.