Example #1
0
    def deliver(self, timeout=None):
        """
        If fail_on_error was set to False, a list of (success, response)
        tuples will be returned.  If success is False, response will be
        an Exception.  Otherwise, response will be the normal query response.

        If fail_on_error was left as True and one of the requests
        failed, the corresponding Exception will be raised. Otherwise,
        the normal response will be returned.
        """
        self.event.wait(timeout)
        if self.error:
            raise self.error
        elif not self.event.is_set():
            raise OperationTimedOut()
        else:
            return self.responses
Example #2
0
    def wait_for_responses(self, *msgs, **kwargs):
        """
        Returns a list of (success, response) tuples.  If success
        is False, response will be an Exception.  Otherwise, response
        will be the normal query response.

        If fail_on_error was left as True and one of the requests
        failed, the corresponding Exception will be raised.
        """
        if self.is_closed or self.is_defunct:
            raise ConnectionShutdown("Connection %s is already closed" % (self, ))
        timeout = kwargs.get('timeout')
        fail_on_error = kwargs.get('fail_on_error', True)
        waiter = ResponseWaiter(self, len(msgs), fail_on_error)

        # busy wait for sufficient space on the connection
        messages_sent = 0
        while True:
            needed = len(msgs) - messages_sent
            with self.lock:
                available = min(needed, self.max_request_id - self.in_flight + 1)
                request_ids = [self.get_request_id() for _ in range(available)]
                self.in_flight += available

            for i, request_id in enumerate(request_ids):
                self.send_msg(msgs[messages_sent + i],
                              request_id,
                              partial(waiter.got_response, index=messages_sent + i))
            messages_sent += available

            if messages_sent == len(msgs):
                break
            else:
                if timeout is not None:
                    timeout -= 0.01
                    if timeout <= 0.0:
                        raise OperationTimedOut()
                time.sleep(0.01)

        try:
            return waiter.deliver(timeout)
        except OperationTimedOut:
            raise
        except Exception as exc:
            self.defunct(exc)
            raise
Example #3
0
 def factory(cls, host, timeout, *args, **kwargs):
     """
     A factory function which returns connections which have
     succeeded in connecting and are ready for service (or
     raises an exception otherwise).
     """
     conn = cls(host, *args, **kwargs)
     conn.connected_event.wait(timeout)
     if conn.last_error:
         if conn.is_unsupported_proto_version:
             raise ProtocolVersionUnsupported(host, conn.protocol_version)
         raise conn.last_error
     elif not conn.connected_event.is_set():
         conn.close()
         raise OperationTimedOut(
             "Timed out creating connection (%s seconds)" % timeout)
     else:
         return conn
Example #4
0
    def __init__(self, *args, **kwargs):

        self._loop_thread.maybe_start()
        self._loop = self._loop_thread._loop
        super().__init__(*args, **kwargs)

        self.connected_event = Event()
        self._loop_connected_event = Event()

        self._reader = None
        self._writer = None

        self._callbacks = {}
        self._loop.call_soon_threadsafe(asyncio.async, self.add_connection())
        self._loop_connected_event.wait(5)
        if not self._loop_connected_event.is_set():
            raise OperationTimedOut("Timed out creating connection")

        self._send_options_message()
        self._loop.call_soon_threadsafe(asyncio.async, self.handle_read())
Example #5
0
    def wait_for_responses(self, *msgs, **kwargs):
        if self.is_closed or self.is_defunct:
            raise ConnectionShutdown("Connection %s is already closed" %
                                     (self, ))
        timeout = kwargs.get('timeout')
        waiter = ResponseWaiter(self, len(msgs))

        # busy wait for sufficient space on the connection
        messages_sent = 0
        while True:
            needed = len(msgs) - messages_sent
            with self.lock:
                available = min(needed, self.max_request_id - self.in_flight)
                request_ids = [self.get_request_id() for _ in range(available)]
                self.in_flight += available

            for i, request_id in enumerate(request_ids):
                self.send_msg(
                    msgs[messages_sent + i], request_id,
                    partial(waiter.got_response, index=messages_sent + i))
            messages_sent += available

            if messages_sent == len(msgs):
                break
            else:
                if timeout is not None:
                    timeout -= 0.01
                    if timeout <= 0.0:
                        raise OperationTimedOut()
                time.sleep(0.01)

        try:
            return waiter.deliver(timeout)
        except OperationTimedOut:
            raise
        except Exception as exc:
            self.defunct(exc)
            raise
Example #6
0
    def wait_for_responses(self, *msgs, **kwargs):
        if self.is_closed or self.is_defunct:
            raise ConnectionShutdown("Connection %s is already closed" %
                                     (self, ))
        timeout = kwargs.get('timeout')
        waiter = ResponseWaiter(self, len(msgs))

        # busy wait for sufficient space on the connection
        messages_sent = 0
        while True:
            needed = len(msgs) - messages_sent
            with self.lock:
                available = min(needed,
                                MAX_STREAM_PER_CONNECTION - self.in_flight)
                self.in_flight += available

            for i in range(messages_sent, messages_sent + available):
                self.send_msg(msgs[i],
                              partial(waiter.got_response, index=i),
                              wait_for_id=True)
            messages_sent += available

            if messages_sent == len(msgs):
                break
            else:
                if timeout is not None:
                    timeout -= 0.01
                    if timeout <= 0.0:
                        raise OperationTimedOut()
                time.sleep(0.01)

        try:
            return waiter.deliver(timeout)
        except OperationTimedOut:
            raise
        except Exception as exc:
            self.defunct(exc)
            raise
 def bad_wait_for_responses(*args, **kwargs):
     self.time.sleep(kwargs['timeout'])
     raise OperationTimedOut()
 def bad_wait_for_responses(*args, **kwargs):
     self.assertEqual(kwargs['timeout'],
                      self.control_connection._timeout)
     raise OperationTimedOut()