Skip to content

Commit d575ab0

Browse files
committed
DOCSP-35254: Connection pooling subsections (#103)
(cherry picked from commit a474ca0)
1 parent 3bf02d6 commit d575ab0

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

source/fundamentals/performance.txt

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,35 @@ Connection Pool
7171

7272
Every ``Client`` instance has a built-in connection pool for each server
7373
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`
7791

7892
.. tip::
7993

8094
To learn more about configuring a connection pool, see
8195
:manual:`Tuning Your Connection Pool Settings
8296
</tutorial/connection-pool-performance-tuning/>` in the Server manual.
8397

98+
.. _rust-max-pool:
99+
100+
Configure Maximum Pool Size
101+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
102+
84103
The maximum size of each connection pool is set by the ``max_pool_size``
85104
option, which defaults to ``10``. If the number of in-use connections to
86105
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
103122

104123
.. code-block:: rust
105124

106-
let mut client_options = ClientOptions::parse("<connection string>").await?;
125+
let mut client_options = ClientOptions::parse_async("<connection string>").await?;
107126
client_options.max_pool_size = Some(20);
108127

109128
let client = Client::with_options(client_options)?;
110129

130+
.. _rust-concurrent-connections:
131+
132+
Configure Concurrent Connection Options
133+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134+
111135
Connection pools are rate-limited. The ``max_connecting`` option
112136
determines the number of connections that the pool can create in
113137
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
127151
idle, drops below the minimum, the connection pool opens more sockets until the
128152
minimum is reached.
129153

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+
130170
You can set the maximum amount of time that a connection can
131171
remain idle in the pool by setting the ``max_idle_time`` option.
132172
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
140180
any ongoing operations by using this method. The driver closes these
141181
sockets only when the process completes.
142182

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``:
146185

147186
.. code-block:: rust
148187

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)?;
150192

151193
.. _rust-performance-parallelism:
152194

source/fundamentals/serialization.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Custom Data Model
6666
You can use any Rust data type that implements the ``Serialize`` and
6767
``Deserialize`` traits from the ``serde`` crate as the generic type
6868
parameter for a ``Collection`` instance. To implement the ``Serialize``
69-
and ``Deserialize`` traits, you must use the following ``derive``
70-
statement before defining a Rust type:
69+
and ``Deserialize`` traits, you must include the following ``derive``
70+
attribute before defining a Rust type:
7171

7272
.. code-block:: rust
7373

0 commit comments

Comments
 (0)