rate_limiter: Fix too early return if no rules are passed in.

In the redis implementation, if rules was an empty list,
this would return too early - before checking if the key isn't
manually blocked.
This commit is contained in:
Mateusz Mandera 2020-04-02 18:51:36 +02:00 committed by Tim Abbott
parent cb85763c78
commit 95fa8b2a26
1 changed files with 3 additions and 3 deletions

View File

@ -222,9 +222,6 @@ class RedisRateLimiterBackend(RateLimiterBackend):
"Returns a tuple of (rate_limited, time_till_free)" "Returns a tuple of (rate_limited, time_till_free)"
list_key, set_key, blocking_key = cls.get_keys(entity_key) list_key, set_key, blocking_key = cls.get_keys(entity_key)
if len(rules) == 0:
return False, 0.0
# Go through the rules from shortest to longest, # Go through the rules from shortest to longest,
# seeing if this user has violated any of them. First # seeing if this user has violated any of them. First
# get the timestamps for each nth items # get the timestamps for each nth items
@ -250,6 +247,9 @@ class RedisRateLimiterBackend(RateLimiterBackend):
blocking_ttl = int(blocking_ttl_b) blocking_ttl = int(blocking_ttl_b)
return True, blocking_ttl return True, blocking_ttl
if len(rules) == 0:
return False, 0.0
now = time.time() now = time.time()
for timestamp, (range_seconds, num_requests) in zip(rule_timestamps, rules): for timestamp, (range_seconds, num_requests) in zip(rule_timestamps, rules):
# Check if the nth timestamp is newer than the associated rule. If so, # Check if the nth timestamp is newer than the associated rule. If so,