From cb71a6571efe8aabf0e6dba34ac9b5f9bcf5fd9d Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Sat, 3 Aug 2019 20:39:49 +0200 Subject: [PATCH] rate_limiter: Rename 'all' domain to 'api_by_user'. --- zerver/decorator.py | 4 ++-- zerver/lib/rate_limiter.py | 10 +++++----- zerver/management/commands/rate_limit.py | 4 ++-- zproject/settings.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/zerver/decorator.py b/zerver/decorator.py index 0137ddacc7..864aca0ffa 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -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. diff --git a/zerver/lib/rate_limiter.py b/zerver/lib/rate_limiter.py index f908fa86f9..52c64e0609 100644 --- a/zerver/lib/rate_limiter.py +++ b/zerver/lib/rate_limiter.py @@ -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] diff --git a/zerver/management/commands/rate_limit.py b/zerver/management/commands/rate_limit.py index 0a5b986b5b..ed00cea3a0 100644 --- a/zerver/management/commands/rate_limit.py +++ b/zerver/management/commands/rate_limit.py @@ -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', diff --git a/zproject/settings.py b/zproject/settings.py index af33a06bb1..9a2bb681e5 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -354,7 +354,7 @@ CACHES = { ######################################################################## RATE_LIMITING_RULES = { - 'all': [ + 'api_by_user': [ (60, 200), # 200 requests max every minute ], 'authenticate': [