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.
This commit is contained in:
Steve Howell 2016-07-09 11:25:31 -07:00 committed by Tim Abbott
parent 801bcdd956
commit 8e528569a7
1 changed files with 9 additions and 4 deletions

View File

@ -546,6 +546,12 @@ def rate_limit(domain='all'):
def wrapped_func(request, *args, **kwargs): def wrapped_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse # 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): if client_is_exempt_from_rate_limiting(request):
return func(request, *args, **kwargs) return func(request, *args, **kwargs)
@ -556,7 +562,6 @@ def rate_limit(domain='all'):
# doing the right thing here. # doing the right thing here.
user = None user = None
if not settings.RATE_LIMITING or not user:
if not user: if not user:
logging.error("Requested rate-limiting on %s but user is not authenticated!" % \ logging.error("Requested rate-limiting on %s but user is not authenticated!" % \
func.__name__) func.__name__)