Beispiel #1
0
    async def extend(self, additional_time):
        """
        Adds more time to an already acquired lock.

        ``additional_time`` can be specified as an integer or a float, both
        representing the number of seconds to add.
        """
        if self.local.token is None:
            raise LockError("Cannot extend an unlocked lock")
        if self.timeout is None:
            raise LockError("Cannot extend a lock with no timeout")
        return await self.do_extend(additional_time)
Beispiel #2
0
 async def do_extend(self, additional_time):
     additional_time = int(additional_time * 1000)
     if not bool(await self.lua_extend.execute(keys=[self.name],
                                               args=[self.local.token, additional_time],
                                               client=self.redis)):
         raise LockError("Cannot extend a lock that's no longer owned")
     return True
Beispiel #3
0
 async def release(self):
     "Releases the already acquired lock"
     expected_token = self.local.token
     if expected_token is None:
         raise LockError("Cannot release an unlocked lock")
     self.local.token = None
     await self.do_release(expected_token)
Beispiel #4
0
    async def do_extend(self, additional_time):
        pipe = await self.redis.pipeline()
        await pipe.watch(self.name)
        lock_value = await pipe.get(self.name)
        if lock_value != self.local.token:
            raise LockError("Cannot extend a lock that's no longer owned")
        expiration = await pipe.pttl(self.name)
        if expiration is None or expiration < 0:
            # Redis evicted the lock key between the previous get() and now
            # we'll handle this when we call pexpire()
            expiration = 0
        pipe.multi()
        await pipe.pexpire(self.name, expiration + int(additional_time * 1000))

        try:
            response = await pipe.execute()
        except WatchError:
            # someone else acquired the lock
            raise LockError("Cannot extend a lock that's no longer owned")
        if not response[0]:
            # pexpire returns False if the key doesn't exist
            raise LockError("Cannot extend a lock that's no longer owned")
        return True
Beispiel #5
0
 async def do_release(self, expected_token):
     await super(ClusterLock, self).do_release(expected_token)
     if await self.check_lock_in_slaves(expected_token):
         raise LockError('Lock is released in master but not in slave yet')
Beispiel #6
0
 def __init__(self, *args, **kwargs):
     super(ClusterLock, self).__init__(*args, **kwargs)
     if not self.timeout:
         raise LockError('timeout must be provided for cluster lock')
Beispiel #7
0
 async def do_release(self, expected_token):
     if not bool(await self.lua_release.execute(keys=[self.name],
                                                args=[expected_token],
                                                client=self.redis)):
         raise LockError("Cannot release a lock that's no longer owned")
Beispiel #8
0
    def __init__(self, redis, name, timeout=None, sleep=0.1,
                 blocking=True, blocking_timeout=None, thread_local=True):
        """
        Create a new Lock instance named ``name`` using the Redis client
        supplied by ``redis``.

        ``timeout`` indicates a maximum life for the lock.
        By default, it will remain locked until release() is called.
        ``timeout`` can be specified as a float or integer, both representing
        the number of seconds to wait.

        ``sleep`` indicates the amount of time to sleep per loop iteration
        when the lock is in blocking mode and another client is currently
        holding the lock.

        ``blocking`` indicates whether calling ``acquire`` should block until
        the lock has been acquired or to fail immediately, causing ``acquire``
        to return False and the lock not being acquired. Defaults to True.
        Note this value can be overridden by passing a ``blocking``
        argument to ``acquire``.

        ``blocking_timeout`` indicates the maximum amount of time in seconds to
        spend trying to acquire the lock. A value of ``None`` indicates
        continue trying forever. ``blocking_timeout`` can be specified as a
        float or integer, both representing the number of seconds to wait.

        ``thread_local`` indicates whether the lock token is placed in
        thread-local storage. By default, the token is placed in thread local
        storage so that a thread only sees its token, not a token set by
        another thread. Consider the following timeline:

            time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
                     thread-1 sets the token to "abc"
            time: 1, thread-2 blocks trying to acquire `my-lock` using the
                     Lock instance.
            time: 5, thread-1 has not yet completed. redis expires the lock
                     key.
            time: 5, thread-2 acquired `my-lock` now that it's available.
                     thread-2 sets the token to "xyz"
            time: 6, thread-1 finishes its work and calls release(). if the
                     token is *not* stored in thread local storage, then
                     thread-1 would see the token value as "xyz" and would be
                     able to successfully release the thread-2's lock.

        In some use cases it's necessary to disable thread local storage. For
        example, if you have code where one thread acquires a lock and passes
        that lock instance to a worker thread to release later. If thread
        local storage isn't disabled in this case, the worker thread won't see
        the token set by the thread that acquired the lock. Our assumption
        is that these cases aren't common and as such default to using
        thread local storage.
        """
        self.redis = redis
        self.name = name
        self.timeout = timeout
        self.sleep = sleep
        self.blocking = blocking
        self.blocking_timeout = blocking_timeout
        self.thread_local = bool(thread_local)
        self.local = threading.local() if self.thread_local else dummy()
        self.local.token = None
        if self.timeout and self.sleep > self.timeout:
            raise LockError("'sleep' must be less than 'timeout'")
Beispiel #9
0
 async def execute_release(pipe):
     lock_value = await pipe.get(name)
     if lock_value != expected_token:
         raise LockError("Cannot release a lock that's no longer owned")
     await pipe.delete(name)