Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
database/LockStore: use Linearizer for lock acquisition
Browse files Browse the repository at this point in the history
This fixes the "test_acquire_contention" test case added in the previous
commit.

Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed May 23, 2022
1 parent 68f7873 commit 22eb9b7
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions synapse/storage/databases/main/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
LoggingTransaction,
)
from synapse.util import Clock
from synapse.util.async_helpers import Linearizer
from synapse.util.stringutils import random_string

if TYPE_CHECKING:
Expand Down Expand Up @@ -84,6 +85,8 @@ def __init__(
self._on_shutdown,
)

self.limiter = Linearizer("database_lock_linearizer")

@wrap_as_background_process("LockStore._on_shutdown")
async def _on_shutdown(self) -> None:
"""Called when the server is shutting down"""
Expand All @@ -103,6 +106,16 @@ async def try_acquire_lock(self, lock_name: str, lock_key: str) -> Optional["Loc
context manager if the lock is successfully acquired, which *must* be
used (otherwise the lock will leak).
"""
async with self.limiter.queue((lock_name, lock_key)):
return await self._try_acquire_lock(lock_name, lock_key)

async def _try_acquire_lock(
self, lock_name: str, lock_key: str
) -> Optional["Lock"]:
"""Try to acquire a lock for the given name/key. Will return an async
context manager if the lock is successfully acquired, which *must* be
used (otherwise the lock will leak).
"""

# Check if this process has taken out a lock and if it's still valid.
lock = self._live_tokens.get((lock_name, lock_key))
Expand Down

0 comments on commit 22eb9b7

Please sign in to comment.