Пример #1
0
    async def send(self, message_type, message_content, timeout=None):
        correlation_id = uuid.uuid4().hex

        self._msg_router.expect_reply(correlation_id)

        message = Message(correlation_id=correlation_id,
                          content=message_content,
                          message_type=message_type)

        # Send the message. Backoff and retry in case of an error
        # We want a short backoff and retry attempt, so use the defaults
        # of 3 retries with 200ms of backoff
        backoff = _Backoff(max_retries=3,
                           interval=200,
                           error=SendBackoffTimeoutError())

        while True:
            try:
                await self._socket.send_multipart(
                    [message.SerializeToString()])
                break
            except asyncio.CancelledError:  # pylint: disable=try-except-raise
                raise
            except zmq.error.Again as e:
                await backoff.do_backoff(err_msg=repr(e))

        return await self._msg_router.await_reply(correlation_id,
                                                  timeout=timeout)
Пример #2
0
    async def send(self, message_type, message_content, timeout=None):
        correlation_id = uuid.uuid4().hex

        self._msg_router.expect_reply(correlation_id)

        message = Message(correlation_id=correlation_id,
                          content=message_content,
                          message_type=message_type)

        try:
            await self._socket.send_multipart([message.SerializeToString()])
        except asyncio.CancelledError:
            return None

        return await self._msg_router.await_reply(correlation_id,
                                                  timeout=timeout)
Пример #3
0
    async def start(self):
        """Starts receiving messages on the underlying socket and passes them
        to the message router.
        """
        self._is_running = True

        while self._is_running:
            try:
                zmq_msg = await self._socket.recv_multipart()

                message = Message()
                message.ParseFromString(zmq_msg[-1])

                await self._msg_router.route_msg(message)
            except DecodeError as e:
                LOGGER.warning('Unable to decode: %s', e)
            except zmq.ZMQError as e:
                LOGGER.warning('Unable to receive: %s', e)
                return
            except asyncio.CancelledError:
                self._is_running = False
Пример #4
0
    async def send(self, message_type, message_content, timeout):
        """Replaces send method on Connection. Should not be called directly.
        """
        request = self._request_proto()
        request.ParseFromString(message_content)

        self._sent_request_type = message_type
        self._sent_request = request

        try:
            response_bytes = self._response.pop().SerializeToString()
        except AttributeError:
            raise AssertionError("Preset a response before sending a request!")

        return Message(content=response_bytes)