Coverage for src / lexigram / contracts / core / stores.py: 0%
8 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
1"""Persistent distributed lock store protocol.
3Provides advisory, TTL-based distributed locks backed by a persistent store
4(SQL advisory locks, a SQL ``locks`` table, Redis, etc.), allowing multiple
5services to coordinate exclusive access to shared resources.
7Implementations ship in ``lexigram-sql`` (``lexigram.sql.stores``) so they can
8share the application's database connection pool without creating a lateral
9cross-extension dependency.
11Example::
13 from lexigram.contracts.core.stores import LockStoreProtocol
15 store = await container.resolve(LockStoreProtocol)
16 acquired = await store.acquire("billing_job", owner=instance_id, ttl=60)
17 if acquired:
18 try:
19 ...
20 finally:
21 await store.release("billing_job", owner=instance_id)
22"""
24from __future__ import annotations
26from typing import Protocol, runtime_checkable
29@runtime_checkable
30class LockStoreProtocol(Protocol):
31 """Persistent distributed lock store.
33 Individual lock handles are *not* returned; all operations are done via
34 the lock name and owner identifier.
35 """
37 async def acquire(self, lock_name: str, owner: str, ttl: int) -> bool:
38 """Attempt to acquire the named lock.
40 Args:
41 lock_name: Globally unique lock identifier.
42 owner: Identifier of the requesting owner (e.g. host + PID).
43 ttl: Lock time-to-live in seconds. The lock is automatically
44 released after this duration to prevent deadlocks.
46 Returns:
47 ``True`` if the lock was acquired; ``False`` if it is already held.
48 """
49 ...
51 async def release(self, lock_name: str, owner: str) -> bool:
52 """Release the named lock *only if* it is held by *owner*.
54 Args:
55 lock_name: Globally unique lock identifier.
56 owner: Must match the owner that acquired the lock.
58 Returns:
59 ``True`` if the lock was released; ``False`` if it was not held
60 by *owner* or did not exist.
61 """
62 ...
64 async def extend(self, lock_name: str, owner: str, ttl: int) -> bool:
65 """Extend the TTL of a currently-held lock.
67 Args:
68 lock_name: Globally unique lock identifier.
69 owner: Must match the owner that currently holds the lock.
70 ttl: New time-to-live in seconds from now.
72 Returns:
73 ``True`` if the extension was applied; ``False`` if the lock was
74 not held by *owner*.
75 """
76 ...
79__all__ = ["LockStoreProtocol"]