Skip to content

Commit

Permalink
Implement Emergency Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
raimannma committed Nov 6, 2024
1 parent fbc7779 commit 7010413
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions deadlock_analytics_api/rate_limiter/limiter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import time

from cachetools.func import ttl_cache
Expand All @@ -13,6 +14,7 @@
LOGGER = logging.getLogger(__name__)

MAX_TTL_SECONDS = 60 * 60 # 1 hour
EMERGENCY_MODE = os.environ.get("EMERGENCY_MODE", "false").lower() == "true"


def apply_limits(
Expand All @@ -26,6 +28,12 @@ def apply_limits(
):
ip = request.headers.get("CF-Connecting-IP", request.client.host)
api_key = request.headers.get("X-API-Key", request.query_params.get("api_key"))
if api_key is None and EMERGENCY_MODE:
raise HTTPException(
status_code=503,
detail="API key required in emergency mode",
headers={"Retry-After": "60"},
)
api_key = (
api_key.lstrip("HEXE-")
if api_key is not None
Expand Down

0 comments on commit 7010413

Please sign in to comment.