ionelmc / python-redis-lock

Lock context manager implemented via redis SET NX EX and BLPOP.
https://pypi.python.org/pypi/python-redis-lock
BSD 2-Clause "Simplified" License
550 stars 78 forks source link

Customizable prefix #101

Open Mogost opened 1 year ago

Mogost commented 1 year ago

Hi. Faced with something similar to #10

I have shared Redis cache, and control access with ACL. To separate instances I use prefixes but python-redis-lock overwrites my prefix with lock: https://github.com/ionelmc/python-redis-lock/blob/149701266b4d147d6321875f19f604e9bacf4f4f/src/redis_lock/__init__.py#L162 So I get an access error:

NoPermissionError
this user has no permissions to access one of the keys used as arguments

Could we have a customizable prefix for this case?

ionelmc commented 1 year ago

You could subclass and do something like:

class PrefixedLock(Lock):
     def __init__(self, redis_client, prefix, name, **kwargs):
        super().__init__(redis_client, name, **kwargs)
        self._name = f'{prefix}:lock:{name}'
        self._signal = f'{prefix}:lock-signal:{name}' 
Mogost commented 1 year ago

Yep. But it looks like a dirty solution. For example in this case I also need to fix this part https://github.com/ionelmc/python-redis-lock/blob/149701266b4d147d6321875f19f604e9bacf4f4f/src/redis_lock/__init__.py#L52 And it all looks like I want to hack python-redis-lock

ionelmc commented 1 year ago

Perhaps a key template argument could be added through all the clases/functions. Eg:

Lock(key_template={'lock': 'lock:{}', 'signal': 'lock-signal:{}'})
reset_all(key_template={'lock': 'lock:{}', 'signal': 'lock-signal:{}'})

But I do want to switch to redis functions (redis>=7) for the next release if I get to do one, so there's that...