Spaces:
Build error
Build error
Sarp Bilgiç commited on
Commit ·
478dc25
1
Parent(s): 791579e
blocking async call for rate limit removed
Browse files
src/api/dependencies/rate_limit.py
CHANGED
|
@@ -4,9 +4,9 @@ from src.api.models.user import User
|
|
| 4 |
from src.api.dependencies.auth import get_auth_service
|
| 5 |
from fastapi_limiter.depends import RateLimiter
|
| 6 |
from src.core.settings import settings
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
async def get_real_ip(request: Request) -> str:
|
| 10 |
if request.client:
|
| 11 |
return request.client.host
|
| 12 |
return "unknown"
|
|
@@ -16,16 +16,16 @@ async def user_id_identifier(request: Request) -> str:
|
|
| 16 |
if authorization and authorization.startswith("Bearer "):
|
| 17 |
token = authorization.split(" ")[1]
|
| 18 |
auth_service = get_auth_service()
|
| 19 |
-
payload = auth_service.decode_access_token
|
| 20 |
if payload:
|
| 21 |
user_email = payload.get("sub")
|
| 22 |
if user_email:
|
| 23 |
return f"user:{user_email}"
|
| 24 |
-
ip =
|
| 25 |
return f"ip:{ip}"
|
| 26 |
|
| 27 |
async def ip_identifier(request: Request) -> str:
|
| 28 |
-
ip =
|
| 29 |
return f"ip:{ip}"
|
| 30 |
|
| 31 |
anonymous_rag_rate_limiter = RateLimiter(
|
|
|
|
| 4 |
from src.api.dependencies.auth import get_auth_service
|
| 5 |
from fastapi_limiter.depends import RateLimiter
|
| 6 |
from src.core.settings import settings
|
| 7 |
+
import asyncio
|
| 8 |
|
| 9 |
+
def get_real_ip(request: Request) -> str:
|
|
|
|
| 10 |
if request.client:
|
| 11 |
return request.client.host
|
| 12 |
return "unknown"
|
|
|
|
| 16 |
if authorization and authorization.startswith("Bearer "):
|
| 17 |
token = authorization.split(" ")[1]
|
| 18 |
auth_service = get_auth_service()
|
| 19 |
+
payload = await asyncio.to_thread(auth_service.decode_access_token, token)
|
| 20 |
if payload:
|
| 21 |
user_email = payload.get("sub")
|
| 22 |
if user_email:
|
| 23 |
return f"user:{user_email}"
|
| 24 |
+
ip = get_real_ip(request)
|
| 25 |
return f"ip:{ip}"
|
| 26 |
|
| 27 |
async def ip_identifier(request: Request) -> str:
|
| 28 |
+
ip = get_real_ip(request)
|
| 29 |
return f"ip:{ip}"
|
| 30 |
|
| 31 |
anonymous_rag_rate_limiter = RateLimiter(
|