Skip to content

Commit 39c3245

Browse files
committed
pv feedback
1 parent 666aaa1 commit 39c3245

File tree

3 files changed

+173
-47
lines changed

3 files changed

+173
-47
lines changed

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

Lines changed: 124 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,64 +60,146 @@ Configure a Connection Pool
6060
You can specify settings for your connection pool either by using a connection
6161
string with the ``options.Client().ApplyURI()`` method.
6262

63-
The following are connection string settings you can use to configure your
64-
connection pool:
63+
Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
64+
see the corresponding syntax:
6565

66-
.. list-table::
67-
:widths: 25,75
68-
:header-rows: 1
66+
.. tabs::
6967

70-
* - Setting
71-
- Description
72-
73-
* - ``maxPoolSize``
68+
.. tab:: Connection String
69+
:tabid: uri
70+
71+
The following are connection string settings you can use to configure your
72+
connection pool:
7473

75-
- Maximum number of connections opened in the pool. If an operation needs a
76-
new connection while the connection pool has ``maxPoolSize`` connections
77-
open, the new operation waits for a new connection to open. To limit this
78-
waiting time, use the single timeout setting.
74+
.. list-table::
75+
:widths: 25,75
76+
:header-rows: 1
7977

80-
*Default:* ``100``
78+
* - Setting
79+
- Description
80+
81+
* - ``maxPoolSize``
8182

82-
* - ``minPoolSize``
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.
8387

84-
- Minimum number of connections opened in the pool. The value of
85-
``minPoolSize`` must be less than the value of ``maxPoolSize``.
88+
*Default:* ``100``
8689

87-
*Default*: ``0``
90+
* - ``minPoolSize``
8891

89-
* - ``maxConnecting``
90-
91-
- Maximum number of connections a pool may establish concurrently.
92-
93-
*Default:* ``2``
94-
95-
* - ``maxIdleTimeMS``
96-
97-
- The maximum number of milliseconds that a connection can remain idle in
98-
the pool before being removed and closed.
92+
- Minimum number of connections opened in the pool. The value of
93+
``minPoolSize`` must be less than the value of ``maxPoolSize``.
9994

100-
*Default:* ``None`` (no limit)
95+
*Default*: ``0``
10196

102-
* - ``waitQueueTimeoutMS```
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.
103107

104-
- Specifies the maximum wait time in milliseconds that a thread can wait
105-
for a connection to become available.
108+
*Default:* ``None`` (no limit)
106109

107-
*Default*: ``0`` (no limit)
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+
You can set the connection pool options directly using the ``options.Client`` methods.
121+
122+
The following table describes the methods you can chain to your settings
123+
to modify the driver's behavior:
124+
125+
.. list-table::
126+
:widths: 25,75
127+
:header-rows: 1
128+
129+
* - Setting
130+
- Description
131+
132+
* - ``SetMaxPoolSize()``
133+
134+
- Maximum number of connections opened in the pool. If an operation needs a
135+
new connection while the connection pool has the configured maximum connections
136+
open, the new operation waits for a new connection to open. To limit this
137+
waiting time, use the single timeout setting.
138+
139+
*Default:* ``100``
140+
141+
* - ``SetMinPoolSize()``
142+
143+
- Minimum number of connections opened in the pool. The value of
144+
``MinPoolSize`` must be less than the value of ``MaxPoolSize``.
145+
146+
*Default*: ``0``
147+
148+
* - ``SetMaxConnecting()``
149+
150+
- Maximum number of connections a pool may establish concurrently.
151+
152+
*Default:* ``2``
153+
154+
* - ``SetMAxConnIdleTime()``
155+
156+
- The maximum number of milliseconds that a connection can remain idle in
157+
the pool before being removed and closed.
158+
159+
*Default:* ``0`` (no limit)
108160

109161
Example
110162
~~~~~~~
111163

112-
The following code creates a client with a maximum connection pool size of
113-
``50``, a minimum pool size of ``10``, and a maximum idle time of
114-
``30000`` milliseconds (30 seconds):
115-
116-
.. literalinclude:: /includes/connect/connection-pools-uri.go
117-
:language: go
118-
:start-after: start-connection-pool-uri
119-
:end-before: end-connection-pool-uri
120-
:dedent:
164+
Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to
165+
see the corresponding example:
166+
167+
.. tabs::
168+
169+
.. tab:: Connection String
170+
:tabid: uriExample
171+
172+
The following code uses the connection string to configure the maximum
173+
connection pool size of ``50``, a minimum pool size of ``10``, and a
174+
maximum idle time of ``30000`` milliseconds (30 seconds):
175+
176+
.. literalinclude:: /includes/connect/connection-pools-uri.go
177+
:language: go
178+
:start-after: start-uri-variable
179+
:end-before: end-uri-variable
180+
:dedent:
181+
182+
The following code creates a client and passes the connection string to the
183+
``ApplyURI()`` method:
184+
185+
.. literalinclude:: /includes/connect/connection-pools-uri.go
186+
:language: go
187+
:start-after: start-apply-uri
188+
:end-before: end-apply-uri
189+
:dedent:
190+
191+
.. tab:: ClientOptions
192+
:tabid: optionsExample
193+
194+
The following code creates a client and sets the connection pool options
195+
with a maximum connection pool size of ``50``, a minimum pool size of
196+
``10``, and a maximum idle time of ``30000`` milliseconds (30 seconds):
197+
198+
.. literalinclude:: /includes/connect/connection-pools-client-options.go
199+
:language: go
200+
:start-after: start-client-options
201+
:end-before: end-client-options
202+
:dedent:
121203

122204
Optimize Connection Pools
123205
-------------------------
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+
}

source/includes/connect/connection-pools-uri.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ import (
88
"go.mongodb.org/mongo-driver/v2/mongo/options"
99
)
1010

11-
func main() {
12-
//start-connection-pool-uri
13-
// Connection string with connection pool options
14-
uri := "mongodb://localhost:27017/?maxPoolSize=50&minPoolSize=10&maxIdleTimeMS=30000"
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+
)
1516

17+
// end-uri-variable
18+
func main() {
19+
// start-apply-uri
1620
// Creates a new client and connect to the server
1721
client, err := mongo.Connect(options.Client().ApplyURI(uri))
1822
if err != nil {
1923
log.Fatal(err)
2024
}
21-
//end-connection-pool-uri
25+
// end-apply-uri
2226

27+
fmt.log("Connected to MongoDB with connection pool options")
2328
defer func() {
2429
if err = client.Disconnect(context.TODO()); err != nil {
2530
log.Fatal(err)

0 commit comments

Comments
 (0)