From 99bc3150ca763a45de647b7a2848bb07ca51a2f2 Mon Sep 17 00:00:00 2001 From: Ranjiwei <32759763+r350178982@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:37:06 +0800 Subject: [PATCH] add-zip-task-request-limit (#6825) * add-zip-task-request-limit * update * Update settings.py * Update share_link_zip_task.py * Update test_settings.py --- frontend/src/utils/utils.js | 2 ++ seahub/api2/endpoints/share_link_zip_task.py | 4 ++-- seahub/api2/throttling.py | 17 +++++++++++++++++ seahub/settings.py | 1 + seahub/test_settings.py | 1 + 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index ef63828dc2c..acad5fad6a3 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -1183,6 +1183,8 @@ export const Utils = { ); } errorMsg = gettext('Permission denied'); + } else if (error.response.status == 429) { + errorMsg = gettext('Too many requests'); } else if (error.response.data && error.response.data['error_msg']) { errorMsg = error.response.data['error_msg']; diff --git a/seahub/api2/endpoints/share_link_zip_task.py b/seahub/api2/endpoints/share_link_zip_task.py index 7c167b7ee59..f3aec91eb8a 100644 --- a/seahub/api2/endpoints/share_link_zip_task.py +++ b/seahub/api2/endpoints/share_link_zip_task.py @@ -10,7 +10,7 @@ from django.conf import settings -from seahub.api2.throttling import UserRateThrottle +from seahub.api2.throttling import ShareLinkZipTaskThrottle from seahub.api2.utils import api_error from seahub.views.file import send_file_access_msg @@ -27,7 +27,7 @@ class ShareLinkZipTaskView(APIView): - throttle_classes = (UserRateThrottle,) + throttle_classes = (ShareLinkZipTaskThrottle, ) def get(self, request, format=None): """ Only used for download dir when view dir share link from web. diff --git a/seahub/api2/throttling.py b/seahub/api2/throttling.py index a74b56a8a9f..1b5e0fc8ead 100644 --- a/seahub/api2/throttling.py +++ b/seahub/api2/throttling.py @@ -7,6 +7,7 @@ from django.core.cache import cache as default_cache from django.core.exceptions import ImproperlyConfigured from rest_framework.settings import api_settings +from rest_framework.throttling import BaseThrottle import time from seahub.utils.ip import get_remote_ip @@ -207,6 +208,22 @@ def get_cache_key(self, request, view): 'ident': ident } +class ShareLinkZipTaskThrottle(SimpleRateThrottle): + + scope = 'share_link_zip_task' + + def get_cache_key(self, request, view): + if request.user.is_authenticated: + ident = request.user.id + else: + ident = self.get_ident(request) + + return self.cache_format % { + 'scope': self.scope, + 'ident': ident + } + + class ScopedRateThrottle(SimpleRateThrottle): """ diff --git a/seahub/settings.py b/seahub/settings.py index f6cab4dd86f..765bb3c1883 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -544,6 +544,7 @@ 'ping': '3000/minute', 'anon': '60/minute', 'user': '3000/minute', + 'share_link_zip_task': '10/minute' }, # https://github.com/tomchristie/django-rest-framework/issues/2891 'UNICODE_JSON': False, diff --git a/seahub/test_settings.py b/seahub/test_settings.py index c37bf24deb1..3eee9c76992 100644 --- a/seahub/test_settings.py +++ b/seahub/test_settings.py @@ -13,6 +13,7 @@ 'ping': '90000/minute', 'anon': '90000/minute', 'user': '90000/minute', + 'share_link_zip_task': '90000/minute' }, }