fix: redis lock bug (#7340)

Co-authored-by: feng626 <1304903146@qq.com>
Co-authored-by: feng626 <57284900+feng626@users.noreply.github.com>
pull/8019/head
fit2bot 2021-12-08 17:55:08 +08:00 committed by GitHub
parent 06caf56f11
commit be7c8ca558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -1,7 +1,10 @@
from functools import wraps
import threading
from redis_lock import Lock as RedisLock, NotAcquired
from redis_lock import (
Lock as RedisLock, NotAcquired, UNLOCK_SCRIPT,
EXTEND_SCRIPT, RESET_SCRIPT, RESET_ALL_SCRIPT
)
from redis import Redis
from django.db import transaction
@ -50,6 +53,7 @@ class DistributedLock(RedisLock):
auto_renewal = False
super().__init__(redis_client=redis, name='{'+name+'}', expire=expire, auto_renewal=auto_renewal)
self.register_scripts(redis)
self._release_on_transaction_commit = release_on_transaction_commit
self._release_raise_exc = release_raise_exc
self._reentrant = reentrant
@ -73,6 +77,13 @@ class DistributedLock(RedisLock):
return func(*args, **kwds)
return inner
@classmethod
def register_scripts(cls, redis_client):
cls.unlock_script = redis_client.register_script(UNLOCK_SCRIPT)
cls.extend_script = redis_client.register_script(EXTEND_SCRIPT)
cls.reset_script = redis_client.register_script(RESET_SCRIPT)
cls.reset_all_script = redis_client.register_script(RESET_ALL_SCRIPT)
def locked_by_me(self):
if self.locked():
if self.get_owner_id() == self.id: