Esempio n. 1
0
    def _send_and_receive(self,
                          request,
                          timeout=app.config.get('INSTANCE_TIMEOUT', 10000),
                          quiet=False,
                          **kwargs):
        logger = logging.getLogger(__name__)
        with self.socket(self.context) as socket:
            try:
                request.request_id = flask.request.id
            except RuntimeError:
                # we aren't in a flask context, so there is no request

                if 'flask_request_id' in kwargs:
                    request.request_id = kwargs['flask_request_id']
            socket.send(request.SerializeToString())
            if socket.poll(timeout=timeout) > 0:
                pb = socket.recv()
                resp = response_pb2.Response()
                resp.ParseFromString(pb)
                self.update_property(
                    resp
                )  # we update the timezone and geom of the instances at each request
                return resp
            else:
                socket.setsockopt(zmq.LINGER, 0)
                socket.close()
                if not quiet:
                    logger.error('request on %s failed: %s', self.socket_path,
                                 six.text_type(request))
                raise DeadSocketException(self.name, self.socket_path)
Esempio n. 2
0
    def _send_and_receive(self, request, quiet=False, **kwargs):
        logger = logging.getLogger(__name__)
        deadline = datetime.utcnow() + timedelta(milliseconds=self.timeout)
        request.deadline = deadline.strftime('%Y%m%dT%H%M%S,%f')

        with self.socket(self.context) as socket:
            if 'request_id' in kwargs and kwargs['request_id']:
                request.request_id = kwargs['request_id']
            else:
                try:
                    request.request_id = flask.request.id
                except RuntimeError:
                    # we aren't in a flask context, so there is no request
                    if 'flask_request_id' in kwargs and kwargs['flask_request_id']:
                        request.request_id = kwargs['flask_request_id']

            socket.send(request.SerializeToString())
            if socket.poll(timeout=self.timeout) > 0:
                pb = socket.recv()
                resp = response_pb2.Response()
                resp.ParseFromString(pb)
                return resp
            else:
                socket.setsockopt(zmq.LINGER, 0)
                socket.close()
                if not quiet:
                    logger.error('request on %s failed: %s', self.zmq_socket, six.text_type(request))
                raise DeadSocketException(self.name, self.zmq_socket)
Esempio n. 3
0
 def send_and_receive(self, *args, **kwargs):
     """
     encapsulate all call to kraken in a circuit breaker, this way we don't loose time calling dead instance
     """
     try:
         return self.breaker.call(self._send_and_receive, *args, **kwargs)
     except pybreaker.CircuitBreakerError as e:
         raise DeadSocketException(self.name, self.socket_path)