aiteamutils 0.2.33__tar.gz → 0.2.35__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.33
3
+ Version: 0.2.35
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
@@ -199,7 +199,7 @@ def rate_limit(
199
199
  key_func: Optional[Callable] = None
200
200
  ):
201
201
  """Rate limiting 데코레이터."""
202
- limiter = RateLimiter(max_requests, window_seconds)
202
+ rate_limits: Dict[str, Dict[str, Any]] = {}
203
203
 
204
204
  def decorator(func: Callable) -> Callable:
205
205
  @wraps(func)
@@ -216,39 +216,56 @@ def rate_limit(
216
216
  request = arg
217
217
  break
218
218
  if not request:
219
- raise SecurityError(
219
+ raise CustomException(
220
220
  ErrorCode.INTERNAL_ERROR,
221
221
  detail="Request object not found",
222
222
  source_function="rate_limit"
223
223
  )
224
224
 
225
- # Rate limit 키 생성
225
+ # 레이트 리밋 키 생성
226
226
  if key_func:
227
227
  rate_limit_key = f"rate_limit:{key_func(request)}"
228
228
  else:
229
229
  client_ip = request.client.host
230
230
  rate_limit_key = f"rate_limit:{client_ip}:{func.__name__}"
231
231
 
232
- try:
233
- if not limiter.is_allowed(rate_limit_key):
234
- remaining_time = limiter.get_remaining_time(rate_limit_key)
232
+ now = datetime.now(UTC)
233
+
234
+ # 현재 rate limit 정보 가져오기
235
+ rate_info = rate_limits.get(rate_limit_key)
236
+
237
+ if rate_info is None or (now - rate_info["start_time"]).total_seconds() >= window_seconds:
238
+ # 새로운 rate limit 설정
239
+ rate_limits[rate_limit_key] = {
240
+ "count": 1,
241
+ "start_time": now
242
+ }
243
+ else:
244
+ # 기존 rate limit 업데이트
245
+ if rate_info["count"] >= max_requests:
246
+ # rate limit 초과
247
+ remaining_seconds = window_seconds - (now - rate_info["start_time"]).total_seconds()
235
248
  raise RateLimitExceeded(
236
- detail=f"Rate limit exceeded. Try again in {int(remaining_time)} seconds",
249
+ detail=rate_limit_key,
237
250
  source_function=func.__name__,
238
- remaining_seconds=remaining_time,
251
+ remaining_seconds=remaining_seconds,
239
252
  max_requests=max_requests,
240
253
  window_seconds=window_seconds
241
254
  )
242
-
255
+ rate_info["count"] += 1
256
+
257
+ try:
258
+ # 원래 함수 실행
243
259
  return await func(*args, **kwargs)
244
-
245
- except (RateLimitExceeded, SecurityError) as e:
260
+ except CustomException as e:
261
+ # CustomException은 그대로 전파
246
262
  raise e
247
263
  except Exception as e:
248
- raise SecurityError(
264
+ # 다른 예외는 INTERNAL_ERROR로 래핑
265
+ raise CustomException(
249
266
  ErrorCode.INTERNAL_ERROR,
250
267
  detail=str(e),
251
- source_function="rate_limit",
268
+ source_function=func.__name__,
252
269
  original_error=e
253
270
  )
254
271
 
@@ -0,0 +1,2 @@
1
+ """버전 정보"""
2
+ __version__ = "0.2.35"
@@ -1,2 +0,0 @@
1
- """버전 정보"""
2
- __version__ = "0.2.33"
File without changes
File without changes
File without changes
File without changes