python-redilock
Advanced tools
+2
-2
| Metadata-Version: 2.1 | ||
| Name: python-redilock | ||
| Version: 0.0.11 | ||
| Version: 0.1.1 | ||
| Summary: Redis Distributed Lock | ||
@@ -83,3 +83,3 @@ Author-email: Zvika Ferentz <zvika.ferentz@gmail.com> | ||
| unlock_secret_token = lock.lock("my_lock") # Acquire the lock | ||
| lock.unlock("my_lock", unlock_secret_token) # Release the lock | ||
| lock.unlock(unlock_secret_token) # Release the lock | ||
| ``` | ||
@@ -86,0 +86,0 @@ |
| Metadata-Version: 2.1 | ||
| Name: python-redilock | ||
| Version: 0.0.11 | ||
| Version: 0.1.1 | ||
| Summary: Redis Distributed Lock | ||
@@ -83,3 +83,3 @@ Author-email: Zvika Ferentz <zvika.ferentz@gmail.com> | ||
| unlock_secret_token = lock.lock("my_lock") # Acquire the lock | ||
| lock.unlock("my_lock", unlock_secret_token) # Release the lock | ||
| lock.unlock(unlock_secret_token) # Release the lock | ||
| ``` | ||
@@ -86,0 +86,0 @@ |
+1
-1
@@ -66,3 +66,3 @@ # redilock : Redis Distributed Lock | ||
| unlock_secret_token = lock.lock("my_lock") # Acquire the lock | ||
| lock.unlock("my_lock", unlock_secret_token) # Release the lock | ||
| lock.unlock(unlock_secret_token) # Release the lock | ||
| ``` | ||
@@ -69,0 +69,0 @@ |
| """Init for redilock""" | ||
| __version__ = "0.0.11" | ||
| __version__ = "0.1.1" | ||
| __author__ = "Zvika Ferentz" | ||
| __author_email__ = "Zvika.Ferentz@gmail.com" | ||
| __url__ = "https://github.com/zferentz/python-redilock" |
@@ -18,7 +18,9 @@ """Async Distributed Lock (using redis)""" | ||
| try: | ||
| result = await _redis.evalsha(self._script_sha1, len(keys), *keys, *args) | ||
| result = await _redis.evalsha(self._script_sha1, len(keys), *keys, | ||
| *args) | ||
| except redis.exceptions.NoScriptError: | ||
| await _redis.script_load(self._script) | ||
| result = await _redis.evalsha(self._script_sha1, len(keys), *keys, *args) | ||
| result = await _redis.evalsha(self._script_sha1, len(keys), *keys, | ||
| *args) | ||
@@ -49,7 +51,7 @@ return result | ||
| async def lock( | ||
| self, | ||
| lock_name: str, | ||
| ttl: float = None, | ||
| block: bool | float | int = True, | ||
| interval: float | int = None, | ||
| self, | ||
| lock_name: str, | ||
| ttl: float = None, | ||
| block: bool | float | int = True, | ||
| interval: float | int = None, | ||
| ): | ||
@@ -93,3 +95,7 @@ """Lock a resource (by lock name). Wait until lock is owned. | ||
| async def unlock(self, lock_name: str, unlock_secret_token: str) -> bool: | ||
| async def unlock(self, unlock_secret_token: str) -> bool: | ||
| lock_name = self._token2lockname(unlock_secret_token) | ||
| if not lock_name: | ||
| return False | ||
| if not self._redis: | ||
@@ -113,2 +119,2 @@ await self._connect() | ||
| finally: | ||
| await self.unlock(lock_name, unlock_secret_token) | ||
| await self.unlock(unlock_secret_token) |
+20
-3
@@ -105,4 +105,21 @@ """base classes & utilities for redilock""" | ||
| return f"_LOCK_{lock_name}_{uuid.uuid4().hex}", ttl, end_wait, interval | ||
| unlock_secret_token = self._lockname2token(lock_name) | ||
| return unlock_secret_token, ttl, end_wait, interval | ||
| @classmethod | ||
| def _lockname2token(cls, lock_name: str) -> str: | ||
| return f"_LOCK:{lock_name}:{uuid.uuid4().hex}" | ||
| @classmethod | ||
| def _token2lockname(cls, unlock_secret_token: str) -> str: | ||
| if not isinstance(unlock_secret_token, str): | ||
| return "" | ||
| delim = unlock_secret_token.split(":") | ||
| if len(delim) != 3 or delim[0] != "_LOCK": | ||
| return "" | ||
| return delim[1] | ||
| @abc.abstractmethod | ||
@@ -141,3 +158,3 @@ async def lock( | ||
| @abc.abstractmethod | ||
| async def unlock(self, lock_name: str, unlock_secret_token: str) -> bool: | ||
| async def unlock(self, unlock_secret_token: str) -> bool: | ||
| """Unlock a resource | ||
@@ -152,3 +169,3 @@ | ||
| @abc.abstractmethod | ||
| def unlock(self, lock_name: str, unlock_secret_token: str) -> bool: | ||
| def unlock(self, unlock_secret_token: str) -> bool: | ||
| """Refer to docstring for async unlock() above""" |
@@ -42,7 +42,7 @@ """Distributed Lock (using redis)""" | ||
| def lock( | ||
| self, | ||
| lock_name: str, | ||
| ttl: float = None, | ||
| block: bool | float | int = True, | ||
| interval: float | int = None, | ||
| self, | ||
| lock_name: str, | ||
| ttl: float = None, | ||
| block: bool | float | int = True, | ||
| interval: float | int = None, | ||
| ): | ||
@@ -86,3 +86,7 @@ """Lock a resource (by lock name). Wait until lock is owned. | ||
| def unlock(self, lock_name: str, unlock_secret_token: str) -> bool: | ||
| def unlock(self, unlock_secret_token: str) -> bool: | ||
| lock_name = self._token2lockname(unlock_secret_token) | ||
| if not lock_name: | ||
| return False | ||
| if not self._redis: | ||
@@ -106,2 +110,2 @@ self._connect() | ||
| finally: | ||
| self.unlock(lock_name, unlock_secret_token) | ||
| self.unlock(unlock_secret_token) |
@@ -100,5 +100,5 @@ import asyncio | ||
| lock._unlock_script = unittest.mock.AsyncMock() | ||
| await lock.unlock("mylock", "my_secret_token") | ||
| await lock.unlock("_LOCK:mylock:AA-BBB-CCCC-DDDD") | ||
| lock._unlock_script.run.assert_called_once_with( | ||
| lock._redis, ("mylock",), ["my_secret_token"] | ||
| lock._redis, ("mylock",), ["_LOCK:mylock:AA-BBB-CCCC-DDDD"] | ||
| ) | ||
@@ -114,2 +114,2 @@ | ||
| mock_unlock.assert_not_called() | ||
| mock_unlock.assert_called_once_with("myresource", unittest.mock.ANY) | ||
| mock_unlock.assert_called_once_with(unittest.mock.ANY) |
+12
-1
@@ -114,2 +114,13 @@ import hashlib | ||
| @unittest.mock.patch.object(uuid, "uuid4") | ||
| def test_token_lock_name(self, mock_uuid4): | ||
| mock_uuid4.return_value.hex = "AAAA-BBBB-CCCC-DDDD" | ||
| token = base.DistributedLockBase._lockname2token("TEST1234") | ||
| self.assertEqual(token, "_LOCK:TEST1234:AAAA-BBBB-CCCC-DDDD") | ||
| lock_name = base.DistributedLockBase._token2lockname(token) | ||
| self.assertEqual(lock_name, "TEST1234") | ||
| @unittest.mock.patch.object(uuid, "uuid4") | ||
| def test_prepare_lock(self, mock_uuid4): | ||
@@ -130,3 +141,3 @@ | ||
| self.assertEqual(end_wait, None) | ||
| self.assertEqual(unlock_secret_token, "_LOCK_mylock_AAAA-BBBB-CCCC-DDDD") | ||
| self.assertEqual(unlock_secret_token, "_LOCK:mylock:AAAA-BBBB-CCCC-DDDD") | ||
| self.assertEqual(ttl, 14) | ||
@@ -133,0 +144,0 @@ self.assertEqual(interval, 0.66) |
@@ -90,5 +90,5 @@ import unittest.mock | ||
| lock._unlock_script = unittest.mock.MagicMock() | ||
| lock.unlock("mylock", "my_secret_token") | ||
| lock.unlock("_LOCK:mylock:AA-BB-CC-DD") | ||
| lock._unlock_script.run.assert_called_once_with( | ||
| lock._redis, ("mylock",), ["my_secret_token"] | ||
| lock._redis, ("mylock",), ["_LOCK:mylock:AA-BB-CC-DD"] | ||
| ) | ||
@@ -105,2 +105,2 @@ | ||
| mock_unlock.assert_not_called() | ||
| mock_unlock.assert_called_once_with("myresource", unittest.mock.ANY) | ||
| mock_unlock.assert_called_once_with(unittest.mock.ANY) |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
45892
2.55%614
4.6%