Connections pool in Go mgo package

后端 未结 1 1071
庸人自扰
庸人自扰 2021-01-12 14:42

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,

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 15:17

    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.

    0 讨论(0)
提交回复
热议问题