fix: backend improvements (customer health, tests, middleware, corpus)
This commit is contained in:
@@ -12,6 +12,7 @@ import logging
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Dict, Optional
|
||||
from app.core.exceptions import QuotaExceededError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -299,15 +300,17 @@ class QuotaMiddleware(BaseHTTPMiddleware):
|
||||
]
|
||||
|
||||
matched_key = None
|
||||
matched_limits = None
|
||||
for prefix, limits in quota_map:
|
||||
if path.startswith(prefix):
|
||||
matched_key = prefix
|
||||
matched_limits = limits
|
||||
break
|
||||
|
||||
if not matched_key:
|
||||
if not matched_key or matched_limits is None:
|
||||
return await call_next(request)
|
||||
|
||||
limit = quota_map[matched_key].get(tier)
|
||||
limit = matched_limits.get(tier)
|
||||
if limit is None:
|
||||
return await call_next(request)
|
||||
|
||||
@@ -317,7 +320,6 @@ class QuotaMiddleware(BaseHTTPMiddleware):
|
||||
current = await r.incr(key)
|
||||
await r.expire(key, 86400)
|
||||
if current > limit:
|
||||
from app.core.exceptions import QuotaExceededError
|
||||
raise QuotaExceededError(matched_key)
|
||||
request.state.quota_remaining = limit - current
|
||||
except QuotaExceededError:
|
||||
|
||||
Reference in New Issue
Block a user