async def _throttled_request(self, request): '''Process a single request, respecting the concurrency limit.''' try: timeout = self.processing_timeout async with timeout_after(timeout): async with self._incoming_concurrency: if self._cost_fraction: await sleep(self._cost_fraction * self.cost_sleep) result = await self.handle_request(request) except (ProtocolError, RPCError) as e: result = e except TaskTimeout: self.logger.info( f'incoming request {request} timed out after {timeout} secs') result = RPCError(JSONRPC.SERVER_BUSY, 'server busy - request timed out') except ExcessiveSessionCostError: result = FinalRPCError(JSONRPC.EXCESSIVE_RESOURCE_USAGE, 'excessive resource usage') except CancelledError: raise except Exception: self.logger.exception(f'exception handling {request}') result = RPCError(JSONRPC.INTERNAL_ERROR, 'internal server error') if isinstance(request, Request): message = request.send_result(result) if message: await self._send_message(message) if isinstance(result, Exception): self._bump_errors() if isinstance(result, FinalRPCError): # Don't await self.close() because that is self-cancelling self._close()
async def _throttled_request(self, request): '''Process a single request, respecting the concurrency limit.''' disconnect = False try: timeout = self.processing_timeout async with timeout_after(timeout): async with self._incoming_concurrency: if self._cost_fraction: await sleep(self._cost_fraction * self.cost_sleep) result = await self.handle_request(request) except (ProtocolError, RPCError) as e: result = e except TaskTimeout: self.logger.info( f'incoming request {request} timed out after {timeout} secs') result = RPCError(JSONRPC.SERVER_BUSY, 'server busy - request timed out') except ReplyAndDisconnect as e: result = e.args[0] disconnect = True except ExcessiveSessionCostError: self.on_disconnect_due_to_excessive_session_cost() result = RPCError(JSONRPC.EXCESSIVE_RESOURCE_USAGE, 'excessive resource usage') disconnect = True except Exception: # pylint:disable=W0703 self.logger.exception(f'exception handling {request}') result = RPCError(JSONRPC.INTERNAL_ERROR, 'internal server error') if isinstance(request, Request): message = request.send_result(result) if message: await self._send_message(message) if isinstance(result, Exception): self._bump_errors(result) if disconnect: await self.close()
async def _throttled_request(self, request): '''Process a single request, respecting the concurrency limit.''' async with self._concurrency.semaphore: try: result = await self.handle_request(request) except (ProtocolError, RPCError) as e: result = e except CancelledError: raise except Exception: self.logger.exception(f'exception handling {request}') result = RPCError(JSONRPC.INTERNAL_ERROR, 'internal server error') if isinstance(request, Request): message = request.send_result(result) if message: await self._send_message(message) if isinstance(result, Exception): self._bump_errors()