feat(F569): DDP-Fix — Option A Puls-Caching + Option E WebSocket-Ping @implements: [F-569]

This commit is contained in:
Bernd (Hermes)
2026-10-03 19:48:27 +02:00
parent 1edea6b71d
commit 1c15ba0a15
4 changed files with 415 additions and 2 deletions
+44 -1
View File
@@ -764,7 +764,25 @@ class DDPNotConnected(Exception):
class DDPClient:
PING_INTERVAL = 8.0
# F-569 Option E: WebSocket-Ping zur Silent-Death-Erkennung.
#
# WICHTIG — Implementierung-Hinweis:
# Dieser DDPClient verwendet eine RAW-SOCKET-WebSocket-Implementierung
# (manueller Handshake + Frame-Parsing in _handshake/_send_frame/
# _reader_loop), NICHT die Bibliotheken `websocket-client` oder
# `websockets`. Daher existieren die Library-Parameter `ping_interval`/
# `ping_timeout` im Connect-Call NICHT — die Äquivalenz wird direkt
# in der Raw-Loop umgesetzt:
# - PING_INTERVAL = 25s → DDP-Level {"msg":"ping"} alle 25s
# - PING_TIMEOUT = 10s → wenn nach gesendetem Ping kein Frame
# empfangen wird, gilt die Verbindung als tot.
#
# Vorher: PING_INTERVAL=8.0, und tote Verbindungen wurden erst nach
# HEALTH_SILENT_LIMIT=300s (Silent Death, is_alive) erkannt. Mit dem
# Ping-Timeout bricht der Reader-Loop bereits nach ~35s (25+10) ab →
# connected=False → _listen_loop triggert sofort _reconnect_ddp().
PING_INTERVAL = 25.0
PING_TIMEOUT = 10.0
def __init__(self, host: str, on_message_cb=None, on_connected_cb=None, on_room_event_cb=None):
@@ -783,6 +801,9 @@ class DDPClient:
self._session: Optional[str] = None
self._reconnect_attempt = 0
self._last_rx_time = 0.0
# F-569 Option E: Zeitpunkt des zuletzt gesendeten DDP-Pings.
# Wird auf 0.0 zurückgesetzt sobald ein Frame empfangen wird.
self._last_ping_sent = 0.0
# P1-2 O-2: Future-Store für await-Semantik
self._pending_results: Dict[str, concurrent.futures.Future] = {}
self._loop: Optional[asyncio.AbstractEventLoop] = None
@@ -854,8 +875,27 @@ class DDPClient:
try:
self._send_json({"msg": "ping"})
last_ping = time.time()
# F-569 Option E: Ping-Sendezeitpunkt merken — wenn
# danach PING_TIMEOUT Sekunden lang KEIN Frame empfangen
# wird, gilt die Verbindung als tot (Silent-Death-Erkennung
# in ~35s statt 300s).
self._last_ping_sent = time.time()
except Exception:
break
# F-569 Option E: Ping-Timeout — kein Frame nach Ping →
# tote Verbindung erkennen und Reader-Loop beenden. Der
# Adapter (_listen_loop) sieht connected=False und triggert
# _reconnect_ddp().
if (
self._last_ping_sent > 0
and (time.time() - self._last_ping_sent) >= self.PING_TIMEOUT
):
logger.warning(
"[ddp] Ping-Timeout (F-569 E): %.0fs ohne Frame nach "
"Ping — Verbindung als tot markiert",
self.PING_TIMEOUT,
)
break
# P1-2 O-2: Periodic sweep of stale pending results (every 5s)
if time.time() - last_sweep >= 5.0:
self._sweep_pending_results()
@@ -886,6 +926,9 @@ class DDPClient:
remaining -= len(chunk)
if len(payload) < length:
break
# F-569 Option E: Jeder empfangene Frame (ping/pong/data)
# belegt Liveness → Ping-Timeout-Timer zurücksetzen.
self._last_ping_sent = 0.0
if masked:
mask = payload[:4]
payload = bytes(b ^ mask[i % 4] for i, b in enumerate(payload[4:]))