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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiteamutils
3
- Version: 0.2.35
3
+ Version: 0.2.37
4
4
  Summary: AI Team Utilities
5
5
  Project-URL: Homepage, https://github.com/yourusername/aiteamutils
6
6
  Project-URL: Issues, https://github.com/yourusername/aiteamutils/issues
@@ -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 = rate_limits.get(rate_limit_key)
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
- rate_limits[rate_limit_key] = {
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
- return await func(*args, **kwargs)
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
- # CustomException 그대로 전파
274
+ logging.error(f"[rate_limit] CustomException occurred: {str(e)}")
262
275
  raise e
263
276
  except Exception as e:
264
- # 다른 예외는 INTERNAL_ERROR로 래핑
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
 
@@ -0,0 +1,2 @@
1
+ """버전 정보"""
2
+ __version__ = "0.2.37"
@@ -1,2 +0,0 @@
1
- """버전 정보"""
2
- __version__ = "0.2.35"
File without changes
File without changes
File without changes
File without changes