From fa31deecd86bc4a4bfe19b294773ea4c0e37c44e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 8 Jun 2022 08:11:17 -0400 Subject: repair backwards incompat redis lock changes Fixed regression caused by backwards-incompatible API changes in Redis that caused the "distributed lock" feature to not function. This commit also updates black which was not passing due to the older version having dependency issues Fixes: #220 Change-Id: Ibba3307dbfd9dfeacc08af79800b681ce160dea9 --- .pre-commit-config.yaml | 8 ++++---- docs/build/unreleased/220.rst | 6 ++++++ dogpile/cache/backends/memcached.py | 2 +- dogpile/cache/backends/redis.py | 28 +++++++++++++++++++++++----- tox.ini | 6 +++--- 5 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 docs/build/unreleased/220.rst diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04d64a4..6d7ebae 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,25 +2,25 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/python/black - rev: 21.5b1 + rev: 22.3.0 hooks: - id: black - repo: https://github.com/sqlalchemyorg/zimports - rev: v0.4.1 + rev: v0.6.0 hooks: - id: zimports args: - --keep-unused-type-checking - repo: https://github.com/pycqa/flake8 - rev: 3.9.2 + rev: 4.0.1 hooks: - id: flake8 additional_dependencies: - flake8-import-order - flake8-builtins - - flake8-docstrings>=1.3.1 + - flake8-docstrings>=1.6.0 - flake8-rst-docstrings - pydocstyle - pygments diff --git a/docs/build/unreleased/220.rst b/docs/build/unreleased/220.rst new file mode 100644 index 0000000..0d46c6b --- /dev/null +++ b/docs/build/unreleased/220.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, redis + :tickets: 220 + + Fixed regression caused by backwards-incompatible API changes in Redis that + caused the "distributed lock" feature to not function. diff --git a/dogpile/cache/backends/memcached.py b/dogpile/cache/backends/memcached.py index 8163d70..7c247bc 100644 --- a/dogpile/cache/backends/memcached.py +++ b/dogpile/cache/backends/memcached.py @@ -58,7 +58,7 @@ class MemcachedLock(object): elif not wait: return False else: - sleep_time = (((i + 1) * random.random()) + 2 ** i) / 2.5 + sleep_time = (((i + 1) * random.random()) + 2**i) / 2.5 time.sleep(sleep_time) if i < 15: i += 1 diff --git a/dogpile/cache/backends/redis.py b/dogpile/cache/backends/redis.py index 5bff3ea..cf646fe 100644 --- a/dogpile/cache/backends/redis.py +++ b/dogpile/cache/backends/redis.py @@ -150,11 +150,13 @@ class RedisBackend(BytesBackend): def get_mutex(self, key): if self.distributed_lock: - return self.writer_client.lock( - "_lock{0}".format(key), - timeout=self.lock_timeout, - sleep=self.lock_sleep, - thread_local=self.thread_local_lock, + return _RedisLockWrapper( + self.writer_client.lock( + "_lock{0}".format(key), + timeout=self.lock_timeout, + sleep=self.lock_sleep, + thread_local=self.thread_local_lock, + ) ) else: return None @@ -193,6 +195,22 @@ class RedisBackend(BytesBackend): self.writer_client.delete(*keys) +class _RedisLockWrapper: + __slots__ = ("mutex", "__weakref__") + + def __init__(self, mutex: typing.Any): + self.mutex = mutex + + def acquire(self, wait: bool = True) -> typing.Any: + return self.mutex.acquire(blocking=wait) + + def release(self) -> typing.Any: + return self.mutex.release() + + def locked(self) -> bool: + return self.mutex.locked() # type: ignore + + class RedisSentinelBackend(RedisBackend): """A `Redis `_ backend, using the `redis-py `_ backend. diff --git a/tox.ini b/tox.ini index 31cff77..8b241dc 100644 --- a/tox.ini +++ b/tox.ini @@ -39,9 +39,9 @@ deps= {memcached}: python-binary-memcached>=0.29.0 {memcached}: pifpaf>=2.5.0 {memcached}: pymemcache>=3.5.0 - {redis}: redis!=4.3.2,!=4.3.3 + {redis}: redis {redis}: pifpaf - {redis_sentinel}: redis!=4.3.2,!=4.3.3 + {redis_sentinel}: redis {redis_sentinel}: pifpaf {cov}: pytest-cov @@ -83,7 +83,7 @@ deps= flake8-rst-docstrings # used by flake8-rst-docstrings pygments - black==21.5b1 + black==22.3.0 commands = flake8 ./dogpile/ ./tests/ setup.py {posargs} black --check . -- cgit v1.2.1