Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update aioredlock to support redis>=5.0.0 #111

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.7.3
current_version = 0.8.0
commit = True
tag = True

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ jobs:
strategy:
matrix:
python-version:
- 3.7
- 3.8
- 3.9
- "3.10"
- "3.11"
- "3.12"

services:
redis:
Expand Down
12 changes: 6 additions & 6 deletions aioredlock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from aioredlock.sentinel import Sentinel

__all__ = (
'Aioredlock',
'Lock',
'LockError',
'LockAcquiringError',
'LockRuntimeError',
'Sentinel'
"Aioredlock",
"Lock",
"LockError",
"LockAcquiringError",
"LockRuntimeError",
"Sentinel",
)
36 changes: 19 additions & 17 deletions aioredlock/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def log(self):
return logging.getLogger(__name__)

async def _set_lock(self, resource, lock_identifier, lease_time):

error = RuntimeError('Retry count less then one')
error = RuntimeError("Retry count less then one")

# Proportional drift time to the length of the lock
# See https://redis.io/topics/distlock#is-the-algorithm-asynchronous for more info
Expand All @@ -67,22 +66,23 @@ async def _set_lock(self, resource, lock_identifier, lease_time):
try:
# global try/except to catch CancelledError
for n in range(self.retry_count):
self.log.debug('Acquiring lock "%s" try %d/%d',
resource, n + 1, self.retry_count)
self.log.debug(
'Acquiring lock "%s" try %d/%d', resource, n + 1, self.retry_count
)
if n != 0:
delay = random.uniform(self.retry_delay_min,
self.retry_delay_max)
delay = random.uniform(self.retry_delay_min, self.retry_delay_max)
await asyncio.sleep(delay)
try:
elapsed_time = await self.redis.set_lock(resource, lock_identifier, lease_time)
elapsed_time = await self.redis.set_lock(
resource, lock_identifier, lease_time
)
except LockError as exc:
error = exc
continue

if lease_time - elapsed_time - drift <= 0:
error = LockError('Lock timeout')
self.log.debug('Timeout in acquiring the lock "%s"',
resource)
error = LockError("Lock timeout")
self.log.debug('Timeout in acquiring the lock "%s"', resource)
continue

error = None
Expand Down Expand Up @@ -114,8 +114,7 @@ async def _auto_extend(self, lock):
try:
await self.extend(lock)
except Exception:
self.log.debug('Error in extending the lock "%s"',
lock.resource)
self.log.debug('Error in extending the lock "%s"', lock.resource)

self._watchdogs[lock.resource] = asyncio.ensure_future(self._auto_extend(lock))

Expand Down Expand Up @@ -143,7 +142,9 @@ async def lock(self, resource, lock_timeout=None, lock_identifier=None):

lock = Lock(self, resource, lock_identifier, lock_timeout, valid=True)
if lock_timeout is None:
self._watchdogs[lock.resource] = asyncio.ensure_future(self._auto_extend(lock))
self._watchdogs[lock.resource] = asyncio.ensure_future(
self._auto_extend(lock)
)
self._locks[resource] = lock

return lock
Expand All @@ -161,7 +162,7 @@ async def extend(self, lock, lock_timeout=None):
self.log.debug('Extending lock "%s"', lock.resource)

if not lock.valid:
raise RuntimeError('Lock is not valid')
raise RuntimeError("Lock is not valid")
if lock_timeout is not None and lock_timeout <= 0:
raise ValueError("Lock timeout must be greater than 0 seconds.")

Expand Down Expand Up @@ -218,8 +219,9 @@ async def is_locked(self, resource_or_lock):
resource = resource_or_lock
else:
raise TypeError(
'Argument should be ether aioredlock.Lock instance or string, '
'%s is given.', type(resource_or_lock)
"Argument should be ether aioredlock.Lock instance or string, "
"%s is given.",
type(resource_or_lock),
)

return await self.redis.is_locked(resource)
Expand All @@ -228,7 +230,7 @@ async def destroy(self):
"""
cancel all _watchdogs, unlock all locks and Clear all the redis connections
"""
self.log.debug('Destroying %s', repr(self))
self.log.debug("Destroying %s", self)

for resource, lock in self._locks.copy().items():
if lock.valid:
Expand Down
1 change: 0 additions & 1 deletion aioredlock/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

@attr.s
class Lock:

lock_manager = attr.ib()
resource = attr.ib()
id = attr.ib()
Expand Down
Loading