Skip to content

Commit c7bb8c1

Browse files
authored
DOCSP-47023 Connection pools (#505)
1 parent 13fdcc3 commit c7bb8c1

File tree

3 files changed

+311
-1
lines changed

3 files changed

+311
-1
lines changed

source/connect/connection-options/connection-pools.txt

Lines changed: 239 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,242 @@
44
Connection Pools
55
================
66

7-
.. TODO
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
Overview
18+
--------
19+
20+
In this guide, you can learn about how the {+driver-short+} uses connection
21+
pools to manage connections to a MongoDB deployment and how you can configure
22+
connection pool settings in your application.
23+
24+
A connection pool is a cache of open database connections maintained by the
25+
{+driver-short+}. When your application requests a connection to MongoDB, the
26+
{+driver-short+} seamlessly gets a connection from the pool, performs
27+
operations, and returns the connection to the pool for reuse.
28+
29+
Connection pools help reduce application latency and the number of times new
30+
connections are created by {+driver-short+}.
31+
32+
.. _golang-faq-connection-pool:
33+
34+
Create a Connection Pool
35+
------------------------
36+
37+
Every ``Client`` instance has a built-in connection pool for each server in your
38+
MongoDB topology. If you do not configure the ``minPoolSize`` option, connection
39+
pools open sockets on demand. These sockets support concurrent MongoDB
40+
operations, or `goroutines <https://go.dev/tour/concurrency/1>`__, in
41+
your application.
42+
43+
When a new ``Client`` instance is instatiated, it opens two sockets per server
44+
in your MongoDB topology for monitoring the server's state.
45+
46+
For example, a client connected to a three-node replica set opens six monitoring
47+
sockets. If the application uses the default setting for ``maxPoolSize`` and
48+
only queries the primary (default) node, then there can be at most ``106`` open
49+
sockets and ``100`` connections in the connection pool. If the application uses
50+
a :ref:`read preference <golang-read-pref>` to query the secondary nodes, their
51+
pools also grow and there can be ``306`` total connections.
52+
53+
For efficiency, create a client once for each process, and reuse it for all
54+
operations. Avoid creating a new client for each request because this will
55+
increase latency.
56+
57+
Configure a Connection Pool
58+
---------------------------
59+
60+
You can specify settings for your connection pool either by using a connection
61+
string or by using the ``options.Client`` methods.
62+
63+
Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
64+
see the corresponding syntax:
65+
66+
.. tabs::
67+
68+
.. tab:: Connection String
69+
:tabid: uri
70+
71+
The following are connection string settings you can use to configure your
72+
connection pool:
73+
74+
.. list-table::
75+
:widths: 25,75
76+
:header-rows: 1
77+
78+
* - Setting
79+
- Description
80+
81+
* - ``maxPoolSize``
82+
83+
- Maximum number of connections opened in the pool. If an operation needs a
84+
new connection while the connection pool has ``maxPoolSize`` connections
85+
open, the new operation waits for a new connection to open. To limit this
86+
waiting time, use the single timeout setting.
87+
88+
*Default:* ``100``
89+
90+
* - ``minPoolSize``
91+
92+
- Minimum number of connections opened in the pool. The value of
93+
``minPoolSize`` must be less than the value of ``maxPoolSize``.
94+
95+
*Default*: ``0``
96+
97+
* - ``maxConnecting``
98+
99+
- Maximum number of connections a pool may establish concurrently.
100+
101+
*Default:* ``2``
102+
103+
* - ``maxIdleTimeMS``
104+
105+
- The maximum number of milliseconds that a connection can remain idle in
106+
the pool before being removed and closed.
107+
108+
*Default:* ``None`` (no limit)
109+
110+
* - ``waitQueueTimeoutMS```
111+
112+
- Specifies the maximum wait time in milliseconds that a thread can wait
113+
for a connection to become available.
114+
115+
*Default*: ``0`` (no limit)
116+
117+
.. tab:: ClientOptions
118+
:tabid: ClientOptions
119+
120+
The following table describes the methods you can chain to your settings
121+
to modify the driver's behavior:
122+
123+
.. list-table::
124+
:widths: 25,75
125+
:header-rows: 1
126+
127+
* - Setting
128+
- Description
129+
130+
* - ``SetMaxPoolSize()``
131+
132+
- Maximum number of connections opened in the pool. If an operation needs a
133+
new connection while the connection pool has the configured maximum connections
134+
open, the new operation waits for a new connection to open. To limit this
135+
waiting time, use the single timeout setting.
136+
137+
*Default:* ``100``
138+
139+
* - ``SetMinPoolSize()``
140+
141+
- Minimum number of connections opened in the pool. The value of
142+
``MinPoolSize`` must be less than the value of ``MaxPoolSize``.
143+
144+
*Default*: ``0``
145+
146+
* - ``SetMaxConnecting()``
147+
148+
- Maximum number of connections a pool may establish concurrently.
149+
150+
*Default:* ``2``
151+
152+
* - ``SetMaxConnIdleTime()``
153+
154+
- The maximum number of milliseconds that a connection can remain idle in
155+
the pool before being removed and closed.
156+
157+
*Default:* ``0`` (no limit)
158+
159+
Example
160+
~~~~~~~
161+
162+
Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
163+
see the corresponding example:
164+
165+
.. tabs::
166+
167+
.. tab:: Connection String
168+
:tabid: uriExample
169+
170+
The following code uses the connection string to configure the maximum
171+
connection pool size of ``50``, a minimum pool size of ``10``, and a
172+
maximum idle time of ``30000`` milliseconds (30 seconds):
173+
174+
.. literalinclude:: /includes/connect/connection-pools-uri.go
175+
:language: go
176+
:start-after: start-uri-variable
177+
:end-before: end-uri-variable
178+
:dedent:
179+
180+
The following code creates a client and passes the connection string to the
181+
``ApplyURI()`` method:
182+
183+
.. literalinclude:: /includes/connect/connection-pools-uri.go
184+
:language: go
185+
:start-after: start-apply-uri
186+
:end-before: end-apply-uri
187+
:dedent:
188+
189+
.. tab:: ClientOptions
190+
:tabid: optionsExample
191+
192+
The following code creates a client and sets the connection pool options
193+
with a maximum connection pool size of ``50``, a minimum pool size of
194+
``10``, and a maximum idle time of ``30000`` milliseconds (30 seconds):
195+
196+
.. literalinclude:: /includes/connect/connection-pools-client-options.go
197+
:language: go
198+
:start-after: start-client-options
199+
:end-before: end-client-options
200+
:dedent:
201+
202+
Optimize Connection Pools
203+
-------------------------
204+
205+
Connection pools are rate-limited such that each connection pool can only create,
206+
at maximum, the value of ``maxConnecting`` connections in parallel at any time.
207+
Any new goroutine stops waiting in the following cases:
208+
209+
- One of the existing goroutines finishes creating a connection, or an existing
210+
connection is checked back into the pool.
211+
- The driver's ability to reuse existing connections improves due to rate-limits
212+
on connection creation.
213+
214+
The driver does not limit the number of operations that can wait for sockets to
215+
become available, so it is the application's responsibility to manage its
216+
operation queue. Operations can wait for any length of time unless you define
217+
the ``waitQueueTimeoutMS`` option.
218+
219+
An operation that waits more than the length of time defined by
220+
``waitQueueTimeoutMS`` for a socket raises a connection error. Use this option
221+
if it is more important to bound the duration of operations during a load spike
222+
than it is to complete every operation.
223+
224+
Disconnecting
225+
-------------
226+
227+
When you call ``Client.Disconnect()`` from any goroutine, the driver closes all
228+
idle sockets, and then closes all sockets as they are returned to the pool.
229+
230+
Additional Information
231+
----------------------
232+
233+
For more information on using a connection pool, see the :manual:`Connection
234+
Pool </administration/connection-pool-overview>` documentation in the Server
235+
manual.
236+
237+
API Documentation
238+
~~~~~~~~~~~~~~~~~
239+
240+
To learn more about any of the methods or types discussed in this
241+
guide, see the following API documentation:
242+
243+
- `Client <{+api+}/mongo#Client>`__
244+
- `ClientOptions <{+api+}/mongo/options#ClientOptions>`__
245+
- `Client.Diconnect() <{+api+}/mongo#Client.Disconnect>`__
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"time"
8+
9+
"go.mongodb.org/mongo-driver/v2/mongo"
10+
"go.mongodb.org/mongo-driver/v2/mongo/options"
11+
)
12+
13+
func main() {
14+
uri := os.Getenv("MONGODB_URI")
15+
if uri == "" {
16+
log.Fatal("Set your 'MONGODB_URI' environment variable.")
17+
}
18+
// start-client-options
19+
// Sets client options with connection pool settings
20+
clientOptions := options.Client().
21+
ApplyURI(uri).
22+
SetMaxPoolSize(50).
23+
SetMinPoolSize(10).
24+
SetMaxConnIdleTime(30 * time.Second)
25+
26+
// Creates a new client and connects to the server
27+
client, err := mongo.Connect(clientOptions)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
// end-client-options
32+
33+
// Ensures the client is disconnected when the function exits
34+
defer func() {
35+
if err = client.Disconnect(context.TODO()); err != nil {
36+
log.Fatal(err)
37+
}
38+
}()
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"go.mongodb.org/mongo-driver/v2/mongo"
8+
"go.mongodb.org/mongo-driver/v2/mongo/options"
9+
)
10+
11+
// start-uri-variable
12+
// Connection string with connection pool options
13+
const (
14+
uri = "mongodb://localhost:27017/?maxPoolSize=50&minPoolSize=10&maxIdleTimeMS=30000"
15+
)
16+
17+
// end-uri-variable
18+
func main() {
19+
// start-apply-uri
20+
// Creates a new client and connect to the server
21+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
// end-apply-uri
26+
27+
fmt.log("Connected to MongoDB with connection pool options")
28+
defer func() {
29+
if err = client.Disconnect(context.TODO()); err != nil {
30+
log.Fatal(err)
31+
}
32+
}()
33+
}

0 commit comments

Comments
 (0)