Skip to content

Commit 3da297d

Browse files
authored
DOCSP-28559: recommend reusing Client (#246)
* DOCSP-28559: recommend reusing Client * add Faq question * update * JS PR fixes 1
1 parent dae75f0 commit 3da297d

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

source/faq.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,85 @@ This page contains frequently asked questions and their corresponding answers.
1919
If you can't find an answer to your problem on this page,
2020
see the :ref:`golang-issues-and-help` page for next steps and more
2121
resources.
22+
23+
.. _golang-faq-connection-pool:
24+
25+
How Does Connection Pooling Work in the {+driver-short+}?
26+
---------------------------------------------------------
27+
28+
Every ``Client`` instance has a built-in connection pool for each server
29+
in your MongoDB topology. Connection pools open sockets on demand to support
30+
concurrent MongoDB operations, or `goroutines
31+
<https://www.golang-book.com/books/intro/10>`__, in your application.
32+
33+
The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
34+
defaults to ``100``. If the number of in-use connections to a server reaches
35+
the value of ``maxPoolSize``, the next request to that server will wait
36+
until a connection becomes available.
37+
38+
The ``Client`` instance opens two additional sockets per server in your
39+
MongoDB topology for monitoring the server's state.
40+
41+
For example, a client connected to a 3-node replica set opens 6
42+
monitoring sockets. It also opens as many sockets as needed to support
43+
an application's concurrent operations on each server, up to
44+
the value of ``maxPoolSize``. If ``maxPoolSize`` is ``100`` and the
45+
application only uses the primary (the default), then only the primary
46+
connection pool grows and there can be at most ``106`` total connections. If the
47+
application uses a :ref:`read preference <golang-read-pref>` to query the
48+
secondary nodes, their pools also grow and there can be ``306`` total connections.
49+
50+
Additionally, connection pools are rate-limited such that each connection pool
51+
can only create, at maximum, the value of ``maxConnecting`` connections
52+
in parallel at any time. Any additional goroutine stops waiting in the
53+
following cases:
54+
55+
- One of the existing goroutines finishes creating a connection, or
56+
an existing connection is checked back into the pool.
57+
- The driver's ability to reuse existing connections improves due to
58+
rate-limits on connection creation.
59+
60+
You can set the minimum number of concurrent connections to
61+
each server with the ``minPoolSize`` option, which defaults to ``0``. The connection pool
62+
will be initialized with this number of sockets. If sockets are closed
63+
due to any network errors, causing the total number of sockets (both in
64+
use and idle) to drop below the minimum, more sockets are opened until
65+
the minimum is reached.
66+
67+
You can set the maximum number of milliseconds that a connection can
68+
remain idle in the pool before being removed and replaced with
69+
the ``maxIdleTimeMS`` option, which defaults to ``None`` (no limit).
70+
71+
The following default configuration for a ``Client`` works for most applications:
72+
73+
.. code-block:: go
2274

75+
client := mongo.Connect("<connection string>")
76+
77+
Create a client once for each process, and reuse it for all
78+
operations. It is a common mistake to create a new client for each
79+
request, which is very inefficient.
80+
81+
To support high numbers of concurrent MongoDB operations
82+
within one process, you can increase ``maxPoolSize``. Once the pool
83+
reaches its maximum size, additional operations wait for sockets
84+
to become available.
85+
86+
The driver does not limit the number of operations that
87+
can wait for sockets to become available and it is the application's
88+
responsibility to limit the size of its pool to bound queuing
89+
during a load spike. Threads can wait for any length of time
90+
unless you define the ``waitQueueTimeoutMS`` option.
91+
92+
An operation that waits more than the length of time defined by
93+
``waitQueueTimeoutMS`` for a socket raises a connection error. Use this
94+
option if it is more important to bound the duration of operations
95+
during a load spike than it is to complete every operation.
96+
97+
When ``Client.Disconnect()`` is called by any goroutine, the driver
98+
closes all idle sockets and closes all sockets that are in
99+
use as they are returned to the pool.
100+
23101
How Can I Fix the "WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel" Error?
24102
--------------------------------------------------------------------------------------------------------------------------
25103

source/fundamentals/connection.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ Connection Example
6060
To connect to MongoDB, you need to create a client. A client manages
6161
your connections and runs database commands.
6262

63+
.. tip:: Reuse Your Client
64+
65+
We recommend that you reuse your client across sessions and operations.
66+
You can use the same ``Client`` instance to perform multiple tasks, instead of
67+
creating a new one each time. The ``Client`` type is safe for
68+
concurrent use by multiple `goroutines
69+
<https://www.golang-book.com/books/intro/10>`__. To learn more about
70+
how connection pools work in the driver, see the :ref:`FAQ page <golang-faq-connection-pool>`.
71+
6372
You can create a client that uses your connection string and other
6473
client options by passing a ``ClientOptions`` object to the ``Connect()``
6574
method.

source/fundamentals/crud/write-read-pref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ with this option.
178178
database := client.Database("myDB")
179179
coll := database.Collection("myCollection", opts)
180180

181+
.. _golang-read-pref:
182+
181183
Read Preference
182184
---------------
183185

source/fundamentals/transactions.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ grouping of related read or write operations that you intend to run
2828
sequentially. Sessions enable :manual:`causal consistency </core/read-isolation-consistency-recency/#causal-consistency>`
2929
for a group of operations or allow you to execute operations in an :website:`ACID
3030
transaction </basics/acid-transactions>`. MongoDB guarantees that the data
31-
involved in your transaction operations remains , even if the operations
31+
involved in your transaction operations remains consistent, even if the operations
3232
encounter unexpected errors.
3333

3434
With the {+driver-short+}, you can create a new session from a
35-
``Client`` instance as a ``Session`` type.
35+
``Client`` instance as a ``Session`` type. We recommend that you reuse
36+
your client for multiple sessions and transactions instead of
37+
instantiating a new client each time.
3638

3739
.. warning::
3840

0 commit comments

Comments
 (0)