Go语言的mgo包中的连接池

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

Connections pool in Go mgo package

问题

在文章使用Go并发运行MongoDB查询中提到,mgo.DialWithInfo用于创建一个维护到MongoDB的套接字连接池的会话。但是当我在DialWithInfo函数的文档中查找时,并没有找到关于连接池的内容,只在Dial函数的文档中找到了一些相关信息:该方法通常只对给定的集群调用一次。然后,可以使用获取的会话上的New或Copy方法建立到同一集群的进一步会话。这将使它们共享底层集群,并适当地管理连接池。

  • 有人可以告诉我MGO中连接池是如何工作的,是否可以设置连接池?
  • DialWithInfo是否真的创建了连接池,还是只有Dial函数创建了连接池?

谢谢!

英文:

In the article running-mongodb-queries-concurrently-with-go said that mgo.DialWithInfo : Create a session which maintains a pool of socket connections to MongoDB, but when I looking for in the documentacion of the function DialWithInfo I do not find something that talk me about pool connection, only I find something in the Dial Function Dial Function that said : This method is generally called just once for a given cluster. 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.

  • Can someone say me how works the pool connections on MGO and if is possible set up this pool?
  • Is it true that DialWithInfo create a Pool Connection or is only the Dial function that create this pool?

Thanks in Advance

答案1

得分: 10

查看Dial函数调用的源代码,你可以看到Dial函数调用了DialWithTimeout函数,而DialWithTimeout函数又调用了DialWithInfo函数。所以回答你关于这些函数之间的区别的问题,似乎DialDialWithTimeout的便捷包装器,而DialWithTimeout又是DialWithInfo的便捷包装器,因此它们都会得到相同的连接池。

至于如何管理这个连接池,你在问题中已经说得很对。

> 然后,可以使用从获取的会话中的New或Copy方法来建立到同一集群的进一步会话。这将使它们共享底层集群,并适当地管理连接池。

因此,对DialDialWithTimeoutDialWithInfo的单个调用将建立连接池,如果你需要多个会话,可以使用session.New()session.Copy()方法从从你选择使用的任何Dial函数返回的会话中获取它。

英文:

Looking into the source code for the Dial function calls, you can see that the Dial function calls the DialWithTimeout function which calls the DialWithInfo function. So to answer your question about the differences between the functions, it seems like Dial is a convenience wrapper for DialWithTimeout, which in turn is a convenience wrapper for DialWithInfo, so they result in the same connection pool.

As to how to manage that connection pool, you've got it right in your question.

> 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.

So a single call to Dial or DialWithTimeout or DialWithInfo will establish the connection pool, if you require more than one session, use the session.New() or session.Copy() methods to obtain it from the session returned from whichever Dial function you chose to use.

huangapple
  • 本文由 发表于 2014年4月22日 23:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/23223743.html
匿名

发表评论

匿名网友

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

确定