Spaces:
Paused
Paused
Mirrowel commited on
Commit ·
1faf5c2
1
Parent(s): 6f43a31
refactor: Remove file locking mechanism from usage data saving
Browse files
src/rotator_library/usage_manager.py
CHANGED
|
@@ -5,7 +5,6 @@ import logging
|
|
| 5 |
import asyncio
|
| 6 |
from datetime import date, datetime, timezone, time as dt_time
|
| 7 |
from typing import Dict, List, Optional, Set
|
| 8 |
-
from filelock import FileLock
|
| 9 |
import aiofiles
|
| 10 |
import litellm
|
| 11 |
|
|
@@ -23,7 +22,6 @@ class UsageManager:
|
|
| 23 |
"""
|
| 24 |
def __init__(self, file_path: str = "key_usage.json", wait_timeout: int = 13, daily_reset_time_utc: Optional[str] = "03:00"):
|
| 25 |
self.file_path = file_path
|
| 26 |
-
self.file_lock = FileLock(f"{self.file_path}.lock")
|
| 27 |
self.key_states: Dict[str, Dict[str, Any]] = {}
|
| 28 |
self.wait_timeout = wait_timeout
|
| 29 |
|
|
@@ -67,9 +65,8 @@ class UsageManager:
|
|
| 67 |
if self._usage_data is None:
|
| 68 |
return
|
| 69 |
async with self._data_lock:
|
| 70 |
-
with self.
|
| 71 |
-
|
| 72 |
-
await f.write(json.dumps(self._usage_data, indent=2))
|
| 73 |
|
| 74 |
async def _reset_daily_stats_if_needed(self):
|
| 75 |
"""Checks if daily stats need to be reset for any key."""
|
|
|
|
| 5 |
import asyncio
|
| 6 |
from datetime import date, datetime, timezone, time as dt_time
|
| 7 |
from typing import Dict, List, Optional, Set
|
|
|
|
| 8 |
import aiofiles
|
| 9 |
import litellm
|
| 10 |
|
|
|
|
| 22 |
"""
|
| 23 |
def __init__(self, file_path: str = "key_usage.json", wait_timeout: int = 13, daily_reset_time_utc: Optional[str] = "03:00"):
|
| 24 |
self.file_path = file_path
|
|
|
|
| 25 |
self.key_states: Dict[str, Dict[str, Any]] = {}
|
| 26 |
self.wait_timeout = wait_timeout
|
| 27 |
|
|
|
|
| 65 |
if self._usage_data is None:
|
| 66 |
return
|
| 67 |
async with self._data_lock:
|
| 68 |
+
async with aiofiles.open(self.file_path, 'w') as f:
|
| 69 |
+
await f.write(json.dumps(self._usage_data, indent=2))
|
|
|
|
| 70 |
|
| 71 |
async def _reset_daily_stats_if_needed(self):
|
| 72 |
"""Checks if daily stats need to be reset for any key."""
|