Go mgo包中的连接池 [英] Connections pool in Go mgo package

查看:122
本文介绍了Go mgo包中的连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文章正在运行的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 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.

    这篇关于Go mgo包中的连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆