Skip to main content

Asyncio implemetation of Redis distributed locks

Project description

https://github.com/joanvila/aioredlock/workflows/Tests/badge.svg https://codecov.io/gh/joanvila/aioredlock/branch/master/graph/badge.svg https://badge.fury.io/py/aioredlock.svg

The asyncio redlock algorithm implementation.

Redlock and asyncio

The redlock algorithm is a distributed lock implementation for Redis. There are many implementations of it in several languages. In this case, this is the asyncio compatible implementation for python 3.5+.

Usage

from aioredlock import Aioredlock, LockError, Sentinel

# Define a list of connections to your Redis instances:
redis_instances = [
  ('localhost', 6379),
  {'host': 'localhost', 'port': 6379, 'db': 1},
  'redis://localhost:6379/2',
  Sentinel(('localhost', 26379), master='leader', db=3),
  Sentinel('redis://localhost:26379/4?master=leader&encoding=utf-8'),
  Sentinel('rediss://:password@localhost:26379/5?master=leader&encoding=utf-8&ssl_cert_reqs=CERT_NONE'),
]

# Create a lock manager:
lock_manager = Aioredlock(redis_instances)

# Check wether a resourece acquired by any other redlock instance:
assert not await lock_manager.is_locked("resource_name")

# Try to acquire the lock:
try:
    lock = await lock_manager.lock("resource_name", lock_timeout=10)
except LockError:
    print('Lock not acquired')
    raise

# Now the lock is acquired:
assert lock.valid
assert await lock_manager.is_locked("resource_name")

# Extend lifetime of the lock:
await lock_manager.extend(lock, lock_timeout=10)
# Raises LockError if the lock manager can not extend the lock lifetime
# on more then half of the Redis instances.

# Release the lock:
await lock_manager.unlock(lock)
# Raises LockError if the lock manager can not release the lock
# on more then half of redis instances.

# The released lock become invalid:
assert not lock.valid
assert not await lock_manager.is_locked("resource_name")

# Or you can use the lock as async context manager:
try:
    async with await lock_manager.lock("resource_name") as lock:
        assert lock.valid is True
        # Do your stuff having the lock
        await lock.extend()  # alias for lock_manager.extend(lock)
        # Do more stuff having the lock
    assert lock.valid is False # lock will be released by context manager
except LockError:
    print('Lock not acquired')
    raise

# Clear the connections with Redis:
await lock_manager.destroy()

How it works

The Aioredlock constructor accepts the following optional parameters:

  • redis_connections: A list of connections (dictionary of host and port and kwargs for aioredis.create_redis_pool(), or tuple (host, port), or string Redis URI) where the Redis instances are running. The default value is [{'host': 'localhost', 'port': 6379}].

  • retry_count: An integer representing number of maximum allowed retries to acquire the lock. The default value is 3 times.

  • retry_delay_min and retry_delay_max: Float values representing waiting time (in seconds) before the next retry attempt. The default values are 0.1 and 0.3, respectively.

In order to acquire the lock, the lock function should be called. If the lock operation is successful, lock.valid will be true, if the lock is not acquired then the LockError will be raised.

From that moment, the lock is valid until the unlock function is called or when the lock_timeout is reached.

Call the extend function to reset lifetime of the lock to lock_timeout interval.

Use the is_locked function to check if the resource is locked by other redlock instance.

In order to clear all the connections with Redis, the lock_manager destroy method can be called.

To-do

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioredlock-0.7.3.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

aioredlock-0.7.3-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file aioredlock-0.7.3.tar.gz.

File metadata

  • Download URL: aioredlock-0.7.3.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.10

File hashes

Hashes for aioredlock-0.7.3.tar.gz
Algorithm Hash digest
SHA256 903727b26eb571c926018a8ae2b754c6c11861996410e3c4f1309872d2545440
MD5 4061328cf5741711751451283ad073e7
BLAKE2b-256 0e6b1e8ab48dbcfe802d1d07dece32bb5eea02bd494e0e3e5d8e8629c136d9ca

See more details on using hashes here.

File details

Details for the file aioredlock-0.7.3-py3-none-any.whl.

File metadata

  • Download URL: aioredlock-0.7.3-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.10

File hashes

Hashes for aioredlock-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7432fe17cf2ce55292409f4e80d26af5ccbf1a09aa4566e30bcfc5dabd4b3e1f
MD5 0e62f6fde5fd322f9ceba02ec7b7e671
BLAKE2b-256 bd67ac617ecad2cbf12e639e9ed50f2ef956e415a8c0b47e6442017ce25c44b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page