Skip to content

Commit

Permalink
rate_limit: Remove unnecessary nesting.
Browse files Browse the repository at this point in the history
`rate_limit` does not accept any arguments. It is unnecessary to nest
the actual decorator in it.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
  • Loading branch information
PIG208 committed Jul 28, 2022
1 parent 1890dcc commit b828d66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
55 changes: 26 additions & 29 deletions zerver/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def _wrapped_view_func(
request: HttpRequest, /, *args: ParamT.args, **kwargs: ParamT.kwargs
) -> HttpResponse:
process_client(request, request.user, is_browser_view=True, query=view_func.__name__)
return rate_limit()(view_func)(request, *args, **kwargs)
return rate_limit(view_func)(request, *args, **kwargs)

return _wrapped_view_func

Expand Down Expand Up @@ -716,7 +716,7 @@ def _wrapped_func_arguments(
) -> HttpResponse:
user_profile = validate_api_key(request, None, api_key, False)
if not skip_rate_limiting:
limited_func = rate_limit()(view_func)
limited_func = rate_limit(view_func)
else:
limited_func = view_func
return limited_func(request, user_profile, *args, **kwargs)
Expand Down Expand Up @@ -780,7 +780,7 @@ def _wrapped_func_arguments(
try:
if not skip_rate_limiting:
# Apply rate limiting
target_view_func = rate_limit()(view_func)
target_view_func = rate_limit(view_func)
else:
target_view_func = view_func
return target_view_func(request, profile, *args, **kwargs)
Expand Down Expand Up @@ -857,7 +857,7 @@ def authenticate_log_and_execute_json(
**kwargs: object,
) -> HttpResponse:
if not skip_rate_limiting:
limited_view_func = rate_limit()(view_func)
limited_view_func = rate_limit(view_func)
else:
limited_view_func = view_func

Expand Down Expand Up @@ -1062,39 +1062,36 @@ def rate_limit_remote_server(
raise e


def rate_limit() -> Callable[[ViewFuncT], ViewFuncT]:
"""Rate-limits a view. Returns a decorator"""
def rate_limit(func: ViewFuncT) -> ViewFuncT:
"""Rate-limits a view."""

def wrapper(func: ViewFuncT) -> ViewFuncT:
@wraps(func)
def wrapped_func(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:

# It is really tempting to not even wrap our original function
# when settings.RATE_LIMITING is False, but it would make
# for awkward unit testing in some situations.
if not settings.RATE_LIMITING:
return func(request, *args, **kwargs)
@wraps(func)
def wrapped_func(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:

if client_is_exempt_from_rate_limiting(request):
return func(request, *args, **kwargs)
# It is really tempting to not even wrap our original function
# when settings.RATE_LIMITING is False, but it would make
# for awkward unit testing in some situations.
if not settings.RATE_LIMITING:
return func(request, *args, **kwargs)

user = request.user
remote_server = RequestNotes.get_notes(request).remote_server
if client_is_exempt_from_rate_limiting(request):
return func(request, *args, **kwargs)

if settings.ZILENCER_ENABLED and remote_server is not None:
rate_limit_remote_server(request, remote_server, domain="api_by_remote_server")
elif not user.is_authenticated:
rate_limit_request_by_ip(request, domain="api_by_ip")
return func(request, *args, **kwargs)
else:
assert isinstance(user, UserProfile)
rate_limit_user(request, user, domain="api_by_user")
user = request.user
remote_server = RequestNotes.get_notes(request).remote_server

if settings.ZILENCER_ENABLED and remote_server is not None:
rate_limit_remote_server(request, remote_server, domain="api_by_remote_server")
elif not user.is_authenticated:
rate_limit_request_by_ip(request, domain="api_by_ip")
return func(request, *args, **kwargs)
else:
assert isinstance(user, UserProfile)
rate_limit_user(request, user, domain="api_by_user")

return cast(ViewFuncT, wrapped_func) # https://github.com/python/mypy/issues/1927
return func(request, *args, **kwargs)

return wrapper
return cast(ViewFuncT, wrapped_func) # https://github.com/python/mypy/issues/1927


def return_success_on_head_request(
Expand Down
2 changes: 1 addition & 1 deletion zerver/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def get_ratelimited_view(self) -> Callable[..., HttpResponse]:
def f(req: Any) -> HttpResponse:
return json_response(msg="some value")

f = rate_limit()(f)
f = rate_limit(f)

return f

Expand Down

0 comments on commit b828d66

Please sign in to comment.