bug fix shutdown/reconnect WS resultiert in offline für FCM
This commit is contained in:
1 parent
c51ef02f88
commit
b5f928bf76
11 files changed
+331
-11
No files matched your search
@@ -122,6 +122,10 @@ class WebSocketBroker:
|
||||
):
|
||||
return False
|
||||
|
||||
# Der Worker ist offensichtlich aktiv; sein Lebenszeichen verhindert,
|
||||
# dass andere Worker seine Clients fälschlich als verwaist markieren.
|
||||
store.touch_worker(self.worker_id)
|
||||
|
||||
# Für Gruppen-Accounts die zugehörige Gruppe ermitteln. Ein Account der
|
||||
# Rolle "Gruppe" ist höchstens einer Gruppe zugeordnet.
|
||||
if group_id is None and role == 'Gruppe':
|
||||
@@ -161,6 +165,11 @@ class WebSocketBroker:
|
||||
'last_ping': datetime.now(),
|
||||
'connected_at': time.monotonic(),
|
||||
'last_pong_at': None,
|
||||
# Zeitpunkt des letzten beliebigen Lebenszeichens. Wird beim
|
||||
# Handshake gesetzt und bei jeder eingehenden Nachricht
|
||||
# aktualisiert, damit auch Clients ohne Pong nicht fälschlich
|
||||
# als tot gelten.
|
||||
'last_seen_at': time.time(),
|
||||
# Zuletzt an diese Client-ID zugestellte Sequenznummer. Die
|
||||
# maßgebliche Zählung liegt in ws_clients und überlebt damit einen
|
||||
# Worker-Wechsel.
|
||||
@@ -283,6 +292,8 @@ class WebSocketBroker:
|
||||
store.set_group_online_since(tenant_id, conn['group_id'])
|
||||
|
||||
self.authenticated.setdefault(tenant_id, {})[client_id] = True
|
||||
conn['last_seen_at'] = time.time()
|
||||
conn['last_pong_at'] = conn['last_seen_at']
|
||||
store.mark_authenticated(
|
||||
tenant_id, client_id, True, worker_id=self.worker_id
|
||||
)
|
||||
@@ -359,6 +370,60 @@ class WebSocketBroker:
|
||||
tenant_id, client_id, worker_id=self.worker_id
|
||||
)
|
||||
|
||||
def check_client_liveness(self, timeout_seconds):
|
||||
"""Trennt authentifizierte Verbindungen ohne Lebenszeichen.
|
||||
|
||||
Ein sauber geschlossener WebSocket wird bereits in der WS-Route
|
||||
erkannt. Mobile Geräte verlieren die Verbindung beim Schlafenlegen
|
||||
jedoch oft ohne FIN/RST; der Server sieht dann scheinbar eine offene
|
||||
Verbindung und markiert den Client nie als getrennt. Diese Prüfung
|
||||
schließt solche halb offenen Verbindungen nach ``timeout_seconds``
|
||||
und macht sie damit für FCM-Aufwachsignale sichtbar.
|
||||
|
||||
Rückgabe: Anzahl der getrennten Verbindungen.
|
||||
"""
|
||||
try:
|
||||
timeout_seconds = float(timeout_seconds)
|
||||
except (TypeError, ValueError):
|
||||
timeout_seconds = 0
|
||||
if timeout_seconds <= 0:
|
||||
return 0
|
||||
|
||||
now = time.time()
|
||||
disconnected = 0
|
||||
for tenant_id, connections in list(self.connections.items()):
|
||||
for client_id, conn in list(connections.items()):
|
||||
if not self.is_authenticated(tenant_id, client_id):
|
||||
continue
|
||||
|
||||
last_seen = conn.get('last_seen_at') or conn.get('last_pong_at')
|
||||
if last_seen is None:
|
||||
# Sehr alte Verbindungsobjekte haben noch keinen
|
||||
# Zeitstempel; sie erhalten eine Gnadenfrist.
|
||||
conn['last_seen_at'] = now
|
||||
continue
|
||||
if now - last_seen <= timeout_seconds:
|
||||
continue
|
||||
|
||||
ws = conn.get('ws')
|
||||
try:
|
||||
if ws is not None:
|
||||
ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.remove_connection(tenant_id, client_id, ws)
|
||||
disconnected += 1
|
||||
|
||||
try:
|
||||
current_app.logger.info(
|
||||
'WebSocket-Verbindung ohne Lebenszeichen getrennt: '
|
||||
'tenant=%s client=%s last_seen=%.0fs',
|
||||
tenant_id, client_id, now - last_seen
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return disconnected
|
||||
|
||||
def _get_connection(self, tenant_id, client_id):
|
||||
return self.connections.get(tenant_id, {}).get(client_id)
|
||||
|
||||
@@ -835,6 +900,9 @@ class WebSocketBroker:
|
||||
"""
|
||||
if client_id:
|
||||
message._sender_client_id = client_id
|
||||
conn = self._get_connection(tenant_id, client_id)
|
||||
if conn is not None:
|
||||
conn['last_seen_at'] = time.time()
|
||||
|
||||
db = get_db()
|
||||
|
||||
|
||||
Reference in new issue
Block a user