From 8e528569a727e6b74b8b5a5f5d34bc3ac8f2e705 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 9 Jul 2016 11:25:31 -0700 Subject: [PATCH] Clean up rate_limit() for deployments that opt out. If settings.RATE_LIMITING is False, short circuit rate limiting earlier in rate_limit(). This change particularly avoids inspect request.user and possibly spamming the error log for sites that don't care about rate limiting. --- zerver/decorator.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/zerver/decorator.py b/zerver/decorator.py index 3384ebbc94..935180a5e5 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -546,6 +546,12 @@ def rate_limit(domain='all'): def wrapped_func(request, *args, **kwargs): # type: (HttpRequest, *Any, **Any) -> 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) + if client_is_exempt_from_rate_limiting(request): return func(request, *args, **kwargs) @@ -556,10 +562,9 @@ def rate_limit(domain='all'): # doing the right thing here. user = None - if not settings.RATE_LIMITING or not user: - if not user: - logging.error("Requested rate-limiting on %s but user is not authenticated!" % \ - func.__name__) + if not user: + logging.error("Requested rate-limiting on %s but user is not authenticated!" % \ + func.__name__) return func(request, *args, **kwargs) # Rate-limiting data is stored in redis