SetMaxOpenConns and SetMaxIdleConns

huangapple go评论93阅读模式
英文:

SetMaxOpenConns and SetMaxIdleConns

问题

为什么会有SetMaxOpenConnsSetMaxIdleConns这两个函数?在文档中有如下说明:

SetMaxIdleConns

SetMaxIdleConns函数用于设置空闲连接池中的最大连接数。

如果MaxOpenConns大于0但小于新的MaxIdleConns,那么新的MaxIdleConns将被调整为与MaxOpenConns相同的限制。

如果n <= 0,则不会保留任何空闲连接。

SetMaxOpenConns

SetMaxOpenConns函数用于设置与数据库的最大打开连接数。

如果MaxIdleConns大于0且新的MaxOpenConns小于MaxIdleConns,那么MaxIdleConns将被调整为与新的MaxOpenConns相同的限制。

如果n <= 0,则对打开连接数没有限制。默认值为0(无限制)。

为什么会有这两个函数而不是一个函数来调整空闲连接和打开连接的总数,比如MaxConns,即MaxIdleConns + MaxOpenConns?为什么开发人员需要自己安排有多少打开和空闲连接,而不是定义连接池的总数呢?

英文:

Why are there SetMaxOpenConns and SetMaxIdleConns. In the doc

SetMaxIdleConns
> SetMaxIdleConns sets the maximum number of connections in the idle
> connection pool.
>
> If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
> then the new MaxIdleConns will be reduced to match the MaxOpenConns
> limit
>
> If n <= 0, no idle connections are retained.

SetMaxOpenConns
> SetMaxOpenConns sets the maximum number of open connections to the
> database.
>
> If MaxIdleConns is greater than 0 and the new MaxOpenConns is less
> than MaxIdleConns, then MaxIdleConns will be reduced to match the new
> MaxOpenConns limit
>
> If n <= 0, then there is no limit on the number of open connections.
> The default is 0 (unlimited).

Why have both functions but not a single function to adjust both idle and open connections like MaxConns which is MaxIdleConns + MaxOpenConns. Why would a developer have to arrange how many open and idle conns there can be instead of defining the total pool?

答案1

得分: 18

数据库连接池可以包含0个或多个空闲连接到数据库的连接。这些连接是在使用后没有关闭,而是保留下来以供将来使用的连接。我们可以保留的这些连接的数量是MaxIdleConns

当您请求其中一个空闲连接时,它变成了一个打开的连接,可供您使用。您可以使用的这些连接的数量是MaxOpenConns

现在,没有必要拥有比允许的最大打开连接数更多的空闲连接,因为如果您可以立即获取所有允许的打开连接,剩余的空闲连接将始终保持空闲。这就像是有一座有四条车道的桥,但只允许同时有三辆车通过。

因此,我们希望确保

MaxIdleConns <= MaxOpenConns

这些函数的编写是为了通过减少MaxIdleConns来保持这个不变量,当它超过MaxOpenConns时。请注意,文档中指出,只有MaxIdleConns会被减少以匹配MaxOpenConns,后者永远不会成立。

回答为什么开发人员可能希望单独调整这些参数的问题:考虑一个通常很安静,但偶尔需要打开大量连接的应用程序的情况。您可能希望指定一个较大的MaxOpenConns,但一个非常小的MaxIdleConns,以确保您的应用程序在需要时可以打开尽可能多的连接,并快速释放这些资源,为自身和数据库释放内存。保持空闲连接活动是有代价的,通常是因为您希望尽快将其转换为可用连接。

因此,这里有两个数字的原因是这两个参数您可能有充分理由单独变化。当然,API的语义意味着如果您不关心设置这两个值中的哪一个,您可以只设置您关心的那个值,这可能是MaxOpenConns

英文:

The db pool may contain 0 or more idle connections to the database. These were connections that were made, used, and rather than closed, were kept around for future use. The number of these we can keep around is MaxIdleConns.

When you request one of these idle connections, it becomes an Open connection, available for you to use. The number of these you can use is MaxOpenConns.

Now, there is no point in ever having any more idle connections than the maximum allowed open connections, because if you could instantanously grab all the allowed open connections, the remain idle connections would always remain idle. It's like having a bridge with four lanes, but only ever allowing three vehicles to drive across it at once.

Therefore, we would like to ensure that

MaxIdleConns &lt;= MaxOpenConns

The functions are written to preserve this invariant by reducing MaxIdleConns whenever it exceeds MaxOpenConns. Note that the documentation says, only MaxIdleConns is ever reduced to match MaxOpenConns, the latter is never true.

To answer the question of why a developer might want to adjust these separately: consider the case of an application that is usually quiet, but occasionally needs to open a large number of connections. You may wish to specify a large MaxOpenConns, but a very small MaxIdleConns, to ensure that your application can open as many connections in requires whenever it needs to, but releases these resources quickly, freeing up memory both for itself and the database. Keeping an idle connection alive is not free, and it's usually done because you want to turn it into usable connection soon.

So the reason there are two numbers here is that these are two parameters that you might have a good reason to vary individually. Of course, the semantics of the API mean that if you don't care about setting both these values, you can just set the one that you care about, which is probably MaxOpenConns

huangapple
  • 本文由 发表于 2015年8月12日 06:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/31952791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定