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

Increasing rate limiting #539

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async def user(request: Request):
# Geolocate route. Returns the country, city, latitude, and longitude of the IP address.
# If we have a custom header of 'X-Sentinel-Source', then we skip rate limiting so that Sentinel is not rate limited
@handler.get("/geolocate/{ip}")
@limiter.limit("10/minute", key_func=sentinel_key_func)
@limiter.limit("20/minute", key_func=sentinel_key_func)
def geolocate(ip, request: Request):
reader = maxmind.geolocate(ip)
if isinstance(reader, str):
Expand Down Expand Up @@ -327,7 +327,7 @@ def handle_webhook(id: str, payload: WebhookPayload | str, request: Request):
# Route53 uses this as a healthcheck every 30 seconds and the alb uses this as a checkpoint every 10 seconds.
# As a result, we are giving a generous rate limit of so that we don't run into any issues with the healthchecks
@handler.get("/version")
@limiter.limit("15/minute")
@limiter.limit("50/minute")
def get_version(request: Request):
return {"version": os.environ.get("GIT_SHA", "unknown")}

Expand Down
8 changes: 4 additions & 4 deletions app/tests/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ async def test_geolocate_rate_limiting():
return_value=("Country", "City", 12.34, 56.78),
):
# Make 10 requests to the geolocate endpoint
for _ in range(10):
for _ in range(20):
response = await client.get("/geolocate/8.8.8.8")
assert response.status_code == 200
assert response.json() == {
Expand All @@ -601,7 +601,7 @@ async def test_geolocate_rate_limiting():
"longitude": 56.78,
}

# The 11th request should be rate limited
# The 21th request should be rate limited
response = await client.get("/geolocate/8.8.8.8")
assert response.status_code == 429
assert response.json() == {"message": "Rate limit exceeded"}
Expand Down Expand Up @@ -634,11 +634,11 @@ async def test_webhooks_rate_limiting():
async def test_version_rate_limiting():
async with AsyncClient(app=app, base_url="http://test") as client:
# Make 5 requests to the version endpoint
for _ in range(15):
for _ in range(50):
response = await client.get("/version")
assert response.status_code == 200

# The 6th request should be rate limited
# The 51th request should be rate limited
response = await client.get("/version")
assert response.status_code == 429
assert response.json() == {"message": "Rate limit exceeded"}
Expand Down
Loading