Skip to content

Commit fa31dee

Browse files
committed
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
1 parent 604a62f commit fa31dee

5 files changed

Lines changed: 37 additions & 13 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
44
- repo: https://github.com/python/black
5-
rev: 21.5b1
5+
rev: 22.3.0
66
hooks:
77
- id: black
88

99
- repo: https://github.com/sqlalchemyorg/zimports
10-
rev: v0.4.1
10+
rev: v0.6.0
1111
hooks:
1212
- id: zimports
1313
args:
1414
- --keep-unused-type-checking
1515

1616
- repo: https://github.com/pycqa/flake8
17-
rev: 3.9.2
17+
rev: 4.0.1
1818
hooks:
1919
- id: flake8
2020
additional_dependencies:
2121
- flake8-import-order
2222
- flake8-builtins
23-
- flake8-docstrings>=1.3.1
23+
- flake8-docstrings>=1.6.0
2424
- flake8-rst-docstrings
2525
- pydocstyle
2626
- pygments

docs/build/unreleased/220.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. change::
2+
:tags: bug, redis
3+
:tickets: 220
4+
5+
Fixed regression caused by backwards-incompatible API changes in Redis that
6+
caused the "distributed lock" feature to not function.

dogpile/cache/backends/memcached.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def acquire(self, wait=True):
5858
elif not wait:
5959
return False
6060
else:
61-
sleep_time = (((i + 1) * random.random()) + 2 ** i) / 2.5
61+
sleep_time = (((i + 1) * random.random()) + 2**i) / 2.5
6262
time.sleep(sleep_time)
6363
if i < 15:
6464
i += 1

dogpile/cache/backends/redis.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ def _create_client(self):
150150

151151
def get_mutex(self, key):
152152
if self.distributed_lock:
153-
return self.writer_client.lock(
154-
"_lock{0}".format(key),
155-
timeout=self.lock_timeout,
156-
sleep=self.lock_sleep,
157-
thread_local=self.thread_local_lock,
153+
return _RedisLockWrapper(
154+
self.writer_client.lock(
155+
"_lock{0}".format(key),
156+
timeout=self.lock_timeout,
157+
sleep=self.lock_sleep,
158+
thread_local=self.thread_local_lock,
159+
)
158160
)
159161
else:
160162
return None
@@ -193,6 +195,22 @@ def delete_multi(self, keys):
193195
self.writer_client.delete(*keys)
194196

195197

198+
class _RedisLockWrapper:
199+
__slots__ = ("mutex", "__weakref__")
200+
201+
def __init__(self, mutex: typing.Any):
202+
self.mutex = mutex
203+
204+
def acquire(self, wait: bool = True) -> typing.Any:
205+
return self.mutex.acquire(blocking=wait)
206+
207+
def release(self) -> typing.Any:
208+
return self.mutex.release()
209+
210+
def locked(self) -> bool:
211+
return self.mutex.locked() # type: ignore
212+
213+
196214
class RedisSentinelBackend(RedisBackend):
197215
"""A `Redis <http://redis.io/>`_ backend, using the
198216
`redis-py <http://pypi.python.org/pypi/redis/>`_ backend.

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ deps=
3939
{memcached}: python-binary-memcached>=0.29.0
4040
{memcached}: pifpaf>=2.5.0
4141
{memcached}: pymemcache>=3.5.0
42-
{redis}: redis!=4.3.2,!=4.3.3
42+
{redis}: redis
4343
{redis}: pifpaf
44-
{redis_sentinel}: redis!=4.3.2,!=4.3.3
44+
{redis_sentinel}: redis
4545
{redis_sentinel}: pifpaf
4646
{cov}: pytest-cov
4747

@@ -83,7 +83,7 @@ deps=
8383
flake8-rst-docstrings
8484
# used by flake8-rst-docstrings
8585
pygments
86-
black==21.5b1
86+
black==22.3.0
8787
commands =
8888
flake8 ./dogpile/ ./tests/ setup.py {posargs}
8989
black --check .

0 commit comments

Comments
 (0)