Skip to content

Commit 5a8736a

Browse files
EnTeQuAkrpkilby
authored andcommitted
Handle 'None' return value of wait() properly during throttling. (#6837)
1 parent a142467 commit 5a8736a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

rest_framework/views.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,15 @@ def check_throttles(self, request):
356356
throttle_durations.append(throttle.wait())
357357

358358
if throttle_durations:
359-
self.throttled(request, max(throttle_durations))
359+
# Filter out `None` values which may happen in case of config / rate
360+
# changes, see #1438
361+
durations = [
362+
duration for duration in throttle_durations
363+
if duration is not None
364+
]
365+
366+
duration = max(durations, default=None)
367+
self.throttled(request, duration)
360368

361369
def determine_version(self, request, *args, **kwargs):
362370
"""

tests/test_throttling.py

+21
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,27 @@ def test_request_throttling_multiple_throttles(self):
159159
assert response.status_code == 429
160160
assert int(response['retry-after']) == 58
161161

162+
def test_throttle_rate_change_negative(self):
163+
self.set_throttle_timer(MockView_DoubleThrottling, 0)
164+
request = self.factory.get('/')
165+
for dummy in range(24):
166+
response = MockView_DoubleThrottling.as_view()(request)
167+
assert response.status_code == 429
168+
assert int(response['retry-after']) == 60
169+
170+
previous_rate = User3SecRateThrottle.rate
171+
try:
172+
User3SecRateThrottle.rate = '1/sec'
173+
174+
for dummy in range(24):
175+
response = MockView_DoubleThrottling.as_view()(request)
176+
177+
assert response.status_code == 429
178+
assert int(response['retry-after']) == 60
179+
finally:
180+
# reset
181+
User3SecRateThrottle.rate = previous_rate
182+
162183
def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers):
163184
"""
164185
Ensure the response returns an Retry-After field with status and next attributes

0 commit comments

Comments
 (0)