All Projects → joanvila → Aioredlock

joanvila / Aioredlock

Licence: mit
🔒 The asyncio implemetation of Redis distributed locks

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Aioredlock

Redlock Php
Redis distributed locks in PHP
Stars: ✭ 651 (+280.7%)
Mutual labels:  lock, redis
Pytask Io
Python Async Task Queue
Stars: ✭ 81 (-52.63%)
Mutual labels:  asyncio, redis
Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (+306.43%)
Mutual labels:  asyncio, redis
Ring
Python cache interface with clean API and built-in memcache & redis + asyncio support.
Stars: ✭ 404 (+136.26%)
Mutual labels:  asyncio, redis
Faust
Python Stream Processing. A Faust fork
Stars: ✭ 124 (-27.49%)
Mutual labels:  asyncio, redis
Aiocache
Asyncio cache manager for redis, memcached and memory
Stars: ✭ 496 (+190.06%)
Mutual labels:  asyncio, redis
Socketshark
A WebSocket message router based on Python/Redis/asyncio
Stars: ✭ 51 (-70.18%)
Mutual labels:  asyncio, redis
Lock
高性能分布式并发锁, 行为限流
Stars: ✭ 260 (+52.05%)
Mutual labels:  lock, redis
Live Mutex
High-performance networked mutex for Node.js libraries.
Stars: ✭ 118 (-30.99%)
Mutual labels:  lock, redis
Fastapi Plugins
FastAPI framework plugins
Stars: ✭ 104 (-39.18%)
Mutual labels:  asyncio, redis
Redlock Rb
Redlock is a redis-based distributed lock implementation in Ruby
Stars: ✭ 385 (+125.15%)
Mutual labels:  lock, redis
Easy
开源的Java开发脚手架,工作经验总结,springboot,springcloud,基于tk-mybatis代码反向生成,基于redis(redisson)注解形式加分布式锁等,计划将用该脚手架抄袭jeesite和ruoyi还有基于vue的后台权限管理系统做一套开源的后台管理和cms系统,域名服务器已买好,脚手架还在继续更新中,更新完毕开始更新easysite
Stars: ✭ 160 (-6.43%)
Mutual labels:  lock, redis
Redislock
Simplified distributed locking implementation using Redis
Stars: ✭ 370 (+116.37%)
Mutual labels:  lock, redis
Spring Boot Klock Starter
基于redis的分布式锁组件,简单方便快捷接入项目,使项目拥有分布式锁能力
Stars: ✭ 546 (+219.3%)
Mutual labels:  lock, redis
Redisson
Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Publish / Subscribe, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, MyBatis, RPC, local cache ...
Stars: ✭ 17,972 (+10409.94%)
Mutual labels:  lock, redis
Fennel
A task queue library for Python and Redis
Stars: ✭ 24 (-85.96%)
Mutual labels:  asyncio, redis
Javainterview
java中高级基础指南
Stars: ✭ 222 (+29.82%)
Mutual labels:  lock, redis
aiorwlock
Read/Write Lock - synchronization primitive for asyncio
Stars: ✭ 90 (-47.37%)
Mutual labels:  lock, asyncio
Foundatio
Pluggable foundation blocks for building distributed apps.
Stars: ✭ 1,365 (+698.25%)
Mutual labels:  lock, redis
Aioredis Py
asyncio (PEP 3156) Redis support
Stars: ✭ 2,003 (+1071.35%)
Mutual labels:  asyncio, redis

aioredlock

.. image:: https://github.com/joanvila/aioredlock/workflows/Tests/badge.svg :target: https://travis-ci.org/joanvila/aioredlock

.. image:: https://codecov.io/gh/joanvila/aioredlock/branch/master/graph/badge.svg :target: https://codecov.io/gh/joanvila/aioredlock

.. image:: https://badge.fury.io/py/aioredlock.svg :target: https://pypi.python.org/pypi/aioredlock

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

.. code-block:: python

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://:[email protected]: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

.. _redlock: https://redis.io/topics/distlock .. _Redis: https://redis.io .. _asyncio: https://docs.python.org/3/library/asyncio.html

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].