Skip to content

Commit 18455ad

Browse files
DOCSP-15527 fundamentals context (#9)
* added Context page
1 parent a9d57ae commit 18455ad

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

config/intersphinx.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Weirdly, giza wants a non-empty list of two or more, so we have to include extraneous/unused one --hence the python
2+
name: python
3+
url: https://docs.python.org/2/
4+
path: python2.inv
5+
---
6+
name: mongodb
7+
url: https://docs.mongodb.com/manual/
8+
path: mongodb.inv
9+
...

snooty.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ toc_landing_pages = [
66
"/usage-examples"
77
]
88

9+
intersphinx = [
10+
"https://docs.mongodb.com/manual/objects.inv",
11+
"https://docs.atlas.mongodb.com/objects.inv",
12+
"https://docs.python.org/2/python2.inv"
13+
]
14+
915
[constants]
1016
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
1117
driver-branch = "v1.7.0" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Fundamentals
99
:maxdepth: 1
1010

1111
/fundamentals/connection
12+
/fundamentals/context
1213
/fundamentals/auth
1314
/fundamentals/bson
1415

source/fundamentals/context.txt

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,119 @@
1-
TODO: Discussion of the ``context`` package
1+
=======
2+
Context
3+
=======
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
The MongoDB Go Driver uses the `context package
17+
<https://pkg.go.dev/context>`__ from Go's standard library to allow
18+
applications to signal timeouts and cancellations for any **blocking function**
19+
call. A blocking function relies on an external event, such as a network
20+
input or output, to proceed with its task.
21+
22+
An example of a blocking function in the Go Driver is the ``Insert()``
23+
function. If you want to perform an insert operation on a ``Collection``
24+
within 10 seconds, you can use a ``Context`` with a timeout. If the
25+
operation doesn't complete within the timeout, the function returns
26+
an error.
27+
28+
.. code-block:: go
29+
:emphasize-lines: 3
30+
31+
client := mongo.Connect(context.TODO())
32+
33+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
34+
defer cancel()
35+
client.Database("<db>").Collection("<collection>").Insert(ctx, bson.D{{"x",1}})
36+
37+
Expiration
38+
----------
39+
40+
The driver considers a Context expired when an operation exceeds its
41+
timeout or is canceled. The driver checks the Context expiration with
42+
the ``Done()`` function.
43+
44+
The following sections describe when and how the driver checks for
45+
expiration.
46+
47+
Server Selection
48+
~~~~~~~~~~~~~~~~
49+
50+
The driver might block a function call if it can't select a server for
51+
an operation.
52+
53+
In this scenario, the driver loops until it finds a server to use for the
54+
operation. After each iteration, the driver returns a server selection
55+
timeout error if the Context expired or the selection process took
56+
longer than the ``serverSelectionTimeoutMS`` setting.
57+
58+
For more information on how the driver selects a server, see the
59+
:ref:`<replica-set-read-preference-behavior>`.
60+
61+
Connection Checkout
62+
~~~~~~~~~~~~~~~~~~~
63+
64+
The driver might block a function call if there are no available
65+
connections to check out.
66+
67+
After selecting a server, the driver tries to check out a connection
68+
from the server’s connection pool. If the Context expires while checking
69+
out a connection, the function returns a timeout error.
70+
71+
Connection Establishment
72+
~~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
The driver might block an function call if it needs to create a new
75+
connection.
76+
77+
When the driver needs to create a new connection to execute an
78+
operation, the Context sets a timeout for the establishment process. The
79+
driver sets the timeout to either the Context expiration or connection
80+
timeout, whichever is shorter.
81+
82+
The following example sets the connection timeout to *1* second and the
83+
Context deadline to *2* seconds. Because the connection timeout is
84+
shorter, the establishment process expires after *1* second.
85+
86+
.. code-block:: go
87+
:emphasize-lines: 2, 5
88+
89+
opts := options.Client()
90+
opts.SetConnectTimeout(1*time.Second)
91+
client := mongo.Connect(context.TODO(), opts)
92+
93+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
94+
defer cancel()
95+
client.Database("<db>").Collection("<collection>").InsertOne(ctx, bson.D{{"x",1}})
96+
97+
Socket Read and Write
98+
~~~~~~~~~~~~~~~~~~~~~
99+
100+
When the driver retrieves a connection for an operation, it sets the
101+
socket’s read or write deadline to either the Context deadline or socket
102+
timeout, whichever is shorter.
103+
104+
.. important::
105+
106+
If you cancel the Context after the execution of the ``Read()`` or
107+
``Write()`` function but before its deadline, the behavior of the driver
108+
differs based on version.
109+
110+
In versions prior to 1.5.0, the driver doesn't detect the Context
111+
cancellation and waits for the ``Read()`` or ``Write()`` function to
112+
return.
113+
114+
In versions 1.5.0 and later, the driver generates a separate
115+
goroutine to listen for Context cancellation when the ``Read()`` or
116+
``Write()`` function is in progress. If the goroutine detects a
117+
cancellation, it closes the connection. The pending ``Read()`` or
118+
``Write()`` function returns an error which the driver overwrites with
119+
the ``context.Canceled`` error.

0 commit comments

Comments
 (0)