@@ -71,16 +71,35 @@ Connection Pool
71
71
72
72
Every ``Client`` instance has a built-in connection pool for each server
73
73
in your MongoDB topology. Connection pools open sockets on demand to
74
- support concurrent requests to MongoDB in your application. You can
75
- tune the connection pool to best fit the needs of your application
76
- and optimize performance.
74
+ support concurrent requests to MongoDB in your application.
75
+
76
+ The default configuration for a ``Client`` works for most applications.
77
+ The following code shows how to create a client with default connection
78
+ settings:
79
+
80
+ .. code-block:: rust
81
+
82
+ let client = Client::with_uri_str("<connection string>").await?;
83
+
84
+ Alternatively, you can tune the connection pool to best fit the needs of your
85
+ application and optimize performance. For more information on how to customize
86
+ your connection settings, see the following subsections of this guide:
87
+
88
+ - :ref:`rust-max-pool`
89
+ - :ref:`rust-concurrent-connections`
90
+ - :ref:`rust-max-idle`
77
91
78
92
.. tip::
79
93
80
94
To learn more about configuring a connection pool, see
81
95
:manual:`Tuning Your Connection Pool Settings
82
96
</tutorial/connection-pool-performance-tuning/>` in the Server manual.
83
97
98
+ .. _rust-max-pool:
99
+
100
+ Configure Maximum Pool Size
101
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
+
84
103
The maximum size of each connection pool is set by the ``max_pool_size``
85
104
option, which defaults to ``10``. If the number of in-use connections to
86
105
a server reaches the value of ``max_pool_size``, the next request to
@@ -103,11 +122,16 @@ option. The following code demonstrates how to specify a value for
103
122
104
123
.. code-block:: rust
105
124
106
- let mut client_options = ClientOptions::parse ("<connection string>").await?;
125
+ let mut client_options = ClientOptions::parse_async ("<connection string>").await?;
107
126
client_options.max_pool_size = Some(20);
108
127
109
128
let client = Client::with_options(client_options)?;
110
129
130
+ .. _rust-concurrent-connections:
131
+
132
+ Configure Concurrent Connection Options
133
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134
+
111
135
Connection pools are rate-limited. The ``max_connecting`` option
112
136
determines the number of connections that the pool can create in
113
137
parallel at any time. For example, if the value of ``max_connecting`` is
@@ -127,6 +151,22 @@ sockets are closed and the total number of sockets, both in use and
127
151
idle, drops below the minimum, the connection pool opens more sockets until the
128
152
minimum is reached.
129
153
154
+ The following code sets the ``max_connecting`` and ``min_pool_size`` options when
155
+ instantiating a ``Client``:
156
+
157
+ .. code-block:: rust
158
+
159
+ let mut client_options = ClientOptions::parse_async("<connection string>").await?;
160
+ client_options.max_connecting = Some(3);
161
+ client_options.min_pool_size = Some(1);
162
+
163
+ let client = Client::with_options(client_options)?;
164
+
165
+ .. _rust-max-idle:
166
+
167
+ Configure Maximum Idle Time
168
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
169
+
130
170
You can set the maximum amount of time that a connection can
131
171
remain idle in the pool by setting the ``max_idle_time`` option.
132
172
Once a connection has been idle for the duration specified in
@@ -140,13 +180,15 @@ closes only inactive sockets, so you cannot interrupt or terminate
140
180
any ongoing operations by using this method. The driver closes these
141
181
sockets only when the process completes.
142
182
143
- The default configuration for a ``Client`` works for most applications.
144
- The following code shows how to create a client with default connection
145
- settings:
183
+ The following code sets the value of the ``max_idle_time`` option to
184
+ ``90`` seconds when instantiating a ``Client``:
146
185
147
186
.. code-block:: rust
148
187
149
- let client = Client::with_uri_str("<connection string>").await?;
188
+ let mut client_options = ClientOptions::parse_async("<connection string>").await?;
189
+ client_options.max_idle_time = Some(Duration::new(90, 0));
190
+
191
+ let client = Client::with_options(client_options)?;
150
192
151
193
.. _rust-performance-parallelism:
152
194
0 commit comments