Example #1
0
    async def __call__(self, socket, addr):
        self.grpc_socket = GRPCProtoSocket(self.config, socket)
        await self.grpc_socket.initiate_connection()

        # TODO: TaskGroup() uses a lot of memory if the connection is kept for a long time
        # TODO: do we really need it here?
        # task_group = curio.TaskGroup()
        # TODO: Should at least pass through GeneratorExit
        try:
            async for stream in self.grpc_socket.listen():
                await curio.spawn(self.request_received(stream), daemon=True)
        except:
            logging.exception("Got exception in main dispatch loop")
        finally:
            # await task_group.join()
            await self.grpc_socket.shutdown()
Example #2
0
 async def __aenter__(self):
     await super().__aenter__()  # Does nothing
     socket = await anyio.connect_tcp(self._host, self._port,
                                      ssl_context=self._ssl,
                                      autostart_tls=self._ssl is not None,
                                      tls_standard_compatible=False)
     config = GRPCConfiguration(client_side=True)
     self._grpc_socket = await self.enter_async_context(GRPCProtoSocket(config, socket))
     return self
Example #3
0
 async def __call__(self, socket):
     async with GRPCProtoSocket(self.config, socket) as self.grpc_socket:
         # TODO: resource usage warning
         # TODO: TaskGroup() uses a lot of memory if the connection is kept for a long time
         # TODO: do we really need it here?
         async with anyio.create_task_group() as task_group:
             # TODO: Should at least pass through GeneratorExit
             try:
                 async for stream in self.grpc_socket.listen():
                     await task_group.spawn(self.request_received, stream)
             except:
                 logging.exception("Got exception in main dispatch loop")
Example #4
0
class ConnectionHandler:
    RECEIVE_BUFFER_SIZE = 65536

    def __init__(self, server: Server):
        self.config = GRPCConfiguration(client_side=False)
        self.grpc_socket = None
        self.server = server

    async def request_received(self, stream: GRPCProtoStream):
        await stream.start_response(stream.stream_id, "+proto")
        event = await stream.receive_event()

        # TODO: Should at least pass through GeneratorExit
        try:
            service = self.server.services[event.service_name]
            bound_rpc_method = service.methods[event.method_name]
            method_fn = bound_rpc_method.method_fn
            cardinality = bound_rpc_method.signature.cardinality
            stream.expect_message_type(bound_rpc_method.signature.request_type)
            if cardinality == Cardinality.STREAM_STREAM:
                await call_server_stream_stream(method_fn, stream)
            elif cardinality == Cardinality.UNARY_STREAM:
                await call_server_unary_stream(method_fn, stream)
            elif cardinality == Cardinality.STREAM_UNARY:
                await call_server_stream_unary(method_fn, stream)
            else:
                await call_server_unary_unary(method_fn, stream)
        except:
            logging.exception("Got exception while writing response stream")
            await stream.close(1, status_message=repr(sys.exc_info()))

    async def __call__(self, socket, addr):
        self.grpc_socket = GRPCProtoSocket(self.config, socket)
        await self.grpc_socket.initiate_connection()

        # TODO: TaskGroup() uses a lot of memory if the connection is kept for a long time
        # TODO: do we really need it here?
        # task_group = curio.TaskGroup()
        # TODO: Should at least pass through GeneratorExit
        try:
            async for stream in self.grpc_socket.listen():
                await curio.spawn(self.request_received(stream), daemon=True)
        except:
            logging.exception("Got exception in main dispatch loop")
        finally:
            # await task_group.join()
            await self.grpc_socket.shutdown()
Example #5
0
 async def connect(self):
     socket = await curio.open_connection(self.host, self.port)
     config = GRPCConfiguration(client_side=True)
     self.grpc_socket = GRPCProtoSocket(config, socket)
     await self.grpc_socket.initiate_connection()