Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 'None' return value of wait() properly during throttling. #6837

Merged
merged 4 commits into from Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion rest_framework/views.py
Expand Up @@ -356,7 +356,17 @@ def check_throttles(self, request):
throttle_durations.append(throttle.wait())

if throttle_durations:
self.throttled(request, max(throttle_durations))
# Filter out `None` values which may happen in case of config / rate
# changes, see #1438
durations = [
duration for duration in throttle_durations
if duration is not None
]

if durations:
self.throttled(request, max(durations))
else:
self.throttled(request, None)
rpkilby marked this conversation as resolved.
Show resolved Hide resolved

def determine_version(self, request, *args, **kwargs):
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/test_throttling.py
Expand Up @@ -159,6 +159,26 @@ def test_request_throttling_multiple_throttles(self):
assert response.status_code == 429
assert int(response['retry-after']) == 58

def test_handle_negative_throttle_value(self):
rpkilby marked this conversation as resolved.
Show resolved Hide resolved
self.set_throttle_timer(MockView_DoubleThrottling, 0)
request = self.factory.get('/')
for dummy in range(24):
response = MockView_DoubleThrottling.as_view()(request)
assert response.status_code == 429
assert int(response['retry-after']) == 60

previous_rate = User3SecRateThrottle.rate
User3SecRateThrottle.rate = '1/sec'

for dummy in range(24):
response = MockView_DoubleThrottling.as_view()(request)

assert response.status_code == 429
assert int(response['retry-after']) == 60

# reset
User3SecRateThrottle.rate = previous_rate
rpkilby marked this conversation as resolved.
Show resolved Hide resolved

def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers):
"""
Ensure the response returns an Retry-After field with status and next attributes
Expand Down