7

記事running-mongodb-queries-concurrently-with-goで、 mgo.DialWithInfo : MongoDB へのソケット接続のプールを維持するセッションを作成しますが、関数DialWithInfoのドキュメントを探していると、何かが見つかりません。それはプール接続について私に話しますが、ダイヤル機能のダイヤル機能で何かを見つけただけですこのメソッドは通常、特定のクラスターに対して一度だけ呼び出されます。その後、取得したセッションで New メソッドまたは Copy メソッドを使用して、同じクラスターへのさらなるセッションが確立されます。これにより、基盤となるクラスターが共有され、接続のプールが適切に管理されます。

  • MGO でプール接続がどのように機能するか、また可能であればこのプールをセットアップする方法を教えてもらえますか?
  • DialWithInfo がプール接続を作成するというのは本当ですか、それとも、このプールを作成するのは Dial 関数だけですか?

前もって感謝します

4

1 に答える 1

10

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.

于 2014-04-22T16:54:43.087 に答える