feat(perception): qualify 1x realtime replay envelope
This commit is contained in:
@@ -78,15 +78,15 @@ def build_live_perception_shadow_router(
|
||||
|
||||
await websocket.accept()
|
||||
client_event = asyncio.create_task(websocket.receive())
|
||||
ingress_event = asyncio.create_task(
|
||||
asyncio.to_thread(
|
||||
ingress.take_next,
|
||||
consumer_id,
|
||||
timeout=0.5,
|
||||
)
|
||||
)
|
||||
try:
|
||||
while True:
|
||||
ingress_event = asyncio.create_task(
|
||||
asyncio.to_thread(
|
||||
ingress.take_next,
|
||||
consumer_id,
|
||||
timeout=0.5,
|
||||
)
|
||||
)
|
||||
completed, _ = await asyncio.wait(
|
||||
{client_event, ingress_event},
|
||||
return_when=asyncio.FIRST_COMPLETED,
|
||||
@@ -94,15 +94,9 @@ def build_live_perception_shadow_router(
|
||||
if client_event in completed:
|
||||
message = client_event.result()
|
||||
if message.get("type") == "websocket.disconnect":
|
||||
ingress_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await ingress_event
|
||||
break
|
||||
result = message.get("bytes")
|
||||
if not isinstance(result, bytes) or result_receiver is None:
|
||||
ingress_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await ingress_event
|
||||
await websocket.close(
|
||||
code=1008,
|
||||
reason="Shadow result direction is unavailable",
|
||||
@@ -111,32 +105,35 @@ def build_live_perception_shadow_router(
|
||||
try:
|
||||
await asyncio.to_thread(result_receiver, result)
|
||||
except (RuntimeError, ValueError):
|
||||
ingress_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await ingress_event
|
||||
await websocket.close(
|
||||
code=1008,
|
||||
reason="Shadow result contract is invalid",
|
||||
)
|
||||
return
|
||||
client_event = asyncio.create_task(websocket.receive())
|
||||
if ingress_event not in completed:
|
||||
ingress_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await ingress_event
|
||||
continue
|
||||
event = await ingress_event
|
||||
if event is None:
|
||||
if ingress.snapshot()["closed"]:
|
||||
break
|
||||
continue
|
||||
await websocket.send_bytes(event.wire_bytes())
|
||||
if ingress_event in completed:
|
||||
event = ingress_event.result()
|
||||
if event is None:
|
||||
if ingress.snapshot()["closed"]:
|
||||
break
|
||||
else:
|
||||
await websocket.send_bytes(event.wire_bytes())
|
||||
ingress_event = asyncio.create_task(
|
||||
asyncio.to_thread(
|
||||
ingress.take_next,
|
||||
consumer_id,
|
||||
timeout=0.5,
|
||||
)
|
||||
)
|
||||
except (WebSocketDisconnect, RuntimeError):
|
||||
return
|
||||
finally:
|
||||
client_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await client_event
|
||||
ingress_event.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await ingress_event
|
||||
ingress.close_consumer(consumer_id)
|
||||
with suppress(RuntimeError):
|
||||
await websocket.close()
|
||||
|
||||
Reference in New Issue
Block a user