Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions source/fundamentals/performance.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,35 @@ Connection Pool

Every ``Client`` instance has a built-in connection pool for each server
in your MongoDB topology. Connection pools open sockets on demand to
support concurrent requests to MongoDB in your application. You can
tune the connection pool to best fit the needs of your application
and optimize performance.
support concurrent requests to MongoDB in your application.

The default configuration for a ``Client`` works for most applications.
The following code shows how to create a client with default connection
settings:

.. code-block:: rust

let client = Client::with_uri_str("<connection string>").await?;

Alternatively, you can tune the connection pool to best fit the needs of your
application and optimize performance. For more information on how to customize
your connection settings, see the following subsections of this guide:

- :ref:`rust-max-pool`
- :ref:`rust-concurrent-connections`
- :ref:`rust-max-idle`

.. tip::

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

.. _rust-max-pool:

Configure Maximum Pool Size
~~~~~~~~~~~~~~~~~~~~~~~~~~~

The maximum size of each connection pool is set by the ``max_pool_size``
option, which defaults to ``10``. If the number of in-use connections to
a server reaches the value of ``max_pool_size``, the next request to
Expand All @@ -103,11 +122,16 @@ option. The following code demonstrates how to specify a value for

.. code-block:: rust

let mut client_options = ClientOptions::parse("<connection string>").await?;
let mut client_options = ClientOptions::parse_async("<connection string>").await?;
client_options.max_pool_size = Some(20);

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

.. _rust-concurrent-connections:

Configure Concurrent Connection Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Connection pools are rate-limited. The ``max_connecting`` option
determines the number of connections that the pool can create in
parallel at any time. For example, if the value of ``max_connecting`` is
Expand All @@ -127,6 +151,22 @@ sockets are closed and the total number of sockets, both in use and
idle, drops below the minimum, the connection pool opens more sockets until the
minimum is reached.

The following code sets the ``max_connecting`` and ``min_pool_size`` options when
instantiating a ``Client``:

.. code-block:: rust

let mut client_options = ClientOptions::parse_async("<connection string>").await?;
client_options.max_connecting = Some(3);
client_options.min_pool_size = Some(1);

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

.. _rust-max-idle:

Configure Maximum Idle Time
~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can set the maximum amount of time that a connection can
remain idle in the pool by setting the ``max_idle_time`` option.
Once a connection has been idle for the duration specified in
Expand All @@ -140,13 +180,15 @@ closes only inactive sockets, so you cannot interrupt or terminate
any ongoing operations by using this method. The driver closes these
sockets only when the process completes.

The default configuration for a ``Client`` works for most applications.
The following code shows how to create a client with default connection
settings:
The following code sets the value of the ``max_idle_time`` option to
``90`` seconds when instantiating a ``Client``:

.. code-block:: rust

let client = Client::with_uri_str("<connection string>").await?;
let mut client_options = ClientOptions::parse_async("<connection string>").await?;
client_options.max_idle_time = Some(Duration::new(90, 0));

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

.. _rust-performance-parallelism:

Expand Down
4 changes: 2 additions & 2 deletions source/fundamentals/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Custom Data Model
You can use any Rust data type that implements the ``Serialize`` and
``Deserialize`` traits from the ``serde`` crate as the generic type
parameter for a ``Collection`` instance. To implement the ``Serialize``
and ``Deserialize`` traits, you must use the following ``derive``
statement before defining a Rust type:
and ``Deserialize`` traits, you must include the following ``derive``
attribute before defining a Rust type:

.. code-block:: rust

Expand Down