aiteamutils 0.2.35__tar.gz → 0.2.37__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/PKG-INFO +1 -1
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/security.py +22 -9
- aiteamutils-0.2.37/aiteamutils/version.py +2 -0
- aiteamutils-0.2.35/aiteamutils/version.py +0 -2
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/.cursorrules +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/.gitignore +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/README.md +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/__init__.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/base_model.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/base_repository.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/base_service.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/cache.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/config.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/database.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/dependencies.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/enums.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/exceptions.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/aiteamutils/validators.py +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/pyproject.toml +0 -0
- {aiteamutils-0.2.35 → aiteamutils-0.2.37}/setup.py +0 -0
@@ -6,6 +6,7 @@ from fastapi import Request, HTTPException, status
|
|
6
6
|
from functools import wraps
|
7
7
|
from jose import jwt, JWTError
|
8
8
|
from passlib.context import CryptContext
|
9
|
+
import logging
|
9
10
|
|
10
11
|
from .exceptions import CustomException, ErrorCode
|
11
12
|
from .database import DatabaseService
|
@@ -14,6 +15,9 @@ from .config import get_settings
|
|
14
15
|
|
15
16
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
16
17
|
|
18
|
+
# 전역 rate limit 상태 저장
|
19
|
+
_rate_limits: Dict[str, Dict[str, Any]] = {}
|
20
|
+
|
17
21
|
class RateLimitExceeded(CustomException):
|
18
22
|
"""Rate limit 초과 예외."""
|
19
23
|
|
@@ -199,11 +203,11 @@ def rate_limit(
|
|
199
203
|
key_func: Optional[Callable] = None
|
200
204
|
):
|
201
205
|
"""Rate limiting 데코레이터."""
|
202
|
-
rate_limits: Dict[str, Dict[str, Any]] = {}
|
203
|
-
|
204
206
|
def decorator(func: Callable) -> Callable:
|
205
207
|
@wraps(func)
|
206
208
|
async def wrapper(*args, **kwargs):
|
209
|
+
logging.info(f"[rate_limit] Starting rate limit check for {func.__name__}")
|
210
|
+
|
207
211
|
# Request 객체 찾기
|
208
212
|
request = None
|
209
213
|
for arg in args:
|
@@ -216,6 +220,7 @@ def rate_limit(
|
|
216
220
|
request = arg
|
217
221
|
break
|
218
222
|
if not request:
|
223
|
+
logging.error("[rate_limit] Request object not found in args or kwargs")
|
219
224
|
raise CustomException(
|
220
225
|
ErrorCode.INTERNAL_ERROR,
|
221
226
|
detail="Request object not found",
|
@@ -229,22 +234,27 @@ def rate_limit(
|
|
229
234
|
client_ip = request.client.host
|
230
235
|
rate_limit_key = f"rate_limit:{client_ip}:{func.__name__}"
|
231
236
|
|
237
|
+
logging.info(f"[rate_limit] Rate limit key: {rate_limit_key}")
|
238
|
+
|
232
239
|
now = datetime.now(UTC)
|
233
240
|
|
234
241
|
# 현재 rate limit 정보 가져오기
|
235
|
-
rate_info =
|
242
|
+
rate_info = _rate_limits.get(rate_limit_key)
|
243
|
+
logging.info(f"[rate_limit] Current rate info: {rate_info}")
|
236
244
|
|
237
245
|
if rate_info is None or (now - rate_info["start_time"]).total_seconds() >= window_seconds:
|
238
246
|
# 새로운 rate limit 설정
|
239
|
-
|
247
|
+
_rate_limits[rate_limit_key] = {
|
240
248
|
"count": 1,
|
241
249
|
"start_time": now
|
242
250
|
}
|
251
|
+
logging.info(f"[rate_limit] Created new rate limit: {_rate_limits[rate_limit_key]}")
|
243
252
|
else:
|
244
253
|
# 기존 rate limit 업데이트
|
245
254
|
if rate_info["count"] >= max_requests:
|
246
255
|
# rate limit 초과
|
247
256
|
remaining_seconds = window_seconds - (now - rate_info["start_time"]).total_seconds()
|
257
|
+
logging.warning(f"[rate_limit] Rate limit exceeded. Remaining seconds: {remaining_seconds}")
|
248
258
|
raise RateLimitExceeded(
|
249
259
|
detail=rate_limit_key,
|
250
260
|
source_function=func.__name__,
|
@@ -253,22 +263,25 @@ def rate_limit(
|
|
253
263
|
window_seconds=window_seconds
|
254
264
|
)
|
255
265
|
rate_info["count"] += 1
|
266
|
+
logging.info(f"[rate_limit] Updated rate info: {rate_info}")
|
256
267
|
|
257
268
|
try:
|
258
|
-
|
259
|
-
|
269
|
+
logging.info(f"[rate_limit] Executing original function: {func.__name__}")
|
270
|
+
result = await func(*args, **kwargs)
|
271
|
+
logging.info("[rate_limit] Function executed successfully")
|
272
|
+
return result
|
260
273
|
except CustomException as e:
|
261
|
-
|
274
|
+
logging.error(f"[rate_limit] CustomException occurred: {str(e)}")
|
262
275
|
raise e
|
263
276
|
except Exception as e:
|
264
|
-
|
277
|
+
logging.error(f"[rate_limit] Unexpected error occurred: {str(e)}")
|
265
278
|
raise CustomException(
|
266
279
|
ErrorCode.INTERNAL_ERROR,
|
267
280
|
detail=str(e),
|
268
281
|
source_function=func.__name__,
|
269
282
|
original_error=e
|
270
283
|
)
|
271
|
-
|
284
|
+
|
272
285
|
return wrapper
|
273
286
|
return decorator
|
274
287
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|