コード例 #1
0
    def establish_request_connection(self, current_request):
        """Return a live connection for the given hash"""
        # We'll keep track of the connections we're attempting to use so if we ever have to retry, we can use this history
        rotating_connections = self.request_to_rotating_connection_queue.get(
            current_request, None)
        if not rotating_connections:
            shuffled_connection_list = list(self.connection_list)
            random.shuffle(shuffled_connection_list)

            rotating_connections = collections.deque(shuffled_connection_list)
            self.request_to_rotating_connection_queue[
                current_request] = rotating_connections

        failed_connections = 0
        chosen_connection = None
        for possible_connection in rotating_connections:
            try:
                chosen_connection = self.establish_connection(
                    possible_connection)
                break
            except ConnectionError:
                # Rotate our server list so we'll skip all our broken servers
                failed_connections += 1

        if not chosen_connection:
            raise ServerUnavailable('Found no valid connections: %r' %
                                    self.connection_list)

        # Rotate our server list so we'll skip all our broken servers
        rotating_connections.rotate(-failed_connections)
        return chosen_connection
コード例 #2
0
    def poll_connections_until_stopped(self, submitted_connections, callback_fxn, timeout=None):
        """Continue to poll our connections until we receive a stopping condition"""
        stopwatch = gearman.util.Stopwatch(timeout)

        any_activity = False
        callback_ok = callback_fxn(any_activity)
        connection_ok = compat.any(current_connection.connected for current_connection in submitted_connections)

        while connection_ok and callback_ok:
            time_remaining = stopwatch.get_time_remaining()
            if time_remaining == 0.0:
                break

            # Do a single robust select and handle all connection activity
            read_connections, write_connections, dead_connections = self.poll_connections_once(submitted_connections, timeout=time_remaining)
            self.handle_connection_activity(read_connections, write_connections, dead_connections)

            any_activity = compat.any([read_connections, write_connections, dead_connections])

            callback_ok = callback_fxn(any_activity)
            connection_ok = compat.any(current_connection.connected for current_connection in submitted_connections)

        # We should raise here if we have no alive connections (don't go into a select polling loop with no connections)
        if not connection_ok:
            raise ServerUnavailable('Found no valid connections in list: %r' % self.connection_list)

        return bool(connection_ok and callback_ok)
コード例 #3
0
    def establish_admin_connection(self):
        try:
            self.establish_connection(self.current_connection)
        except ConnectionError:
            raise ServerUnavailable('Found no valid connections in list: %r' % self.connection_list)

        self.current_handler = self.connection_to_handler_map[self.current_connection]
コード例 #4
0
    def __init__(self,
                 host=None,
                 port=DEFAULT_GEARMAN_PORT,
                 keyfile=None,
                 certfile=None,
                 ca_certs=None,
                 keepalive=False,
                 keepintvl=None,
                 keepcnt=None,
                 keepidle=None):
        port = port or DEFAULT_GEARMAN_PORT
        self.gearman_host = host
        self.gearman_port = port
        self.keyfile = keyfile
        self.certfile = certfile
        self.ca_certs = ca_certs

        # These options are described in more detail in the tcp(7) man page
        # for the TCP_KEEPINTVL, TCP_KEEPCNT, and TCP_KEEPIDLE socket options.
        # Default values can be found in /proc/sys/net/ipv4 in the tcp_keep* files.
        # NOTE: May not work on all systems.
        self.keepalive = keepalive  # turn KEEPALIVE on or off
        self.keepintvl = keepintvl  # seconds b/w TCP keep-alive probes
        self.keepcnt = keepcnt  # max probes to send before killing connection
        self.keepidle = keepidle  # seconds of idle time before sending probes

        if host is None:
            raise ServerUnavailable("No host specified")

        # All 3 files must be given before SSL can be used
        self.use_ssl = False
        if all([self.keyfile, self.certfile, self.ca_certs]):
            self.use_ssl = True

        self._reset_connection()
コード例 #5
0
    def poll_connections_until_stopped(self,
                                       submitted_connections,
                                       callback_fxn,
                                       timeout=None,
                                       prehandle=None):
        """Continue to poll our connections until we receive a stopping condition"""
        stopwatch = gearman.util.Stopwatch(timeout)
        submitted_connections = set(submitted_connections)
        connection_map = {}

        any_activity = False
        callback_ok = callback_fxn(any_activity)
        connection_ok = compat.any(
            current_connection.connected
            for current_connection in submitted_connections)
        poller = gearman.io.get_connection_poller()
        if connection_ok:
            self._register_connections_with_poller(submitted_connections,
                                                   poller)
            connection_map = dict([(c.fileno(), c)
                                   for c in submitted_connections
                                   if c.connected])

        while connection_ok and callback_ok:
            time_remaining = stopwatch.get_time_remaining()
            if time_remaining == 0.0:
                break

            # Do a single robust select and handle all connection activity
            read_connections, write_connections, dead_connections = self.poll_connections_once(
                poller, connection_map, timeout=time_remaining)

            if prehandle:
                prehandle(read_connections, write_connections,
                          dead_connections)

            # Handle reads and writes and close all of the dead connections
            read_connections, write_connections, dead_connections = self.handle_connection_activity(
                read_connections, write_connections, dead_connections)

            any_activity = compat.any(
                [read_connections, write_connections, dead_connections])

            # Do not retry dead connections on the next iteration of the loop, as we closed them in handle_error
            submitted_connections -= dead_connections

            callback_ok = callback_fxn(any_activity)
            connection_ok = compat.any(
                current_connection.connected
                for current_connection in submitted_connections)

        poller.close()

        # We should raise here if we have no alive connections (don't go into a select polling loop with no connections)
        if not connection_ok:
            raise ServerUnavailable('Found no valid connections in list: %r' %
                                    self.connection_list)

        return bool(connection_ok and callback_ok)
コード例 #6
0
    def __init__(self, host=None, port=DEFAULT_GEARMAN_PORT):
        port = port or DEFAULT_GEARMAN_PORT
        self.gearman_host = host
        self.gearman_port = port

        if host is None:
            raise ServerUnavailable("No host specified")

        self._reset_connection()
コード例 #7
0
    def __init__(self, host=None, port=DEFAULT_GEARMAN_PORT):
        port = port or DEFAULT_GEARMAN_PORT
        self.gearman_host = host
        self.gearman_port = port

        self.reconnect_timeout = RECONNECT_TIMEOUT_DEFAULT

        if host is None:
            raise ServerUnavailable("No host specified")

        self._reset_connection()
コード例 #8
0
ファイル: connection.py プロジェクト: masroore/python-gearman
    def __init__(self, host=None, port=DEFAULT_GEARMAN_PORT, keyfile=None, certfile=None, ca_certs=None):
        port = port or DEFAULT_GEARMAN_PORT
        self.gearman_host = host
        self.gearman_port = port
        self.keyfile = keyfile
        self.certfile = certfile
        self.ca_certs = ca_certs

        if host is None:
            raise ServerUnavailable("No host specified")

        # All 3 files must be given before SSL can be used
        self.use_ssl = False
        if all([self.keyfile, self.certfile, self.ca_certs]):
            self.use_ssl = True

        self._reset_connection()