Lightweight DB-backed coordination primitive with leases and fencing tokens
Project description
Sentinel
A lightweight coordination primitive for backend systems. Useful for coordinating workers, preventing duplicate jobs, and ensuring safe execution with fencing tokens.
Installation
pip install sentinel-coordination
Features
- DB-backed leases
- Fencing tokens (prevents stale worker writes)
- Simple API (
acquire,heartbeat,release,lease)
Example
import sentinel
with sentinel.lease(conn, "order_123", ttl_ms=5000, get_conn=get_conn):
process_order()
API
acquire
result = sentinel.acquire(conn, key, ttl_ms=5000)
Returns:
acquired(bool)owner_id(str)expires_at(timestamp)fencing_token(int)
heartbeat
result = sentinel.heartbeat(conn, key, owner_id, ttl_ms=5000)
Returns:
extended(bool)
release
result = sentinel.release(conn, key, owner_id)
Returns:
released(bool)
lease (recommended)
with sentinel.lease(conn, key, ttl_ms=5000, get_conn=get_conn):
do_work()
Handles:
- acquire
- automatic heartbeat
- release on exit
Fencing Tokens (Important)
Each acquire returns a monotonically increasing fencing token.
To use fencing tokens safely:
- Store
fencing_tokenin your application data - Include a condition in all writes
Example:
UPDATE orders
SET status = 'paid', fencing_token = %s
WHERE id = %s AND fencing_token <= %s;
This ensures:
- stale workers cannot overwrite newer ones
- race conditions are safe
Status
⚠️ Beta (v0.1.0)
Limitations
- No retries yet
- No async support
- TTL tuning required
Long-running / stuck jobs
The lease() helper maintains ownership via continuous heartbeats.
If a job:
- hangs
- enters an infinite loop
- or gets stuck without crashing
it will continue renewing the lease indefinitely.
This prevents other workers from taking over.
Recommendation:
- Set appropriate timeouts at the application level
- Avoid unbounded blocking operations
- Consider external watchdogs for critical workflows
Philosophy
Sentinel does not eliminate race conditions. It makes them safe.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sentinel_coordination-0.1.3.tar.gz.
File metadata
- Download URL: sentinel_coordination-0.1.3.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09f0bd54fe7cc384ccee52d4c0ca8d6721db184e2092f76ac42b756c6056b68f
|
|
| MD5 |
2e46c0d2633877056fcea391a7b5221d
|
|
| BLAKE2b-256 |
046bfe4e42d8d6e1125add59778e0a8caf35df4cdf9b9c3d56eac3a70c95ea75
|
File details
Details for the file sentinel_coordination-0.1.3-py3-none-any.whl.
File metadata
- Download URL: sentinel_coordination-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30067a887ac7e212712194671be7fe5e4fe4789c0b1c310ffcb7bef21c4c8e85
|
|
| MD5 |
32295e07a861e5f643d2d0554ec5bf9c
|
|
| BLAKE2b-256 |
52a5f5afdca11a74fea96c5a991e90a6755835b043f59ed4f1ef87455ec606be
|