fix(asset_user): 修改创建AuthBook对象锁机制,使用select_for_update替换redis_lock (#4351)

* fix(authbook): 修改创建AuthBook对象锁机制,解决并发操作堵塞问题

* fix(asset_user): 修改创建AuthBook对象锁机制,使用select_for_update替换redis_lock

* fix(asset_user): 修改创建AuthBook对象锁机制,使用select_for_update替换redis_lock2

* fix(asset_user): 修改创建AuthBook对象锁机制,使用select_for_update替换redis_lock3
pull/4396/head
BaiJiangJie 2020-07-17 15:26:12 +08:00 committed by GitHub
parent 1ae2b84dd7
commit d1aed7c9ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 14 deletions

View File

@ -3,7 +3,6 @@
from django.db import models, transaction
from django.db.models import Max
from django.core.cache import cache
from django.utils.translation import ugettext_lazy as _
from orgs.mixins.models import OrgManager
@ -59,19 +58,17 @@ class AuthBook(BaseUser):
"""
username = kwargs['username']
asset = kwargs['asset']
key_lock = 'KEY_LOCK_CREATE_AUTH_BOOK_{}_{}'.format(username, asset.id)
with cache.lock(key_lock):
with transaction.atomic():
cls.objects.filter(
username=username, asset=asset, is_latest=True
).update(is_latest=False)
max_version = cls.get_max_version(username, asset)
kwargs.update({
'version': max_version + 1,
'is_latest': True
})
obj = cls.objects.create(**kwargs)
return obj
with transaction.atomic():
# 使用select_for_update限制并发创建相同的username、asset条目
instances = cls.objects.select_for_update().filter(username=username, asset=asset)
instances.filter(is_latest=True).update(is_latest=False)
max_version = cls.get_max_version(username, asset)
kwargs.update({
'version': max_version + 1,
'is_latest': True
})
obj = cls.objects.create(**kwargs)
return obj
@property
def connectivity(self):