mongodb - Go mgo 包中的连接池

标签 mongodb go mgo

在文章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/

相关文章:

javascript - MongoDB - 列表中每个 ID 的最新项目

java - 在 mongodb 中搜索复杂文档

go - Go的MongoDB独特查询和$ in

mongodb - NodeJS 和 Mongoose 与 Mongodb 分片问题

arrays - MongoDB 中的唯一索引

file - 读取文件返回<nil>

go - 如何取消引用mgo中的dbref?

ubuntu - 如何从源代码在我的 ubuntu 12.10 上安装 Go

mongodb - GoLang mgo - mgo.ErrNotFound for find(...).All(...)

mongodb - MGO 驱动程序中的更新查询,适用于 bson.M,但不适用于自定义结构