Redis Python中池中的默认连接数

时间:2019-05-08 11:46:33

标签: python redis

Python-3.7

Redis-2.10.6


我正在使用Redis创建连接池

redis_pool = redis.ConnectionPool(host=REDIS_URL, port=REDIS_PORT, decode_responses=True) 

我没有指定max_connections。在查看redis.ConnectionPool()的源代码时,

def __init__(self, connection_class=Connection, max_connections=None,
             **connection_kwargs):
    """
    Create a connection pool. If max_connections is set, then this
    object raises redis.ConnectionError when the pool's limit is reached.

    By default, TCP connections are created unless connection_class is
    specified. Use redis.UnixDomainSocketConnection for unix sockets.

    Any additional keyword arguments are passed to the constructor of
    connection_class.
    """
    max_connections = max_connections or 2 ** 31
    if not isinstance(max_connections, (int, long)) or max_connections < 0:
        raise ValueError('"max_connections" must be a positive integer')

    self.connection_class = connection_class
    self.connection_kwargs = connection_kwargs
    self.max_connections = max_connections

    self.reset()

我看到max_connections设置为2 ** 31 ,即2,147,483,648 (如果未设置,则为 )。这对我来说很奇怪。

Redis在池中维护的默认连接数是多少?最大值约为200万。因此,这意味着我们必须为此传递自己的实际价值。

1 个答案:

答案 0 :(得分:2)

Redis端不存在池,该类实际上只是Python端的self.connection_class实例的精美集合。

尽管您同意,但是2 ** 31的数字过大的可能性超过了99%。不过不要认为这太在意,因为初始化池不会创建任何连接(或为它们保留空间)。 max_connections仅限制_available_connections数组的边界,当需要连接但池中没有空闲的数组可立即使用时,数组会增大。

这里是ConnectionPool类的更多内容,并带有一些注释。

https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L967

def reset(self):
    self.pid = os.getpid()
    self._created_connections = 0
    self._available_connections = []  # <- starts empty
    self._in_use_connections = set()
    self._check_lock = threading.Lock()

https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L983

def get_connection(self, command_name, *keys, **options):
    "Get a connection from the pool"
    self._checkpid()
    try:
        connection = self._available_connections.pop()
    except IndexError:
        connection = self.make_connection()  # <- make a new conn only if _available_connections is tapped
    self._in_use_connections.add(connection)
    try:
        # ensure this connection is connected to Redis
        connection.connect()
        # connections that the pool provides should be ready to send
        # a command. if not, the connection was either returned to the
        # pool before all data has been read or the socket has been
        # closed. either way, reconnect and verify everything is good.
        if not connection.is_ready_for_command():
            connection.disconnect()
            connection.connect()
            if not connection.is_ready_for_command():
                raise ConnectionError('Connection not ready')
    except:  # noqa: E722
        # release the connection back to the pool so that we don't leak it
        self.release(connection)
        raise

    return connection

https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L1019

 def make_connection(self):
    "Create a new connection"
    if self._created_connections >= self.max_connections:  # <- where the bounding happens
        raise ConnectionError("Too many connections")
    self._created_connections += 1
    return self.connection_class(**self.connection_kwargs)

无论如何,我敢打赌,选择特定值是为了降低开发人员将池完全耗尽到接近0的可能性。请注意,连接对象非常轻巧,因此不可能有成千上万个数组使您的应用程序停止运行。实际上,这没有什么区别:大多数Redis呼叫返回得如此之快,以至于无论如何很难无意间并行地启动数百万个呼叫。 (如果您是故意这样做的,您可能知道足够多,可以根据自己的确切需求进行调整。;-)