diff --git a/source/fundamentals/performance.txt b/source/fundamentals/performance.txt index a38db105..7b528edd 100644 --- a/source/fundamentals/performance.txt +++ b/source/fundamentals/performance.txt @@ -71,9 +71,23 @@ 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("").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:: @@ -81,6 +95,11 @@ and optimize performance. :manual:`Tuning Your Connection Pool Settings ` 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 @@ -103,11 +122,16 @@ option. The following code demonstrates how to specify a value for .. code-block:: rust - let mut client_options = ClientOptions::parse("").await?; + let mut client_options = ClientOptions::parse_async("").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 @@ -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("").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 @@ -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("").await?; + let mut client_options = ClientOptions::parse_async("").await?; + client_options.max_idle_time = Some(Duration::new(90, 0)); + + let client = Client::with_options(client_options)?; .. _rust-performance-parallelism: diff --git a/source/fundamentals/serialization.txt b/source/fundamentals/serialization.txt index 2129057c..b0719f0c 100644 --- a/source/fundamentals/serialization.txt +++ b/source/fundamentals/serialization.txt @@ -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