def initial(self, request, *args, **kwargs): self.lock = None if request.instance and request.method not in permissions.SAFE_METHODS: # Make a lock to avoid deadlocks self.lock = redis.lock(self.model.get_instance_lock_key( request.instance), timeout=settings.LOCK_TIMEOUT) self.lock.acquire() return super().initial(request, *args, **kwargs)
def __call__(self, *args, **kwargs): logger = self.get_logger() lock_key = self.get_lock_key(*args, **kwargs) lock_expire = self.get_lock_expire(*args, **kwargs) try: with redis.lock(lock_key, timeout=lock_expire, blocking_timeout=self.lock_blocking_timeout): ret = super().__call__(*args, **kwargs) self.after_lock_released(args, kwargs) return ret except LockError: logger.debug('Already locked.') return False
def lock(cls, instance_pk): return redis.lock(cls.LOCK_KEY_TEMPLATE.format(instance_pk=instance_pk), lock_class=ReentrantLock, timeout=cls.LOCK_TIMEOUT, sleep=0.01)