Process incoming requests directly from the socket.
(self)
| 333 | return True |
| 334 | |
| 335 | async def _process_requests(self) -> None: |
| 336 | """Process incoming requests directly from the socket.""" |
| 337 | assert self._client_socket is not None, "Client socket is not bound" |
| 338 | |
| 339 | while not self._shutdown_event.is_set(): |
| 340 | try: |
| 341 | #logger_debug(f"[server] Worker waiting for request", color="green") |
| 342 | # Read request directly from socket with timeout |
| 343 | req, routing_id = await asyncio.wait_for( |
| 344 | self._client_socket.get_async_noblock(return_identity=True), |
| 345 | timeout=2) |
| 346 | req.routing_id = routing_id |
| 347 | logger_debug(f"[server] Worker got request: {req}", |
| 348 | color="green") |
| 349 | except asyncio.TimeoutError: |
| 350 | continue |
| 351 | except asyncio.CancelledError: |
| 352 | logger_debug("[server] RPC worker cancelled") |
| 353 | break |
| 354 | except Exception as e: |
| 355 | if self._shutdown_event.is_set(): |
| 356 | break |
| 357 | logger.error(f"RPC worker caught an exception: {e}") |
| 358 | logger.error(traceback.format_exc()) |
| 359 | continue |
| 360 | |
| 361 | # shutdown methods depend on _num_pending_requests, so |
| 362 | # they should not be counted |
| 363 | if req.method_name not in ["_rpc_shutdown", "shutdown"]: |
| 364 | self._num_pending_requests += 1 |
| 365 | logger_debug( |
| 366 | f"[server] Worker received request {req}, pending: {self._num_pending_requests}" |
| 367 | ) |
| 368 | |
| 369 | # Check if we should cancel due to shutdown |
| 370 | if await self._handle_shutdown_request(req): |
| 371 | continue |
| 372 | |
| 373 | # Check if the method exists |
| 374 | if req.method_name not in self._functions: |
| 375 | logger.error( |
| 376 | f"Method '{req.method_name}' not found in RPC server.") |
| 377 | self._num_pending_requests -= 1 |
| 378 | |
| 379 | error = RPCStreamingError if req.is_streaming else RPCError |
| 380 | await self._send_error_response( |
| 381 | req, |
| 382 | error( |
| 383 | f"Method '{req.method_name}' not found in RPC server.", |
| 384 | traceback=traceback.format_exc())) |
| 385 | continue |
| 386 | |
| 387 | func = self._functions[req.method_name] |
| 388 | |
| 389 | # Final shutdown check before processing |
| 390 | if await self._handle_shutdown_request(req): |
| 391 | continue |
| 392 |
no test coverage detected