Go mgo包中的连接池 [英] Connections pool in Go mgo package
问题描述
在文章正在运行的mongodb查询中 - 同时-go-go 说mgo.DialWithInfo:创建一个会话,它维护一个到MongoDB的套接字连接池,但是当我在 documentacion DialWithInfo 我没有发现有关池连接的问题,只有我在拨号功能拨号功能说:此方法通常只针对给定的群集调用一次。然后在获得的会话上使用New或Copy方法建立到同一集群的更多会话。这将使他们共享底层集群,并适当地管理连接池。
- 有人可以说我在MGO上的游泳池连接是如何工作的,如果可能的话建立这个游泳池?
- 确实DialWithInfo创建池连接或者只是创建该池的Dial函数?
提前致谢
解决方案展望拨号函数调用的源代码,您可以看到
拨号
函数调用调用DialWithInfo
函数的DialWithTimeout
函数。因此,要回答你关于函数之间差异的问题,看起来像Dial
是DialWithTimeout
的便捷包装,它这对于DialWithInfo
来说是一个便捷的包装,因此它们产生相同的连接池。
至于如何管理连接池,你的问题就对了。
然后在获得的会话上使用New或Copy方法建立到同一群集的更多会话。这将使他们共享底层集群,并适当地管理连接池。
因此,一次调用
拨号
或DialWithTimeout
或DialWithInfo
将建立连接池,如果您需要超过使用session.New()
或session.Copy()
方法从返回的会话中获取它无论您选择使用哪种拨号功能。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
解决方案Looking into the source code for the Dial function calls, you can see that the
Dial
function calls theDialWithTimeout
function which calls theDialWithInfo
function. So to answer your question about the differences between the functions, it seems likeDial
is a convenience wrapper forDialWithTimeout
, which in turn is a convenience wrapper forDialWithInfo
, 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
orDialWithTimeout
orDialWithInfo
will establish the connection pool, if you require more than one session, use thesession.New()
orsession.Copy()
methods to obtain it from the session returned from whichever Dial function you chose to use.这篇关于Go mgo包中的连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!