예제 #1
0
파일: client.py 프로젝트: zeroshade/aredis
 def from_url(cls, url, db=None, skip_full_coverage_check=False, **kwargs):
     """
     Return a Redis client object configured from the given URL, which must
     use either `the ``redis://`` scheme
     <http://www.iana.org/assignments/uri-schemes/prov/redis>`_ for RESP
     connections or the ``unix://`` scheme for Unix domain sockets.
     For example::
     redis://[:password]@localhost:6379/0
     unix://[:password]@/path/to/socket.sock?db=0
     There are several ways to specify a database number. The parse function
     will return the first specified option:
     1. A ``db`` querystring option, e.g. redis://localhost?db=0
     2. If using the redis:// scheme, the path argument of the url, e.g.
     redis://localhost/0
     3. The ``db`` argument to this function.
     If none of these options are specified, db=0 is used.
     Any additional querystring arguments and keyword arguments will be
     passed along to the ConnectionPool class's initializer. In the case
     of conflicting arguments, querystring arguments always win.
     """
     connection_pool = ClusterConnectionPool.from_url(
         url,
         db=db,
         skip_full_coverage_check=skip_full_coverage_check,
         **kwargs)
     return cls(connection_pool=connection_pool)
예제 #2
0
파일: client.py 프로젝트: ysj123688/aredis
    def __init__(self, host=None, port=None, startup_nodes=None, max_connections=32,
                 max_connections_per_node=False, readonly=False,
                 reinitialize_steps=None, skip_full_coverage_check=False,
                 nodemanager_follow_cluster=False, **kwargs):
        """
        :startup_nodes:
        List of nodes that initial bootstrapping can be done from
        :host:
        Can be used to point to a startup node
        :port:
        Can be used to point to a startup node
        :max_connections:
        Maximum number of connections that should be kept open at one time
        :readonly:
        enable READONLY mode. You can read possibly stale data from slave.
        :skip_full_coverage_check:
        Skips the check of cluster-require-full-coverage config, useful for clusters
        without the CONFIG command (like aws)
        :nodemanager_follow_cluster:
        The node manager will during initialization try the last set of nodes that
        it was operating on. This will allow the client to drift along side the cluster
        if the cluster nodes move around alot.
        :**kwargs:
        Extra arguments that will be sent into StrictRedis instance when created
        (See Official redis-py doc for supported kwargs
        [https://github.com/andymccurdy/redis-py/blob/master/redis/client.py])
        Some kwargs is not supported and will raise RedisClusterException
        - db (Redis do not support database SELECT in cluster mode)
        """
        # Tweaks to StrictRedis client arguments when running in cluster mode
        if "db" in kwargs:
            raise RedisClusterException("Argument 'db' is not possible to use in cluster mode")
        if "connection_pool" in kwargs:
            pool = kwargs.pop('connection_pool')
        else:
            startup_nodes = [] if startup_nodes is None else startup_nodes

            # Support host/port as argument
            if host:
                startup_nodes.append({"host": host, "port": port if port else 7000})
            pool = ClusterConnectionPool(
                startup_nodes=startup_nodes,
                max_connections=max_connections,
                reinitialize_steps=reinitialize_steps,
                max_connections_per_node=max_connections_per_node,
                skip_full_coverage_check=skip_full_coverage_check,
                nodemanager_follow_cluster=nodemanager_follow_cluster,
                readonly=readonly,
                **kwargs
            )

        super(StrictRedisCluster, self).__init__(connection_pool=pool, **kwargs)

        self.refresh_table_asap = False
        self.nodes_flags = self.__class__.NODES_FLAGS.copy()
        self.result_callbacks = self.__class__.RESULT_CALLBACKS.copy()
        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
예제 #3
0
 async def get_pool(self, connection_kwargs=None, max_connections=None, startup_nodes=None):
     startup_nodes = startup_nodes or [{'host': '127.0.0.1', 'port': 7000}]
     connection_kwargs = connection_kwargs or {}
     pool = ClusterConnectionPool(
         max_connections=max_connections,
         startup_nodes=startup_nodes,
         readonly=True,
         **connection_kwargs)
     await pool.initialize()
     return pool
예제 #4
0
 async def get_pool(self, connection_kwargs=None, max_connections=None, max_connections_per_node=None,
                    connection_class=DummyConnection):
     connection_kwargs = connection_kwargs or {}
     pool = ClusterConnectionPool(
         connection_class=connection_class,
         max_connections=max_connections,
         max_connections_per_node=max_connections_per_node,
         startup_nodes=[{"host": "127.0.0.1", "port": 7000}],
         **connection_kwargs)
     await pool.initialize()
     return pool
예제 #5
0
 async def test_connection_idle_check(self, event_loop):
     pool = ClusterConnectionPool(startup_nodes=[dict(host='127.0.0.1', port=7000)],
                                max_idle_time=0.2, idle_check_interval=0.1)
     conn = pool.get_connection_by_node({
         'name': '127.0.0.1:7000',
         'host': '127.0.0.1',
         'port': 7000,
         'server_type': 'master',
     })
     name = conn.node['name']
     assert len(pool._in_use_connections[name]) == 1
     # not ket could be found in dict for now
     assert not pool._available_connections
     pool.release(conn)
     assert len(pool._in_use_connections[name]) == 0
     assert len(pool._available_connections[name]) == 1
     await asyncio.sleep(0.21)
     assert len(pool._in_use_connections[name]) == 0
     assert len(pool._available_connections[name]) == 0
     last_active_at = conn.last_active_at
     assert last_active_at == conn.last_active_at
     assert conn._writer is None and conn._reader is None