【Engineering】连接池(Connection Pool)

Posted by 西维蜀黍 on 2020-04-21, Last Modified on 2023-07-18

Configs

Unused timeout / IdleTimeout

为了让 middleware(比如 Redis)主动清理长时间空闲的客户端连接,回收资源,您可以设置timeout参数来限制客户端连接的空闲时间。

默认设置下,即使某个客户端已经空闲了很长时间,Redis也不会主动断开与该客户端的连接。但在业务核心应用中,建议配置timeout参数以使Redis具有主动回收资源的能力。否则,如果客户端出现异常,连接池资源将无法及时回收,可能因空闲连接占满连接池导致服务崩溃。核心应用出现这样的问题可能引发整个业务的混乱,后果严重。

timeout参数值的取值范围为[0,100000],默认为0(表示无限制),单位为秒(s)。在实际运行中,为了提高性能,Redis不一定会精确地按照timeout参数值的时间来断开符合条件的空闲连接,例如设置timeout参数值为10s,但空闲连接可能在12s后,服务器中新增很多连接时才会被断开。如需降低这个延迟,可适当增大hz参数的值,提高负责断开超时连接等定时任务的运行频率。

Connection timeout / PoolTimeout

Specifies the interval, in seconds, after which a connection request times out and a ConnectionWaitTimeoutException is thrown.

This value indicates the number of seconds that a connection request waits when there are no connections available in the free pool and no new connections can be created. This usually occurs because the maximum value of connections in the particular connection pool has been reached.

For example, if Connection timeout is set to 300, and the maximum number of connections are all in use, the pool manager waits for 300 seconds for a physical connection to become available. If a physical connection is not available within this time, the pool manager initiates a ConnectionWaitTimeout exception.

If the Connection timeout is set to 0, the pool manager waits as long as necessary until a connection becomes available. This happens when the application completes a transaction and returns a connection to the pool, or when the number of connections is less than the value of Maximum Connections, and a new physical connection is created.

If Maximum Connections is set to 0, an infinite number of physical connections are enabled, and the Connection timeout value is ignored.

Pool Size

The maximum number of socket connections.

E..g, default is 10 connections.

Ref