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>
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
from collections.abc import Callable
|
|
|
|
LIVE_TTL = 2.0
|
|
|
|
|
|
class LiveState:
|
|
def __init__(self, ttl: float) -> None:
|
|
"""Args:
|
|
ttl: seconds a finished build serves later callers of the same
|
|
key; 0 rebuilds every time but still one build at a time.
|
|
"""
|
|
self.ttl = ttl
|
|
self.guard = threading.Lock()
|
|
self.locks: dict[object, threading.Lock] = {}
|
|
self.built: dict[object, tuple[int, float, dict]] = {}
|
|
self.generation = 0
|
|
|
|
def invalidate(self) -> None:
|
|
with self.guard:
|
|
self.generation += 1
|
|
|
|
def shared(self, key: object, build: Callable[[], dict]) -> dict:
|
|
"""Returns:
|
|
build()'s result; callers arriving while it runs wait for it and
|
|
share it, and so do callers within ttl after it, until
|
|
invalidate() is called.
|
|
"""
|
|
with self.guard:
|
|
lock = self.locks.setdefault(key, threading.Lock())
|
|
with lock:
|
|
with self.guard:
|
|
generation = self.generation
|
|
hit = self.built.get(key)
|
|
if hit is not None and hit[0] == generation and time.monotonic() - hit[1] < self.ttl:
|
|
return hit[2]
|
|
value = build()
|
|
with self.guard:
|
|
self.built[key] = (generation, time.monotonic(), value)
|
|
return value
|