@@ -71,16 +71,35 @@ Connection Pool
7171
7272Every ``Client`` instance has a built-in connection pool for each server
7373in 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+
84103The maximum size of each connection pool is set by the ``max_pool_size``
85104option, which defaults to ``10``. If the number of in-use connections to
86105a 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+
111135Connection pools are rate-limited. The ``max_connecting`` option
112136determines the number of connections that the pool can create in
113137parallel 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
127151idle, drops below the minimum, the connection pool opens more sockets until the
128152minimum 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+
130170You can set the maximum amount of time that a connection can
131171remain idle in the pool by setting the ``max_idle_time`` option.
132172Once a connection has been idle for the duration specified in
@@ -140,13 +180,15 @@ closes only inactive sockets, so you cannot interrupt or terminate
140180any ongoing operations by using this method. The driver closes these
141181sockets 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
0 commit comments