@@ -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+
23101How Can I Fix the "WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel" Error?
24102--------------------------------------------------------------------------------------------------------------------------
25103
0 commit comments