Esempio n. 1
0
    def send_request_message(self,
                             request_id,
                             meta,
                             body,
                             message_expiry_in_seconds=None):
        # type: (int, Dict[six.text_type, Any], Dict[six.text_type, Any], Optional[int]) -> None
        meta['reply_to'] = '{receive_queue_name}{thread_id}'.format(
            receive_queue_name=self._receive_queue_name,
            thread_id=get_hex_thread_id(),
        )

        with self.metrics.timer('client.transport.redis_gateway.send',
                                resolution=TimerResolution.MICROSECONDS):
            try:
                self.core.send_message(self._send_queue_name, request_id, meta,
                                       body, message_expiry_in_seconds)
                # If we increment this before sending and sending fails, the client will be broken forever, so only
                # increment when sending succeeds.
                self._requests_outstanding += 1
            except TransientPySOATransportError:
                self._previous_error_was_transport_problem = True
                self.metrics.counter(
                    'client.transport.redis_gateway.send.error.transient'
                ).increment()
                raise
Esempio n. 2
0
 def receive_response_message(self, receive_timeout_in_seconds=None):
     # type: (Optional[int]) -> ReceivedMessage
     if self._requests_outstanding > 0:
         with self.metrics.timer('client.transport.redis_gateway.receive',
                                 resolution=TimerResolution.MICROSECONDS):
             try:
                 received_message = self.core.receive_message(
                     '{receive_queue_name}{thread_id}'.format(
                         receive_queue_name=self._receive_queue_name,
                         thread_id=get_hex_thread_id(),
                     ),
                     receive_timeout_in_seconds,
                 )
             except MessageReceiveTimeout:
                 if self._previous_error_was_transport_problem:
                     # We're almost certainly recovering from a failover
                     self._requests_outstanding = 0
                     self._previous_error_was_transport_problem = False
                 self.metrics.counter(
                     'client.transport.redis_gateway.receive.error.timeout'
                 ).increment()
                 raise
             except TransientPySOATransportError:
                 self._previous_error_was_transport_problem = True
                 self.metrics.counter(
                     'client.transport.redis_gateway.receive.error.transient'
                 ).increment()
                 raise
         self._requests_outstanding -= 1
         return received_message
     else:
         self._previous_error_was_transport_problem = False
         # This tells Client.get_all_responses to stop waiting for more.
         return ReceivedMessage(None, None, None)
Esempio n. 3
0
    def send_request_message(self, request_id, meta, body, message_expiry_in_seconds=None):
        self._requests_outstanding += 1
        meta['reply_to'] = '{receive_queue_name}{thread_id}'.format(
            receive_queue_name=self._receive_queue_name,
            thread_id=get_hex_thread_id(),
        )

        with self.metrics.timer('client.transport.redis_gateway.send', resolution=TimerResolution.MICROSECONDS):
            self.core.send_message(self._send_queue_name, request_id, meta, body, message_expiry_in_seconds)
Esempio n. 4
0
 def receive_response_message(self, receive_timeout_in_seconds=None):
     if self._requests_outstanding > 0:
         with self.metrics.timer('client.transport.redis_gateway.receive', resolution=TimerResolution.MICROSECONDS):
             try:
                 request_id, meta, response = self.core.receive_message(
                     '{receive_queue_name}{thread_id}'.format(
                         receive_queue_name=self._receive_queue_name,
                         thread_id=get_hex_thread_id(),
                     ),
                     receive_timeout_in_seconds,
                 )
             except MessageReceiveTimeout:
                 self.metrics.counter('client.transport.redis_gateway.receive.error.timeout').increment()
                 raise
         self._requests_outstanding -= 1
         return request_id, meta, response
     else:
         # This tells Client.get_all_responses to stop waiting for more.
         return None, None, None
Esempio n. 5
0
    def test_send_request_message_another_service(self, mock_core):
        transport = self._get_transport('geo')

        request_id = random.randint(1, 1000)
        message = {'another': 'message'}

        transport.send_request_message(request_id, {}, message, 25)

        mock_core.return_value.send_message.assert_called_once_with(
            'service.geo',
            request_id,
            {
                'reply_to': 'service.geo.{client_id}!{thread_id}'.format(
                    client_id=transport.client_id,
                    thread_id=get_hex_thread_id(),
                ),
            },
            message,
            25,
        )
Esempio n. 6
0
    def test_send_request_message(self, mock_core):
        transport = self._get_transport()

        request_id = random.randint(1, 1000)
        meta = {'app': 'ppa'}
        message = {'test': 'payload'}

        transport.send_request_message(request_id, meta, message)

        mock_core.return_value.send_message.assert_called_once_with(
            'service.my_service',
            request_id,
            {
                'app': 'ppa',
                'reply_to': 'service.my_service.{client_id}!{thread_id}'.format(
                    client_id=transport.client_id,
                    thread_id=get_hex_thread_id(),
                ),
            },
            message,
            None,
        )
Esempio n. 7
0
    def test_receive_response_message(self, mock_core):
        transport = self._get_transport()
        transport._requests_outstanding = 1

        request_id = random.randint(1, 1000)
        meta = {'app': 'ppa'}
        message = {'test': 'payload'}

        mock_core.return_value.receive_message.return_value = request_id, meta, message

        response = transport.receive_response_message()

        self.assertEqual(request_id, response[0])
        self.assertEqual(meta, response[1])
        self.assertEqual(message, response[2])

        mock_core.return_value.receive_message.assert_called_once_with(
            'service.my_service.{client_id}!{thread_id}'.format(
                client_id=transport.client_id,
                thread_id=get_hex_thread_id(),
            ),
            None,
        )
Esempio n. 8
0
    def test_receive_response_message_another_service(self, mock_core):
        transport = self._get_transport('geo')
        transport._requests_outstanding = 1

        request_id = random.randint(1, 1000)
        meta = {}  # type: Dict[six.text_type, Any]
        message = {'another': 'message'}

        mock_core.return_value.receive_message.return_value = request_id, meta, message

        response = transport.receive_response_message(15)

        self.assertEqual(request_id, response[0])
        self.assertEqual(meta, response[1])
        self.assertEqual(message, response[2])

        mock_core.return_value.receive_message.assert_called_once_with(
            'service.geo.{client_id}!{thread_id}'.format(
                client_id=transport.client_id,
                thread_id=get_hex_thread_id(),
            ),
            15,
        )