summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-06-08 08:11:17 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-06-10 09:27:30 -0400
commitfa31deecd86bc4a4bfe19b294773ea4c0e37c44e (patch)
treeda763e0de7b98604b8263fce31d6580ba96f3f4b
parent604a62f84f36ccc6fa08735ac8dce14950ef54f8 (diff)
downloaddogpile-cache-fa31deecd86bc4a4bfe19b294773ea4c0e37c44e.tar.gz
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
-rw-r--r--.pre-commit-config.yaml8
-rw-r--r--docs/build/unreleased/220.rst6
-rw-r--r--dogpile/cache/backends/memcached.py2
-rw-r--r--dogpile/cache/backends/redis.py28
-rw-r--r--tox.ini6
5 files changed, 37 insertions, 13 deletions
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 <http://redis.io/>`_ backend, using the
`redis-py <http://pypi.python.org/pypi/redis/>`_ 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 .