Skip to content

Commit ed33536

Browse files
committed
feat: add RATE_LIMIT_ envs to controller rate limits
1 parent 3258a19 commit ed33536

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

server/verify/rate_limit.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
from auth.get_user_info import getUserInfoByToken
66
from db.supabase.client import get_client
7+
from uilts.env import get_env_variable
78

8-
RATE_LIMIT_REQUESTS = 100
9-
RATE_LIMIT_DURATION = timedelta(minutes=1)
9+
RATE_LIMIT_ENABLED = get_env_variable("RATE_LIMIT_ENABLED")
10+
RATE_LIMIT_REQUESTS = get_env_variable("RATE_LIMIT_REQUESTS") or 100
11+
RATE_LIMIT_DURATION = timedelta(minutes=int(get_env_variable("RATE_LIMIT_DURATION") or 1))
1012

1113
async def verify_rate_limit(petercat: str = Cookie(None)):
14+
if not RATE_LIMIT_ENABLED:
15+
return
16+
1217
if not petercat:
1318
raise HTTPException(status_code=403, detail="Must Login")
1419
user = await getUserInfoByToken(petercat)
@@ -28,7 +33,7 @@ async def verify_rate_limit(petercat: str = Cookie(None)):
2833
# If the elapsed time is greater than the rate limit duration, reset the count
2934
user_usage['request_count'] = 1
3035
else:
31-
if user_usage['request_count'] >= RATE_LIMIT_REQUESTS:
36+
if user_usage['request_count'] >= int(RATE_LIMIT_REQUESTS):
3237
# If the request count exceeds the rate limit, return a JSON response with an error message
3338
raise HTTPException(
3439
status_code=429,

0 commit comments

Comments
 (0)