perf(hub): cache the names directory, share state builds, retry stations
Concurrent /api/state polls share one build through LiveState for a short TTL, and every POST and PATCH invalidates it, so a write is never followed by a stale view. The names directory is cached, the first transcript scan reads outside the hub lock, the graph timeline starts at the window begin, and unresolved radio stations are retried on a daemon thread every RETRY_SECONDS instead of failing until restart. 🎯 Fix: 85 % 💥 Break: 8 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 78 🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6c63c364bd
commit
96d4d2933c
18 files changed
+299
-32
No files matched your search
@@ -82,6 +82,7 @@ class Transcripts:
|
||||
def __init__(self, root: Path) -> None:
|
||||
self.root = root
|
||||
self.lock = threading.Lock()
|
||||
self.scanning = threading.Lock()
|
||||
self.offsets: dict[Path, int] = {}
|
||||
self.entries: deque[dict] = deque(maxlen=STREAM_KEEP)
|
||||
self.crons: dict[str, dict] = {}
|
||||
@@ -89,7 +90,8 @@ class Transcripts:
|
||||
self.scanned = -math.inf
|
||||
|
||||
def refresh(self) -> None:
|
||||
with self.lock:
|
||||
# Trip-wire: a first scan reads hundreds of MiB; only the per-file add takes self.lock, which /api/state also needs.
|
||||
with self.scanning:
|
||||
if time.monotonic() - self.scanned < STREAM_SCAN:
|
||||
return
|
||||
self.scanned = time.monotonic()
|
||||
@@ -98,7 +100,10 @@ class Transcripts:
|
||||
except OSError:
|
||||
paths = []
|
||||
for path in paths:
|
||||
self.read(path)
|
||||
rows = self.read(path)
|
||||
with self.lock:
|
||||
for row in rows:
|
||||
self.add(path, row)
|
||||
|
||||
def start(self, path: Path, info: os.stat_result) -> int:
|
||||
offset = self.offsets.get(path)
|
||||
@@ -107,7 +112,7 @@ class Transcripts:
|
||||
return max(info.st_size - STREAM_FIRST, 0) if recent else info.st_size
|
||||
return 0 if info.st_size < offset else offset
|
||||
|
||||
def read(self, path: Path) -> None:
|
||||
def read(self, path: Path) -> list[dict]:
|
||||
try:
|
||||
info = path.stat()
|
||||
begin = self.start(path, info)
|
||||
@@ -115,13 +120,11 @@ class Transcripts:
|
||||
handle.seek(begin)
|
||||
data = handle.read(max(info.st_size - begin, 0))
|
||||
except OSError:
|
||||
return
|
||||
return []
|
||||
end = data.rfind(b"\n") + 1
|
||||
self.offsets[path] = begin + end
|
||||
for raw in data[:end].splitlines():
|
||||
row = parse_line(raw.decode("utf-8", errors="replace"))
|
||||
if row is not None:
|
||||
self.add(path, row)
|
||||
rows = (parse_line(raw.decode("utf-8", errors="replace")) for raw in data[:end].splitlines())
|
||||
return [row for row in rows if row is not None]
|
||||
|
||||
def add(self, path: Path, row: dict) -> None:
|
||||
ts = iso_epoch(row.get("timestamp"))
|
||||
|
||||
Reference in new issue
Block a user