new tasks, devnet topup, cli, new model support
This commit is contained in:
@@ -57,7 +57,9 @@ class RelayServer:
|
||||
self._ready = threading.Event()
|
||||
self._running = False
|
||||
self._stop_event: asyncio.Event | None = None
|
||||
self._pending_rpc: dict[str, asyncio.Future] = {}
|
||||
# request_id → queue of relay-http-response frames (US-036: a streamed
|
||||
# response is a sequence of frames; a frame without "stream" is terminal).
|
||||
self._pending_rpc: dict[str, asyncio.Queue] = {}
|
||||
|
||||
@property
|
||||
def registry(self) -> PeerRegistry:
|
||||
@@ -172,9 +174,9 @@ class RelayServer:
|
||||
if topic == "relay-http-response":
|
||||
payload = envelope.get("payload", {})
|
||||
request_id = payload.get("request_id")
|
||||
fut = self._pending_rpc.pop(request_id, None)
|
||||
if fut is not None and not fut.done():
|
||||
fut.set_result(payload)
|
||||
queue = self._pending_rpc.get(request_id)
|
||||
if queue is not None:
|
||||
queue.put_nowait(payload)
|
||||
continue
|
||||
|
||||
# Fan out to all other registered peers
|
||||
@@ -240,8 +242,12 @@ class RelayServer:
|
||||
request_id = str(payload.get("request_id") or uuid.uuid4())
|
||||
payload["request_id"] = request_id
|
||||
payload["target_peer"] = target_peer_id
|
||||
fut = self._loop.create_future() if self._loop is not None else asyncio.get_running_loop().create_future()
|
||||
self._pending_rpc[request_id] = fut
|
||||
queue: asyncio.Queue = asyncio.Queue()
|
||||
self._pending_rpc[request_id] = queue
|
||||
overall_timeout = 310.0
|
||||
idle_timeout = 120.0
|
||||
loop = asyncio.get_running_loop()
|
||||
deadline = loop.time() + overall_timeout
|
||||
try:
|
||||
await target.ws.send(json.dumps({
|
||||
"topic": "relay-http-request",
|
||||
@@ -249,8 +255,19 @@ class RelayServer:
|
||||
"from_peer": "relay",
|
||||
"payload": payload,
|
||||
}))
|
||||
response = await asyncio.wait_for(fut, timeout=310.0)
|
||||
await ws_requester.send(json.dumps(response))
|
||||
# Forward frames until a terminal one: streamed responses (US-036)
|
||||
# end with {"stream": true, "done": true}; a frame without "stream"
|
||||
# is a complete legacy single response.
|
||||
while True:
|
||||
remaining = deadline - loop.time()
|
||||
if remaining <= 0:
|
||||
raise asyncio.TimeoutError
|
||||
frame = await asyncio.wait_for(
|
||||
queue.get(), timeout=min(idle_timeout, remaining)
|
||||
)
|
||||
await ws_requester.send(json.dumps(frame))
|
||||
if not frame.get("stream") or frame.get("done"):
|
||||
break
|
||||
except asyncio.TimeoutError:
|
||||
await ws_requester.send(json.dumps({
|
||||
"request_id": request_id,
|
||||
|
||||
Reference in New Issue
Block a user