feat: 性能优化 + 设备总览轨迹展示 + 广播指令API
性能: SQLite WAL模式、aiohttp Session复用、TCP连接锁+空闲超时、 device_id缓存、WebSocket并发广播、API Key认证缓存、围栏N+1查询 批量化、逆地理编码并行化、新增5个DB索引、日志降级DEBUG 功能: 广播指令API(broadcast)、exclude_type低精度后端过滤、 前端设备总览Tab+多设备轨迹叠加+高亮联动+搜索+专属颜色 via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run>
This commit is contained in:
@@ -62,17 +62,22 @@ class WebSocketManager:
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
disconnected = []
|
||||
# Snapshot dict to avoid RuntimeError from concurrent modification
|
||||
for ws, topics in list(self.active_connections.items()):
|
||||
if topic in topics:
|
||||
try:
|
||||
await ws.send_text(message)
|
||||
except Exception:
|
||||
disconnected.append(ws)
|
||||
# Send to all subscribers concurrently with timeout
|
||||
subscribers = [(ws, topics) for ws, topics in list(self.active_connections.items()) if topic in topics]
|
||||
if not subscribers:
|
||||
return
|
||||
|
||||
for ws in disconnected:
|
||||
self.active_connections.pop(ws, None)
|
||||
async def _safe_send(ws):
|
||||
try:
|
||||
await asyncio.wait_for(ws.send_text(message), timeout=3.0)
|
||||
return None
|
||||
except Exception:
|
||||
return ws
|
||||
|
||||
results = await asyncio.gather(*[_safe_send(ws) for ws, _ in subscribers])
|
||||
for ws in results:
|
||||
if ws is not None:
|
||||
self.active_connections.pop(ws, None)
|
||||
|
||||
def broadcast_nonblocking(self, topic: str, data: dict):
|
||||
"""Fire-and-forget broadcast (used from TCP handler context)."""
|
||||
|
||||
Reference in New Issue
Block a user