This commit is contained in:
2025-12-18 07:27:37 -05:00
parent 6240888ac5
commit 041de95698
8 changed files with 1151 additions and 745 deletions

View File

@@ -47,12 +47,12 @@ class Radio(FastAPI):
self.sr_util = SRUtil()
self.lrclib = LRCLib()
self.lrc_cache: Dict[str, Optional[str]] = {}
self.lrc_cache_locks = {}
self.lrc_cache_locks: Dict[str, asyncio.Lock] = defaultdict(asyncio.Lock)
self.playlists_loaded: bool = False
# WebSocket connection management
self.active_connections: Dict[str, Set[WebSocket]] = {}
# Initialize broadcast locks to prevent duplicate events
self.broadcast_locks = defaultdict(asyncio.Lock)
self.broadcast_locks: Dict[str, asyncio.Lock] = defaultdict(asyncio.Lock)
self.endpoints: dict = {
"radio/np": self.radio_now_playing,
"radio/request": self.radio_request,
@@ -71,9 +71,9 @@ class Radio(FastAPI):
if endpoint == "radio/album_art":
methods = ["GET"]
app.add_api_route(
f"/{endpoint}", handler, methods=methods, include_in_schema=True,
f"/{endpoint}", handler, methods=methods, include_in_schema=False,
dependencies=[Depends(
RateLimiter(times=25, seconds=2))] if not endpoint == "radio/np" else None,
RateLimiter(times=25, seconds=2))],
)
# Add WebSocket route
@@ -83,12 +83,8 @@ class Radio(FastAPI):
app.add_websocket_route("/radio/ws/{station}", websocket_route_handler)
app.add_event_handler("startup", self.on_start)
async def on_start(self) -> None:
# Initialize locks in the event loop
self.lrc_cache_locks = defaultdict(asyncio.Lock)
self.broadcast_locks = defaultdict(asyncio.Lock)
# Load playlists for all stations
stations = ", ".join(self.radio_util.db_queries.keys())
logging.info("radio: Initializing stations:\n%s", stations)
await self.radio_util.load_playlists()