Skip to content

Commit d222951

Browse files
pierwillrustagir
andauthored
DOCSP-30506 Async and Sync Runtimes (#54)
Co-authored-by: Rea Rustagi <[email protected]>
1 parent 294a906 commit d222951

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fundamentals
2121
/fundamentals/tracing-logging
2222
/fundamentals/run-command
2323
/fundamentals/performance
24+
/fundamentals/runtimes
2425

2526
..
2627
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>

source/fundamentals/runtimes.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
.. _rust-runtimes:
2+
3+
=================================
4+
Asynchronous and Synchronous APIs
5+
=================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn about the {+driver-short+}'s :ref:`asynchronous <rust-runtimes-configure-async>`
17+
and :ref:`synchronous <rust-runtimes-configure-sync>` APIs.
18+
This guide explains how to enable the available APIs and structure your code to use each.
19+
20+
The {+driver-short+} supports ``tokio`` and ``async-std``, two popular asynchronous runtime crates.
21+
By default, the driver uses the ``tokio`` asynchronous runtime, but you can select a specific runtime
22+
by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
23+
24+
The driver also includes a synchronous API for use cases that require blocking, or when parallelism is not necessary.
25+
You can select the synchronous API by adding feature flags to the ``mongodb`` dependency in your ``Cargo.toml`` file.
26+
27+
.. _rust-runtimes-configure-async:
28+
29+
Configure the Asynchronous Runtime
30+
----------------------------------
31+
32+
The driver uses the ``tokio`` runtime by default.
33+
You can explicitly choose a runtime by adding the ``"tokio-runtime"`` or ``"async-std-runtime"``
34+
feature flags to the ``mongodb`` dependency.
35+
36+
Select from the following tabs to see how to add feature flags for each corresponding crate:
37+
38+
.. tabs::
39+
40+
.. tab:: tokio
41+
:tabid: asynchronous-api-tokio
42+
43+
.. code-block:: toml
44+
45+
[dependencies.mongodb]
46+
version = "{+version+}"
47+
features = ["tokio-runtime"]
48+
49+
.. tab:: async-std
50+
:tabid: asynchronous-api-async-std
51+
52+
.. code-block:: toml
53+
54+
[dependencies.mongodb]
55+
version = "{+version+}"
56+
default-features = false
57+
features = ["async-std"]
58+
59+
For more information on installing the driver and adding feature flags,
60+
see the :ref:`rust-quick-start-download-and-install` step of the Quick Start.
61+
62+
Tokio Runtime Example
63+
~~~~~~~~~~~~~~~~~~~~~
64+
65+
The following code uses the ``task`` module from the ``tokio`` crate
66+
to create separate, concurrent tasks for multiple data operations:
67+
68+
.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-tokio.rs
69+
:language: rust
70+
:dedent:
71+
72+
.. _rust-runtimes-configure-sync:
73+
74+
Configure the Synchronous API
75+
-----------------------------
76+
77+
The driver also provides a blocking, synchronous API.
78+
To use the synchronous API, add the either the ``"sync"`` or ``"tokio-sync"`` feature flag
79+
to the ``mongodb`` dependency.
80+
81+
Select from the following tabs to see how to add feature flags for each corresponding crate:
82+
83+
.. tabs::
84+
85+
.. tab:: sync
86+
:tabid: synchronous-api
87+
88+
.. code-block:: toml
89+
90+
[dependencies.mongodb]
91+
version = "{+version+}"
92+
default-features = false
93+
features = ["sync"]
94+
95+
.. tab:: tokio-sync
96+
:tabid: synchronous-api-tokio
97+
98+
.. code-block:: toml
99+
100+
[dependencies.mongodb]
101+
version = "{+version+}"
102+
default-features = false
103+
features = ["tokio-sync"]
104+
105+
Synchronous Code Example
106+
~~~~~~~~~~~~~~~~~~~~~~~~
107+
108+
When using the synchronous API, use types from the ``mongodb::sync`` module to perform operations.
109+
The following code uses the ``sync`` module to insert data into a collection using the synchronous API.
110+
When the ``insert_one`` method runs inside the ``for`` loop, the driver waits for each request to complete before continuing.
111+
112+
.. literalinclude:: /includes/fundamentals/code-snippets/runtimes-sync.rs
113+
:language: rust
114+
:dedent:
115+
116+
Use Both Asynchronous and Synchronous APIs
117+
------------------------------------------
118+
119+
You can use both asynchronous and synchronous APIs in the same application.
120+
For example, to enable both ``tokio`` runtimes, you can add the ``tokio`` dependency to your dependencies list, and add the
121+
``"tokio-sync"`` flag to the ``mongodb`` dependency:
122+
123+
.. code-block:: toml
124+
125+
[dependencies]
126+
futures = "0.3.28"
127+
tokio = {version = "1.32.0", features = ["full"]}
128+
129+
[dependencies.mongodb]
130+
version = "{+version+}"
131+
features = ["tokio-sync"]
132+
133+
Additional Information
134+
----------------------
135+
136+
For more information about the concepts in this guide, see the following pages:
137+
138+
- :ref:`Performance Considerations <rust-performance>`
139+
- `Asynchronous Programming in Rust <https://rust-lang.github.io/async-book/>`__
140+
141+
API Documentation
142+
~~~~~~~~~~~~~~~~~
143+
144+
To learn more about the methods and types discussed in this
145+
guide, see the following API Documentation:
146+
147+
- `Client <{+api+}/struct.Client.html>`__
148+
- `sync <{+api+}/sync/index.html>`__

source/includes/fundamentals-sections.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Fundamentals section:
1414
- :ref:`Record Driver Events <rust-tracing-logging>`
1515
- :ref:`Run A Database Command <rust-run-command>`
1616
- :ref:`Optimize Driver Performance <rust-performance>`
17+
- :ref:`Configure Asynchronous and Synchronous Runtimes <rust-runtimes>`
1718

1819
..
1920
- :atlas:`Connect to MongoDB Atlas from AWS Lambda </manage-connections-aws-lambda/>`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use mongodb::sync::Client;
2+
3+
fn main() {
4+
let client = Client::with_uri_str("<connection string>")?;
5+
let some_data = doc! { "title": "1984", "author": "George Orwell" };
6+
7+
for i in 0..5 {
8+
let client_ref = client.clone();
9+
let somedata_ref = some_data.clone();
10+
11+
let collection = client_ref
12+
.database("items")
13+
.collection::<Document>(&format!("coll{}", i));
14+
15+
collection.insert_one(somedata_ref, None);
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let client = Client::with_uri_str("<connection string>").await?;
2+
let some_data = doc! { "title": "1984", "author": "George Orwell" };
3+
4+
for i in 0..5 {
5+
let client_ref = client.clone();
6+
let somedata_ref = some_data.clone();
7+
8+
task::spawn(async move {
9+
let collection = client_ref
10+
.database("items")
11+
.collection::<Document>(&format!("coll{}", i));
12+
13+
collection.insert_one(somedata_ref, None).await
14+
});
15+
}

0 commit comments

Comments
 (0)