Better throttle rate corner case handling

This commit is contained in:
mizvyt 2019-10-01 13:12:56 +08:00
parent dd7adc7ed7
commit c297600fb9
2 changed files with 7 additions and 7 deletions

View File

@ -106,15 +106,15 @@ class SimpleRateThrottle(BaseThrottle):
try: try:
num, period = rate.split('/') num, period = rate.split('/')
num_requests = int(num) num_requests = int(num)
# Get rate multiplier value if available period_timescale = re.findall(r'^\d*[s|m|h|d]', period)[0]
period_mult, _ = re.split('[s|m|h|d]', period, maxsplit=1) period_char = period_timescale[-1]
period_char = re.findall('[s|m|h|d]', period)[0] except (ValueError, IndexError):
except ValueError:
msg = "Incorrect throttle rate set for '%s' scope" % self.scope msg = "Incorrect throttle rate set for '%s' scope" % self.scope
raise ImproperlyConfigured(msg) raise ImproperlyConfigured(msg)
try: try:
period_mult = int(period_mult) # Get rate multiplier value if available
period_mult = int(period_timescale[:-1])
except ValueError: except ValueError:
period_mult = 1 period_mult = 1

View File

@ -474,11 +474,11 @@ class SimpleRateThrottleTests(TestCase):
with pytest.raises(ImproperlyConfigured): with pytest.raises(ImproperlyConfigured):
SimpleRateThrottle() SimpleRateThrottle()
SimpleRateThrottle.rate = '100/century' SimpleRateThrottle.rate = '100/foos'
with pytest.raises(ImproperlyConfigured): with pytest.raises(ImproperlyConfigured):
SimpleRateThrottle() SimpleRateThrottle()
SimpleRateThrottle.rate = '100/10century' SimpleRateThrottle.rate = '100/10foos'
with pytest.raises(ImproperlyConfigured): with pytest.raises(ImproperlyConfigured):
SimpleRateThrottle() SimpleRateThrottle()