Jagd auf WS Abbrüche

This commit is contained in:
concierge committed 2026-11-02 11:00:58 +01:00
1 parent a95204b8d6
commit 1e2a58e563
9 files changed
+462 -51

No files matched your search

+58 -17
View File
@@ -66,6 +66,15 @@ class WebSocketBroker:
self.disconnected_clients = {}
self.queue_lock = Lock()
# Sequenznummern werden pro Client-ID vergeben und bleiben über einen
# WebSocket-Reconnect derselben Webview hinweg erhalten. Dadurch kann
# der Client nach einem Reconnect erkennen, ob ihm Frames fehlen.
# client_sequences[(tenant_id, client_id)] = zuletzt zugestellte Sequenz
self.client_sequences = {}
# Ein Lock pro Client-ID serialisiert Sequenzvergabe und Sendevorgang
# auch dann, wenn die Verbindung während eines Reconnects ersetzt wird.
self.client_sequence_locks = {}
# Zeitpunkt des letzten Code-2-Pongs je Gruppe (monotonic).
self.group_last_pong = {}
self.group_online_since = {}
@@ -138,6 +147,10 @@ class WebSocketBroker:
'last_ping': datetime.now(),
'connected_at': time.monotonic(),
'last_pong_at': None,
# Zuletzt an diese Client-ID zugestellte Sequenznummer. Die
# maßgebliche Zählung liegt in self.client_sequences, damit sie
# einen Reconnect überlebt.
'seq': self.client_sequences.get((tenant_id, client_id), 0),
# Ob diese Client-ID bereits vor diesem Handshake bekannt war.
# Nur dann bleibt ihre Queue auch bei einem fehlgeschlagenen
# erneuten Handshake erhalten.
@@ -156,24 +169,48 @@ class WebSocketBroker:
return True
def send_to_connection(self, tenant_id, client_id, data):
"""Sendet einen Frame thread-sicher an eine registrierte Client-Verbindung."""
conn = self._get_connection(tenant_id, client_id)
if not conn:
return False
"""Sendet einen Frame mit Sequenznummer thread-sicher an einen Client.
try:
lock = conn.get('send_lock')
if lock is None:
lock = Lock()
conn['send_lock'] = lock
with lock:
conn['ws'].send(data)
return True
except Exception:
# Eine fehlerhafte Verbindung darf den Versand an andere Clients
# nicht verhindern. Das Aufräumen übernimmt die Route beim
# Schließen der Verbindung.
return False
Die Sequenz wird pro Client-ID vergeben. Sie wird erst nach einem
erfolgreichen Socket-Send fortgeschrieben, damit ein fehlgeschlagener
Versand beim erneuten Zustellen keine Lücke erzeugt. Die Zählung bleibt
über Reconnects derselben Webview erhalten.
"""
key = (tenant_id, client_id)
seq_lock = self.client_sequence_locks.setdefault(key, Lock())
with seq_lock:
conn = self._get_connection(tenant_id, client_id)
if not conn:
return False
try:
payload = json.loads(data)
if not isinstance(payload, dict):
return False
last_seq = self.client_sequences.get(key)
if last_seq is None:
last_seq = int(conn.get('seq') or 0)
next_seq = last_seq + 1
payload['seq'] = next_seq
framed = json.dumps(payload, ensure_ascii=False)
send_lock = conn.get('send_lock')
if send_lock is None:
send_lock = Lock()
conn['send_lock'] = send_lock
with send_lock:
conn['ws'].send(framed)
self.client_sequences[key] = next_seq
conn['seq'] = next_seq
return True
except Exception:
# Eine fehlerhafte Verbindung darf den Versand an andere Clients
# nicht verhindern. Das Aufräumen übernimmt die Route beim
# Schließen der Verbindung.
return False
def is_authenticated(self, tenant_id, client_id):
"""Prüft, ob ein Client den Code-0-Handshake erfolgreich absolviert hat."""
@@ -282,6 +319,8 @@ class WebSocketBroker:
account_id = self.client_accounts.pop((tenant_id, client_id), None)
self.client_queues.pop((tenant_id, client_id), None)
self.disconnected_clients.pop((tenant_id, client_id), None)
self.client_sequences.pop((tenant_id, client_id), None)
self.client_sequence_locks.pop((tenant_id, client_id), None)
if account_id is not None:
clients = self.account_clients.get((tenant_id, account_id))
if clients is not None:
@@ -496,6 +535,8 @@ class WebSocketBroker:
account_id = self.client_accounts.pop(key, None)
self.client_queues.pop(key, None)
self.disconnected_clients.pop(key, None)
self.client_sequences.pop(key, None)
self.client_sequence_locks.pop(key, None)
if account_id is not None:
clients = self.account_clients.get((tenant_id, account_id))
if clients is not None: