Skip to content
Snippets Groups Projects
Verified Commit f7e463b5 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

See #261: More verbose X-RateLimit headers

parent e274eab2
No related branches found
No related tags found
1 merge request!877: Rate limiting
......@@ -230,9 +230,16 @@ class ThrottleStatusMiddleware:
)
response["X-RateLimit-Duration"] = str(throttle_status["duration"])
if throttle_status["history"]:
latest_request = throttle_status["history"][0]
now = int(time.time())
# At this point, the client can send additional requests
oldtest_request = throttle_status["history"][-1]
remaining = throttle_status["duration"] - (now - int(oldtest_request))
response["X-RateLimit-Available"] = str(now + remaining)
response["X-RateLimit-AvailableSeconds"] = str(remaining)
# At this point, all Rate Limit is reset to 0
latest_request = throttle_status["history"][0]
remaining = throttle_status["duration"] - (now - int(latest_request))
response["X-RateLimit-Reset"] = str(now + remaining)
response["X-RateLimit-ResetSeconds"] = str(remaining)
return response
......@@ -202,7 +202,7 @@ def test_throttle_status_middleware_includes_info_in_response_headers(mocker):
"num_requests": 42,
"duration": 3600,
"scope": "hello",
"history": [time.time() - 1600],
"history": [time.time() - 1600, time.time() - 1800],
}
),
)
......@@ -210,10 +210,13 @@ def test_throttle_status_middleware_includes_info_in_response_headers(mocker):
assert m(request) == response
assert response["X-RateLimit-Limit"] == "42"
assert response["X-RateLimit-Remaining"] == "41"
assert response["X-RateLimit-Remaining"] == "40"
assert response["X-RateLimit-Duration"] == "3600"
assert response["X-RateLimit-Scope"] == "hello"
assert response["X-RateLimit-Reset"] == str(int(time.time()) + 2000)
assert response["X-RateLimit-ResetSeconds"] == str(2000)
assert response["X-RateLimit-Available"] == str(int(time.time()) + 1800)
assert response["X-RateLimit-AvailableSeconds"] == str(1800)
def test_throttle_status_middleware_returns_proper_response(mocker):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment