mirror of https://github.com/zulip/zulip.git
rate_limiter: Rename 'all' domain to 'api_by_user'.
This commit is contained in:
parent
06198af5b9
commit
cb71a6571e
|
@ -351,7 +351,7 @@ def api_key_only_webhook_view(
|
|||
client_name=full_webhook_client_name(webhook_client_name))
|
||||
|
||||
if settings.RATE_LIMITING:
|
||||
rate_limit_user(request, user_profile, domain='all')
|
||||
rate_limit_user(request, user_profile, domain='api_by_user')
|
||||
try:
|
||||
return view_func(request, user_profile, *args, **kwargs)
|
||||
except Exception as err:
|
||||
|
@ -777,7 +777,7 @@ def rate_limit_user(request: HttpRequest, user: UserProfile, domain: str) -> Non
|
|||
entity = RateLimitedUser(user, domain=domain)
|
||||
rate_limit_request_by_entity(request, entity)
|
||||
|
||||
def rate_limit(domain: str='all') -> Callable[[ViewFuncT], ViewFuncT]:
|
||||
def rate_limit(domain: str='api_by_user') -> Callable[[ViewFuncT], ViewFuncT]:
|
||||
"""Rate-limits a view. Takes an optional 'domain' param if you wish to
|
||||
rate limit different types of API calls independently.
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class RateLimitedObject:
|
|||
raise NotImplementedError()
|
||||
|
||||
class RateLimitedUser(RateLimitedObject):
|
||||
def __init__(self, user: UserProfile, domain: str='all') -> None:
|
||||
def __init__(self, user: UserProfile, domain: str='api_by_user') -> None:
|
||||
self.user = user
|
||||
self.domain = domain
|
||||
|
||||
|
@ -54,8 +54,8 @@ class RateLimitedUser(RateLimitedObject):
|
|||
return "{}:{}:{}".format(type(self.user), self.user.id, self.domain)
|
||||
|
||||
def rules(self) -> List[Tuple[int, int]]:
|
||||
# user.rate_limits are general limits, applicable to the domain 'all'
|
||||
if self.user.rate_limits != "" and self.domain == 'all':
|
||||
# user.rate_limits are general limits, applicable to the domain 'api_by_user'
|
||||
if self.user.rate_limits != "" and self.domain == 'api_by_user':
|
||||
result = [] # type: List[Tuple[int, int]]
|
||||
for limit in self.user.rate_limits.split(','):
|
||||
(seconds, requests) = limit.split(':', 2)
|
||||
|
@ -75,7 +75,7 @@ def max_api_window(entity: RateLimitedObject) -> int:
|
|||
"Returns the API time window for the highest limit"
|
||||
return entity.rules()[-1][0]
|
||||
|
||||
def add_ratelimit_rule(range_seconds: int, num_requests: int, domain: str='all') -> None:
|
||||
def add_ratelimit_rule(range_seconds: int, num_requests: int, domain: str='api_by_user') -> None:
|
||||
"Add a rate-limiting rule to the ratelimiter"
|
||||
global rules
|
||||
|
||||
|
@ -87,7 +87,7 @@ def add_ratelimit_rule(range_seconds: int, num_requests: int, domain: str='all')
|
|||
rules[domain].append((range_seconds, num_requests))
|
||||
rules[domain].sort(key=lambda x: x[0])
|
||||
|
||||
def remove_ratelimit_rule(range_seconds: int, num_requests: int, domain: str='all') -> None:
|
||||
def remove_ratelimit_rule(range_seconds: int, num_requests: int, domain: str='api_by_user') -> None:
|
||||
global rules
|
||||
rules[domain] = [x for x in rules[domain] if x[0] != range_seconds and x[1] != num_requests]
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ class Command(ZulipBaseCommand):
|
|||
help="Seconds to block for.")
|
||||
parser.add_argument('-d', '--domain',
|
||||
dest='domain',
|
||||
default='all',
|
||||
help="Rate-limiting domain. Defaults to 'all'.")
|
||||
default='api_by_user',
|
||||
help="Rate-limiting domain. Defaults to 'api_by_user'.")
|
||||
parser.add_argument('-b', '--all-bots',
|
||||
dest='bots',
|
||||
action='store_true',
|
||||
|
|
|
@ -354,7 +354,7 @@ CACHES = {
|
|||
########################################################################
|
||||
|
||||
RATE_LIMITING_RULES = {
|
||||
'all': [
|
||||
'api_by_user': [
|
||||
(60, 200), # 200 requests max every minute
|
||||
],
|
||||
'authenticate': [
|
||||
|
|
Loading…
Reference in New Issue