Mirrowel commited on
Commit
33e95e6
·
1 Parent(s): 5538104

feat: Enhance daily reset logic to ensure timezone-aware date handling

Browse files
src/rotator_library/usage_manager.py CHANGED
@@ -84,8 +84,11 @@ class UsageManager:
84
  last_reset_str = data.get("last_daily_reset", "")
85
 
86
  if last_reset_str != today_str:
87
- last_reset_dt = datetime.fromisoformat(last_reset_str) if last_reset_str else None
88
-
 
 
 
89
  # Determine the reset threshold for today
90
  reset_threshold_today = datetime.combine(now_utc.date(), self.daily_reset_time_utc)
91
 
@@ -224,11 +227,12 @@ class UsageManager:
224
  """
225
  await self._lazy_init()
226
  async with self._data_lock:
227
- key_data = self._usage_data.setdefault(key, {"daily": {"date": date.today().isoformat(), "models": {}}, "global": {"models": {}}, "model_cooldowns": {}, "failures": {}})
 
228
 
229
  # Perform a just-in-time daily reset if the date has changed.
230
- if key_data["daily"].get("date") != date.today().isoformat():
231
- key_data["daily"] = {"date": date.today().isoformat(), "models": {}}
232
 
233
  # Always record a success and reset failures
234
  model_failures = key_data.setdefault("failures", {}).setdefault(model, {})
@@ -261,7 +265,8 @@ class UsageManager:
261
  """Records a failure and applies cooldowns based on an escalating backoff strategy."""
262
  await self._lazy_init()
263
  async with self._data_lock:
264
- key_data = self._usage_data.setdefault(key, {"daily": {"date": date.today().isoformat(), "models": {}}, "global": {"models": {}}, "model_cooldowns": {}, "failures": {}})
 
265
 
266
  # Handle specific error types first
267
  if classified_error.error_type == 'rate_limit' and classified_error.retry_after:
 
84
  last_reset_str = data.get("last_daily_reset", "")
85
 
86
  if last_reset_str != today_str:
87
+ last_reset_dt = None
88
+ if last_reset_str:
89
+ # Ensure the parsed datetime is timezone-aware (UTC)
90
+ last_reset_dt = datetime.fromisoformat(last_reset_str).replace(tzinfo=timezone.utc)
91
+
92
  # Determine the reset threshold for today
93
  reset_threshold_today = datetime.combine(now_utc.date(), self.daily_reset_time_utc)
94
 
 
227
  """
228
  await self._lazy_init()
229
  async with self._data_lock:
230
+ today_utc_str = datetime.now(timezone.utc).date().isoformat()
231
+ key_data = self._usage_data.setdefault(key, {"daily": {"date": today_utc_str, "models": {}}, "global": {"models": {}}, "model_cooldowns": {}, "failures": {}})
232
 
233
  # Perform a just-in-time daily reset if the date has changed.
234
+ if key_data["daily"].get("date") != today_utc_str:
235
+ key_data["daily"] = {"date": today_utc_str, "models": {}}
236
 
237
  # Always record a success and reset failures
238
  model_failures = key_data.setdefault("failures", {}).setdefault(model, {})
 
265
  """Records a failure and applies cooldowns based on an escalating backoff strategy."""
266
  await self._lazy_init()
267
  async with self._data_lock:
268
+ today_utc_str = datetime.now(timezone.utc).date().isoformat()
269
+ key_data = self._usage_data.setdefault(key, {"daily": {"date": today_utc_str, "models": {}}, "global": {"models": {}}, "model_cooldowns": {}, "failures": {}})
270
 
271
  # Handle specific error types first
272
  if classified_error.error_type == 'rate_limit' and classified_error.retry_after: