Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1877c6b
v2.1+ client bw link in parallelism note
rustagir May 1, 2025
16332ab
DOCSP-49699: compound ops page typo fix (#496)
rustagir May 1, 2025
841569f
remove screenshots
rachel-mack May 2, 2025
ece4c15
link description
rachel-mack May 2, 2025
a750d0b
MW feedback
rachel-mack May 5, 2025
b6885b3
Merge pull request #497 from rachel-mack/DOCSP-49650
rachel-mack May 5, 2025
d012d8e
DOCSP-49853: 2.2.1 release (#498)
norareidy May 7, 2025
295483e
DOCSP-50090: Add netlify links action (#500)
norareidy May 15, 2025
9e905c7
DOCSP-50026: remove eol server versions (#501)
norareidy May 22, 2025
44cd686
DOCSP-50565: Go v2.2.2 (#510)
norareidy Jun 6, 2025
301b107
DOCSP-49780 Update Go min version (#523)
stephmarie17 Jul 7, 2025
eb00c66
bson raw
shuangela Jul 28, 2025
ecb45f0
melanie feedback
shuangela Jul 28, 2025
46b471f
Merge pull request #549 from shuangela/DOCSP-51805-bson-raw
shuangela Jul 29, 2025
ccd5fd5
DOCSP-47019 Shift Go ToC (#499)
lindseymoore May 19, 2025
a718c6d
DOCSP-47018 Authentication Reorg (#502)
lindseymoore May 29, 2025
24afc53
DOCSP-49975 Specify Documents to Return (#503)
lindseymoore Jun 2, 2025
6d53e53
DOCSP-47020 Databases and collections (#504)
stephmarie17 Jun 4, 2025
4920445
DOCSP-49977 Replace One (#506)
lindseymoore Jun 6, 2025
a28f89e
DOCSP-47023 Connection pools (#505)
stephmarie17 Jun 6, 2025
99c072b
DOCSP-49972 removing extra files
mballard-mdb Jun 9, 2025
125da2e
DOCSP-49972 Suggestions from review
mballard-mdb Jun 9, 2025
732ace1
DOCS-49972 nits
mballard-mdb Jun 9, 2025
66bab2f
DOCSP-47022 Add link to replication manual (#519)
stephmarie17 Jun 13, 2025
25aeea7
DOCSP-47006 Retryable Writes and Reads (#518)
stephmarie17 Jun 13, 2025
e121dc0
DOCSP-49973 Create a MongoClient (#507)
stephmarie17 Jun 13, 2025
86114d5
DOCSP-49986: TLS (#520)
rustagir Jun 17, 2025
2bb47e5
DOCSP-50749: agg tutorial link (#521)
rustagir Jun 17, 2025
576726b
DOCSP-49622 Atlas Search (#508)
lindseymoore Jun 17, 2025
1e86fb8
DOCSP-49976 Update Documents (#517)
lindseymoore Jun 26, 2025
5b07d05
DOCSP-49982 Monitor Application Events (#525)
lindseymoore Jun 30, 2025
1f13e58
DOCSP-50493: quick reference cleanup (#527)
rustagir Jul 2, 2025
1b5942a
DOCSP-49621 Customize cluster settings (#522)
stephmarie17 Jul 2, 2025
720e82d
DOCSP-47011: Extended JSON (#528)
rustagir Jul 3, 2025
df0eb21
DOCSP-51236: Remove support for MONGODB-CR auth
mcmorisi Jul 7, 2025
796b358
Fix
mcmorisi Jul 7, 2025
0c754b6
DOCSP-49971: csot cc (#526)
rustagir Jul 7, 2025
60219e9
DOCSP-49974 Configure CRUD Operations (#524)
stephmarie17 Jul 8, 2025
5c35c91
DOCSP-49623 Upgrade versions (#531)
stephmarie17 Jul 8, 2025
7ff7bfe
DOCSP-47007 DNS Service Discovery (#533)
lindseymoore Jul 9, 2025
bdcaabc
DOCSP-47012 Document load balancers (#532)
stephmarie17 Jul 11, 2025
47f5e69
DOCSP-51389 Add Go Specific Pages (#534)
lindseymoore Jul 11, 2025
a936b83
DOCSP-49981 Monitor data with change streams (#535)
stephmarie17 Jul 14, 2025
7dfcf63
DOCSP-49984 Check release note refs (#537)
lindseymoore Jul 14, 2025
f190cc1
DOCSP-51826 Connection Str as Env Var (#536)
lindseymoore Jul 14, 2025
3cab7ed
DOCSP-51817 Move update usage examples (#540)
stephmarie17 Jul 22, 2025
db2c16e
DOCSP-51815 Move and standardize find usage exs (#539)
lindseymoore Jul 28, 2025
8c5059c
DOCSP-51822 Standardize count usage ex (#542)
lindseymoore Jul 28, 2025
d761004
DOCSP-51818 Standardize replace usage ex (#541)
lindseymoore Jul 28, 2025
8f57373
DOCSP-51819 Standardize and move delete usage examples (#544)
stephmarie17 Jul 28, 2025
0a773e1
DOCSP-52409 Remove struct filter from update usage ex (#550)
stephmarie17 Jul 28, 2025
c0c7413
DOCSP-51823 Standardize Distinct usage example (#543)
lindseymoore Jul 28, 2025
2558161
DOCSP-51824 Standardize Run a Command usage ex (#545)
lindseymoore Jul 28, 2025
98d2d3d
DOCSP-51816 Move and standardize insert usage examples (#538)
stephmarie17 Jul 29, 2025
4621b3c
DOCSP-51820 Move and standardize bulk operations usage example (#548)
stephmarie17 Jul 29, 2025
2feca6f
DOCSP-51825 Standardize Struct Tag Usage ex (#546)
lindseymoore Jul 29, 2025
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
Prev Previous commit
Next Next commit
DOCSP-49971: csot cc (#526)
  • Loading branch information
rustagir authored and rachel-mack committed Jul 30, 2025
commit 0c754b6749428f7fe2df79075c8d2ac285dcac9e
233 changes: 232 additions & 1 deletion source/connect/connection-options/csot.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,237 @@
.. _golang-timeout-setting:
.. _golang-csot:

===========================
Limit Server Execution Time
===========================

.. TODO
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

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

Overview
--------

In this guide, you can learn about the single timeout setting in the
{+driver-short+}, also known as the **client-side operation timeout (CSOT)**.

When you use the {+driver-short+} to perform a server operation, you can also
limit the amount of time in which the server can finish the operation.
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.

.. note:: Experimental Feature

The CSOT feature is experimental and might change in future driver
releases.

timeoutMS Option
----------------

To specify a timeout when connecting to a MongoDB deployment, set the
``timeoutMS`` connection option to the timeout length in milliseconds. You can
set the ``timeoutMS`` option in the following ways:

- Calling the ``SetTimeout()`` method when
specifying options for your ``Client`` instance
- Setting the ``timeoutMS`` parameter in your connection string

The following code examples set a client-level timeout of ``200`` milliseconds.
Select the :guilabel:`Client` or :guilabel:`Connection
String` tab to see the corresponding code.

.. tabs::

.. tab:: MongoClientSettings
:tabid: mongoclientsettings

.. literalinclude:: /includes/connect/csot.go
:language: go
:start-after: start-client-opts
:end-before: end-client-opts
:dedent:

.. tab:: Connection String
:tabid: connection-string

.. code-block:: go
:copyable: true

uri := "<connection string>/?timeoutMS=200"
client, err := mongo.Connect(options.Client().ApplyURI(uri))

.. note:: Retries Under Timeout Specification

If you set a timeout on your ``Client`` or in an operation-level
Context and the server returns a retryable error, the driver retries the
operation as many times as possible before the timeout expires.

Once the timeout expires, the driver returns a timeout error.
See the Server manual for more information about :ref:`retryable
reads <retryable-reads>` and :manual:`retryable writes
</core/retryable-writes/>`.

Accepted Timeout Values
~~~~~~~~~~~~~~~~~~~~~~~

The following table describes the timeout behavior corresponding to the
accepted values for ``timeoutMS``:

.. list-table::
:header-rows: 1
:widths: 25 75

* - Value
- Behavior

* - Positive integer
- Sets the timeout to use for operation completion.

* - ``0``
- Specifies that operations never time out.

* - ``null`` or unset
- | Defers the timeout behavior to the following settings:

- :manual:`waitQueueTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.waitQueueTimeoutMS>`
- :manual:`socketTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.socketTimeoutMS>`
- :manual:`wTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.wtimeoutMS>`
- :manual:`maxTimeMS </reference/method/cursor.maxTimeMS/>`

| These settings are deprecated and are ignored if you set ``timeoutMS``.

If you specify the ``timeoutMS`` option, the driver automatically applies the
specified timeout to each server operation.

Timeout Inheritance
~~~~~~~~~~~~~~~~~~~

By default, all operations in your application inherit the
``Timeout`` option from ``Client`` if you do not set a different timeout
on specific operations in the operation's Context.

If you set a timeout on a Context passed into an operation, the driver uses
that value for the operation. If you do not specify a Context timeout,
the operation Context derives the timeout from the ``Client`` instance.

The following table describes how the timeout value is inherited at each level:

.. list-table::
:header-rows: 1
:widths: 30 70

* - Level
- Inheritance Description

* - Operation
- Takes the highest precedence and overrides the timeout
options that you set at any other level.

* - Transaction
- Takes precedence over the timeout value that you set at the
client level.

* - Client
- Applies to all databases, collections, sessions, transactions, and
operations within that client that do not otherwise specify a
Context timeout.

For more information on overrides and specific options, see the following
:ref:`golang-csot-overrides` section.

.. _golang-csot-overrides:

Overrides
---------

The {+driver-short+} supports various levels of configuration to control the
behavior and performance of database operations.

You can specify a ``timeoutMS`` option at a more specific level to override the
client-level configuration. The table in the preceding section describes
the levels at which you can specify a timeout setting. This allows you
to customize timeouts based on the needs of individual operations.

The following example shows how to set an operation-level timeout in a
Context, which takes priority over the client-level timeout:

.. literalinclude:: /includes/connect/csot.go
:language: go
:start-after: start-override
:end-before: end-override
:dedent:
:emphasize-lines: 9, 12

.. _go-csot-transaction:

Transactions
~~~~~~~~~~~~

When you perform a transaction by using the `WithTransaction()
<{+api+}/mongo#Session.WithTransaction>`__ method, you can apply a
timeout to the transaction operations by setting a timeout within the
Context.

The following code demonstrates how to set a Context timeout when
calling the ``WithTransaction()`` method to perform a transaction:

.. literalinclude:: /includes/connect/csot.go
:language: go
:start-after: start-txn-context
:end-before: end-txn-context
:dedent:
:emphasize-lines: 1, 4

If you do not specify a Context timeout, the driver inherits the timeout
value set on the parent ``Client``.

You can also pass Context timeouts to the following session
methods:

- ``AbortTransaction()``
- ``CommitTransaction()``
- ``EndSession()``

To learn more about transactions, see the :ref:`golang-transactions`
guide.

.. _go-csot-cursor:

Cursors
-------

Cursors offer configurable timeout settings when using the CSOT feature. You can
adjust cursor timeouts by passing Contexts that have timeout
specifications to `Cursor <{+api+}/mongo#Cursor>`__ methods.

For operations that create cursors, the timeout setting can either limit
the lifetime of the cursor or be applied separately to the original
operation and all subsequent calls.

For example, if you pass a Context timeout to the ``Cursor.Next()``
method, the timeout applies to each action to fetch a result document.
If you pass a Context timeout to the ``Cursor.All()`` method, the
timeout applies to the entire lifetime of the cursor.

To learn more about cursors, see the :ref:`golang-cursor` guide.

API Documentation
-----------------

To learn more about using timeouts with the {+driver-short+}, see the following
API documentation:

- `ClientOptions <{+api+}/mongo/options#ClientOptions>`__
- `SetTimeout() <{+api+}/mongo/options#ClientOptions.SetTimeout>`__
- `Context.WithTimeout() <https://pkg.go.dev/context#WithTimeout>`__
83 changes: 3 additions & 80 deletions source/connect/specify-connection-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ default value, and a description of the option.
- ``null``
- Specifies the number of milliseconds that a single operation run on the
``Client`` can take before returning a timeout error. Operations honor
this setting only if there is no deadline on the operation Context.
this setting only if there is no deadline on the operation
Context. To learn more about this option, see the :ref:`CSOT
<golang-csot>` guide.

* - **connectTimeoutMS**
- integer
Expand Down Expand Up @@ -127,82 +129,3 @@ default value, and a description of the option.
For a full list of connection options, see the `ClientOptions API
documentation
<{+api+}/mongo/options#ClientOptions>`__.

.. _golang-timeout-setting:

Single Timeout Setting
----------------------

You can set a single ``Timeout`` option on your ``Client`` instance to
specify the maximum amount of time that a single operation can take to
execute.

Set a client-level timeout by calling the ``SetTimeout()`` method when
specifying options for your ``Client`` instance or by specifying the
``timeoutMS`` option in your connection URI. By default, all
``Database``, ``Collection``, ``Session``, ``ChangeStream``, and
``Bucket`` instances elsewhere in your application inherit the
``Timeout`` option from ``Client`` if you do not set a different timeout
on specific operations in the operation's Context.

If you set a timeout on a Context passed into an operation, the driver uses
that value for the operation. If you do not specify a Context timeout,
the operation Context derives the timeout from the ``Client`` instance.

.. note:: Retries under Timeout Specification

If you set a timeout on your ``Client`` or in an operation-level
Context and the server returns a retryable error, the driver retries the
operation as many times as possible before the timeout expires.

Once the timeout expires, the driver returns a timeout error.
See the Server manual for more information about :ref:`retryable
reads <retryable-reads>` and :manual:`retryable writes
</core/retryable-writes/>`.

Timeout Examples
~~~~~~~~~~~~~~~~

This section provides examples that demonstrate different ways to set a
timeout in your application.

Client Option
^^^^^^^^^^^^^

The following code shows how to set the ``Timeout`` option on a ``Client``
by using the ``SetTimeout()`` method:

.. code-block:: go

opts := options.Client().SetTimeout(5 * time.Second)
client, err := mongo.Connect(opts)

Connection String Option
^^^^^^^^^^^^^^^^^^^^^^^^

The following example shows how to set a single timeout by using the
``timeoutMS`` URI option. Then, the code executes an insert operation
that inherits the timeout:

.. code-block:: go
:emphasize-lines: 1, 5

uri := "mongodb://user:pass@sample.host:27017/?timeoutMS=5000"
client, err := mongo.Connect(options.Client().ApplyURI(uri))

...
coll.InsertOne(context.Background(), doc)

Operation Timeout
^^^^^^^^^^^^^^^^^

The following example shows how to set an operation-level timeout in a
Context, which takes priority over a client-level timeout you might have
set:

.. code-block:: go

ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

res, err := coll.InsertOne(ctx, bson.D{{"x", 2}})
59 changes: 59 additions & 0 deletions source/includes/connect/csot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"log"
"time"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)

func main() {

// start-client-opts
opts := options.Client().SetTimeout(200 * time.Millisecond)
client, err := mongo.Connect(opts)
// end-client-opts

if err != nil {
log.Fatal(err)
}

// start-override
opts = options.Client().SetTimeout(200 * time.Millisecond)
client, err = mongo.Connect(opts)
if err != nil {
log.Fatal(err)
}

coll := client.Database("db").Collection("people")

ctx, cancel := context.WithTimeout(context.TODO(), 300*time.Millisecond)
defer cancel()

_, err = coll.InsertOne(ctx, bson.D{{"name", "Agnes Georgiou"}})
// end-override

session, err := client.StartSession()
if err != nil {
log.Fatal(err)
}
defer session.EndSession(context.TODO())

// start-txn-context
txnContext, cancel := context.WithTimeout(context.TODO(), 300*time.Millisecond)
defer cancel()

result, err := session.WithTransaction(txnContext, func(ctx context.Context) (string, error) {
// Perform transaction operations
})
// end-txn-context

defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
log.Fatal(err)
}
}()
}