gpt4 book ai didi

mongodb - Go mgo 包中的连接池

转载 作者:IT老高 更新时间:2023-10-28 13:21:19 25 4
gpt4 key购买 nike

在文章running-mongodb-queries-concurrently-with-go说 mgo.DialWithInfo :创建一个 session ,该 session 维护一个与 MongoDB 的套接字连接池,但是当我在 documentacion 中查找时函数DialWithInfo我没有找到任何关于池连接的内容,只有我在 Dial 中找到了一些内容。 Function Dial Function 表示:对于给定的集群,这个方法通常只调用一次。然后在获得的 session 上使用 New 或 Copy 方法建立到同一集群的更多 session 。这将使它们共享底层集群,并适本地管理连接池。

  • 谁能告诉我 MGO 上的池连接是如何工作的,是否可以设置这个池?
  • 是真的 DialWithInfo 创建了一个 Pool Connection 还是只有 Dial 函数创建了这个 pool?

提前致谢

最佳答案

查看 source code for the Dial function calls ,可以看到Dial函数调用了DialWithTimeout函数,该函数又调用了DialWithInfo函数。因此,为了回答您关于函数之间差异的问题,似乎 DialDialWithTimeout 的便利包装器,而后者又是 DialWithInfo< 的便利包装器,因此它们会产生相同的连接池。

关于如何管理该连接池,您的问题是正确的。

Further sessions to the same cluster are then established using the New or Copy methods on the obtained session. This will make them share the underlying cluster, and manage the pool of connections appropriately.

因此,一次调用 DialDialWithTimeoutDialWithInfo 将建立连接池,如果您需要多个 session ,请使用session.New()session.Copy() 方法从您选择使用的任何 Dial 函数返回的 session 中获取它。

关于mongodb - Go mgo 包中的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com