From f74f8b7d8c468e4abe72f2c5aa02e26e844b6906 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 17 Jan 2024 19:31:01 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20delay=5Frun=20?= =?UTF-8?q?=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/authentication/backends/drf.py | 16 +++++++++------- apps/common/decorators.py | 11 ++++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/apps/authentication/backends/drf.py b/apps/authentication/backends/drf.py index 86999037d..29d586e27 100644 --- a/apps/authentication/backends/drf.py +++ b/apps/authentication/backends/drf.py @@ -10,6 +10,7 @@ from rest_framework import authentication, exceptions from common.auth import signature from common.decorators import merge_delay_run from common.utils import get_object_or_none, get_request_ip_or_data, contains_ip +from users.models import User from ..models import AccessKey, PrivateToken @@ -19,20 +20,21 @@ def date_more_than(d, seconds): @merge_delay_run(ttl=60) def update_token_last_used(tokens=()): - for token in tokens: - token.date_last_used = timezone.now() - token.save(update_fields=['date_last_used']) + access_keys_ids = [token.id for token in tokens if isinstance(token, AccessKey)] + private_token_keys = [token.key for token in tokens if isinstance(token, PrivateToken)] + if len(access_keys_ids) > 0: + AccessKey.objects.filter(id__in=access_keys_ids).update(date_last_used=timezone.now()) + if len(private_token_keys) > 0: + PrivateToken.objects.filter(key__in=private_token_keys).update(date_last_used=timezone.now()) @merge_delay_run(ttl=60) def update_user_last_used(users=()): - for user in users: - user.date_api_key_last_used = timezone.now() - user.save(update_fields=['date_api_key_last_used']) + User.objects.filter(id__in=users).update(date_api_key_last_used=timezone.now()) def after_authenticate_update_date(user, token=None): - update_user_last_used(users=(user,)) + update_user_last_used(users=(user.id,)) if token: update_token_last_used(tokens=(token,)) diff --git a/apps/common/decorators.py b/apps/common/decorators.py index 7269c9e0d..0034a41df 100644 --- a/apps/common/decorators.py +++ b/apps/common/decorators.py @@ -101,7 +101,10 @@ def run_debouncer_func(cache_key, org, ttl, func, *args, **kwargs): first_run_time = current if current - first_run_time > ttl: + _loop_debouncer_func_args_cache.pop(cache_key, None) + _loop_debouncer_func_task_time_cache.pop(cache_key, None) executor.submit(run_func_partial, *args, **kwargs) + logger.debug('executor submit run {}'.format(func.__name__,)) return loop = _loop_thread.get_loop() @@ -136,10 +139,12 @@ class Debouncer(object): def _run_func_with_org(key, org, func, *args, **kwargs): from orgs.utils import set_current_org try: - set_current_org(org) - func(*args, **kwargs) + with transaction.atomic(): + set_current_org(org) + func(*args, **kwargs) except Exception as e: - logger.error('delay run error: %s' % e) + logger.error('thread {} delay run {} error: {}'.format( + threading.current_thread(), func.__name__, e)) _loop_debouncer_func_task_cache.pop(key, None) _loop_debouncer_func_args_cache.pop(key, None) _loop_debouncer_func_task_time_cache.pop(key, None)