Remove useless reports, avoid DB-Races
This commit is contained in:
+8
-3
@@ -7,13 +7,14 @@ from dataclasses import dataclass
|
||||
@dataclass
|
||||
class InboxRunLease:
|
||||
inbox_id: str
|
||||
_lock: threading.Lock
|
||||
_locks: list[threading.Lock]
|
||||
_released: bool = False
|
||||
|
||||
def release(self) -> None:
|
||||
if not self._released:
|
||||
self._released = True
|
||||
self._lock.release()
|
||||
for lock in reversed(self._locks):
|
||||
lock.release()
|
||||
|
||||
def __enter__(self) -> "InboxRunLease":
|
||||
return self
|
||||
@@ -25,14 +26,18 @@ class InboxRunLease:
|
||||
class InboxRunLocks:
|
||||
def __init__(self) -> None:
|
||||
self._guard = threading.Lock()
|
||||
self._global_lock = threading.Lock()
|
||||
self._locks: dict[str, threading.Lock] = {}
|
||||
|
||||
def acquire(self, inbox_id: str, *, blocking: bool = False) -> InboxRunLease | None:
|
||||
if not self._global_lock.acquire(blocking=blocking):
|
||||
return None
|
||||
with self._guard:
|
||||
lock = self._locks.setdefault(inbox_id, threading.Lock())
|
||||
if not lock.acquire(blocking=blocking):
|
||||
self._global_lock.release()
|
||||
return None
|
||||
return InboxRunLease(inbox_id=inbox_id, _lock=lock)
|
||||
return InboxRunLease(inbox_id=inbox_id, _locks=[self._global_lock, lock])
|
||||
|
||||
def active(self, inbox_id: str) -> bool:
|
||||
lease = self.acquire(inbox_id, blocking=False)
|
||||
|
||||
Reference in New Issue
Block a user