Skip to content

Commit be98308

Browse files
committed
Extract manifest_id / user_id from websocket headers
Use headers instead of sniffing websocket messages.
1 parent ece07a0 commit be98308

1 file changed

Lines changed: 22 additions & 71 deletions

File tree

src/scope/cloud/livepeer_fal_app.py

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -273,91 +273,26 @@ def _build_runner_command() -> list[str]:
273273
]
274274

275275

276-
async def _handle_first_client_text(text_data: str, metadata: dict | None) -> None:
277-
"""Parse the first client text message, print manifest_id, publish connected.
278-
279-
Best-effort — any parsing or publishing failure is logged with the
280-
``[KAFKA-DEBUG]`` prefix but never raises, so the proxy loop is unaffected.
281-
"""
282-
import json
283-
284-
if metadata is None:
285-
return
286-
287-
try:
288-
parsed = json.loads(text_data)
289-
except (json.JSONDecodeError, TypeError) as exc:
290-
print(f"[KAFKA-DEBUG] First client text not JSON: {exc}")
291-
return
292-
293-
manifest_id = parsed.get("manifest_id") if isinstance(parsed, dict) else None
294-
params = parsed.get("params") if isinstance(parsed, dict) else None
295-
user_id = params.get("daydream_user_id") if isinstance(params, dict) else None
296-
297-
metadata["manifest_id"] = manifest_id
298-
metadata["user_id"] = user_id
299-
300-
print(
301-
f"[KAFKA-DEBUG] First client message parsed manifest_id={manifest_id} "
302-
f"user_id={user_id}"
303-
)
304-
305-
if kafka_publisher is None or not kafka_publisher.is_running:
306-
print("[KAFKA-DEBUG] Skipping websocket_connected: Kafka not running")
307-
return
308-
309-
connection_info = metadata.get("connection_info") or {}
310-
ok = await kafka_publisher.publish(
311-
"websocket_connected",
312-
{
313-
"user_id": user_id,
314-
"connection_id": manifest_id,
315-
"connection_info": connection_info,
316-
},
317-
)
318-
print(f"[KAFKA-DEBUG] websocket_connected publish result: ok={ok}")
319-
320-
321-
async def _proxy_ws(client_ws: WebSocket, metadata: dict | None = None) -> None:
276+
async def _proxy_ws(client_ws: WebSocket) -> None:
322277
"""Connect to the local runner and proxy traffic bidirectionally.
323278
324-
If *metadata* is provided, the first client→runner text message is
325-
parsed (best-effort) to extract ``manifest_id`` and ``user_id`` and a
326-
``websocket_connected`` Kafka event is published inline.
327-
328279
Raises WebSocketDisconnect if the client disconnects.
329280
Returns normally if the runner connection drops.
330281
"""
331282

332283
import websockets
333284
from websockets.exceptions import ConnectionClosed
334285

335-
# NOTE: Previously we sniffed the two-message handshake explicitly
336-
# (recv runner ready, then receive client job_info) before starting
337-
# the parallel proxy loop. That is disabled here for debugging —
338-
# keep the proxy transparent and sniff inline in client_to_runner.
339-
#
340-
# async with websockets.connect(RUNNER_LOCAL_WS_URL) as runner_ws:
341-
# ready_msg = await runner_ws.recv()
342-
# ...
343-
# job_msg = await client_ws.receive()
344-
# ...
345-
346286
async with websockets.connect(RUNNER_LOCAL_WS_URL) as runner_ws:
347-
first_client_text_seen = False
348287

349288
async def client_to_runner() -> None:
350-
nonlocal first_client_text_seen
351289
while True:
352290
message = await client_ws.receive()
353291
msg_type = message.get("type")
354292
if msg_type == "websocket.receive":
355293
text_data = message.get("text")
356294
bytes_data = message.get("bytes")
357295
if text_data is not None:
358-
if not first_client_text_seen:
359-
first_client_text_seen = True
360-
await _handle_first_client_text(text_data, metadata)
361296
await runner_ws.send(text_data)
362297
elif bytes_data is not None:
363298
await runner_ws.send(bytes_data)
@@ -533,6 +468,14 @@ async def websocket_handler(self, client_ws: WebSocket) -> None:
533468

534469
connection_start_time = time.time()
535470
metadata: dict = {}
471+
manifest_id = client_ws.headers.get("manifest-id")
472+
user_id = client_ws.headers.get("daydream-user-id")
473+
metadata["manifest_id"] = manifest_id
474+
metadata["user_id"] = user_id
475+
print(
476+
f"[KAFKA-DEBUG] Handshake headers manifest_id={manifest_id} "
477+
f"user_id={user_id}"
478+
)
536479

537480
import json
538481

@@ -551,6 +494,18 @@ async def websocket_handler(self, client_ws: WebSocket) -> None:
551494
"fal_log_labels": fal_log_labels,
552495
}
553496
metadata["connection_info"] = connection_info
497+
if kafka_publisher is None or not kafka_publisher.is_running:
498+
print("[KAFKA-DEBUG] Skipping websocket_connected: Kafka not running")
499+
else:
500+
ok = await kafka_publisher.publish(
501+
"websocket_connected",
502+
{
503+
"user_id": user_id,
504+
"connection_id": manifest_id,
505+
"connection_info": connection_info,
506+
},
507+
)
508+
print(f"[KAFKA-DEBUG] websocket_connected publish result: ok={ok}")
554509

555510
# Ensure any previous session data is cleaned up
556511
event = _get_cleanup_event()
@@ -563,7 +518,7 @@ async def websocket_handler(self, client_ws: WebSocket) -> None:
563518
while True:
564519
print(f"Connecting proxy to runner websocket at {RUNNER_LOCAL_WS_URL}")
565520
try:
566-
await _proxy_ws(client_ws, metadata=metadata)
521+
await _proxy_ws(client_ws)
567522
except (
568523
ConnectionClosed,
569524
InvalidStatus,
@@ -572,10 +527,6 @@ async def websocket_handler(self, client_ws: WebSocket) -> None:
572527
) as exc:
573528
print(f"Livepeer fal ws runner connection failed: {exc}")
574529

575-
# websocket_connected is published inline from
576-
# _handle_first_client_text the first time the client sends a
577-
# text message through the proxy — no post-proxy publish here.
578-
579530
now = time.monotonic()
580531
cutoff = now - RUNNER_FAILURE_WINDOW_SECONDS
581532
failure_timestamps.append(now)

0 commit comments

Comments
 (0)