Skip to content

DOCSP-42104 CSOT #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 30, 2025
Merged
Changes from 2 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
51 changes: 29 additions & 22 deletions source/connect/csot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,37 @@ Limit Server Execution Time
:values: reference

.. meta::
:keywords: error, blocking, thread, task
:keywords: error, blocking, thread, task, code examples

Overview
--------

When you use the {+driver-short+} to perform a server operation, you can also
In this guide, you can learn how to use the ``timeout_ms`` option to set a
timeout for server operations.

When you use the {+driver-short+} to perform a server operation, you can
limit the duration allowed for the server to finish the operation. To do so,
specify a **client-side operation timeout (CSOT)**. The timeout applies to all
steps needed to complete the operation, including server selection, connection
checkout, and server-side execution. When the timeout expires, the
{+driver-short+} raises a timeout exception.
checkout, and server-side execution. If the timeout expires before the operation
completes, the {+driver-short+} raises a timeout exception.

timeout_ms Option
-----------------
``timeout_ms`` Option
---------------------

To specify a timeout when connecting to a MongoDB deployment, set the
``timeout_ms`` connection option to the timeout length in milliseconds. You can
do this in two ways: by passing an argument to the ``Mongo::Client`` constructor
or through a parameter in your connection string.
specify a timeout value in two ways:

The following code examples use the ``timeout_ms`` client option or the ``timeoutMS``
connection string option to specify a timeout of 30 seconds:
- Pass the ``timeout_ms`` client option to the ``Mongo::Client`` constructor.
- Pass the ``timeoutMS`` connection string option as a parameter to your connection string.

Select from the following tabs to view examples for how to use the ``timeout_ms`` client
option or the ``timeoutMS`` connection string option to specify a timeout of 30 seconds:

.. tabs::

.. tab:: MongoClient
.. tab:: Mongo::Client
:tabid: mongoclient

.. code-block:: ruby
Expand All @@ -61,12 +66,12 @@ connection string option to specify a timeout of 30 seconds:
client = Mongo::Client.new(uri)

If you specify the ``timeout_ms`` option, the driver automatically applies the
specified timeout per each server operation.
specified timeout for each server operation.

.. note::

The ``timeout_ms`` connection option unifies most timeout related options.
As of the {+driver-short+} v2.21, the following timeout options are deprecated:
The following timeout options are deprecated:

- ``socket_timeout``
- ``wait_queue_timeout``
Expand All @@ -92,7 +97,7 @@ The following table describes how the timeout value is inherited at each level:
- Inheritance Description

* - Operation
- Takes the highest precedence and will override ``timeout_ms``
- Takes the highest precedence and overrides ``timeout_ms``
options set at any other level.

* - Transaction
Expand All @@ -116,8 +121,8 @@ The following table describes how the timeout value is inherited at each level:
operations within that client that do not otherwise specify
``timeout_ms``.

For more information on overrides and specific options, see the :ref:`Overrides
<ruby-csot-overrides>` section.
To learn more about overriding the timeout option and setting other options, see the
following :ref:`ruby-csot-overrides` section.

.. _ruby-csot-overrides:

Expand All @@ -139,9 +144,11 @@ configuration can override a client-level ``timeout_ms`` configuration:
Transactions
~~~~~~~~~~~~

When you create a new ``Mongo::Session`` instance to implement a transaction, use
the ``default_timeout_ms`` option. You can set ``default_timeout_ms`` to specify the
``timeout_ms`` values to use for:
You can set a timeout for transactions by using the ``default_timeout_ms`` client option.

When you create a new ``Mongo::Session`` instance to implement a transaction,
you can set the ``default_timeout_ms`` client option to specify the
``timeout_ms`` values for the following methods:

- `commit_transaction <{+api-root+}/Mongo/Session.html#commit_transaction-instance_method>`__
- `abort_transaction <{+api-root+}/Mongo/Session.html#abort_transaction-instance_method>`__
Expand All @@ -159,8 +166,8 @@ You can only set a timeout value for the
method by using the ``timeout_ms`` option.

You cannot override ``default_timeout_ms`` by setting the ``timeout_ms`` option on an
operation in a transaction session provided by the ``with_transaction`` callback.
Doing so throws an error.
operation in a transaction session provided by the ``with_transaction`` callback,
or the driver throws an error.

Client Encryption
~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -215,7 +222,7 @@ This is the default mode for all tailable cursors,
such as the tailable cursors returned by the ``find`` method on capped
collections or change streams.

The following code example iterates over documents in the ``mflix`` collection
The following code example iterates over documents in a movie collection
by using an iterable cursor, and then fetches and logs the ``imdb`` information for
each movie document:

Expand Down
Loading