Skip to content

Commit d0079dc

Browse files
authored
DOCSP-51389 Add Go Specific Pages (#534)
* DOCSP-51389 Add Go Specific Pages * add context to index * fix ref on struct tagging * ref in context page * touch ups * Todo and remove usage example mention * add redirects * style
1 parent df331a6 commit d0079dc

File tree

12 files changed

+213
-3
lines changed

12 files changed

+213
-3
lines changed

config/redirects

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ raw: ${prefix}/master -> ${base}/upcoming/
2020
[*-master]: ${prefix}/${version}/fundamentals/crud/write-read-pref/ -> ${base}/${version}/crud/configure/
2121
[*-master]: ${prefix}/${version}/fundamentals/collations/ -> ${base}/${version}/crud/configure/
2222

23+
# CC Redirects
24+
[v1.12-master]: ${prefix}/${version}/usage-examples/struct-tagging/ -> ${base}/${version}/data-formats/struct-tagging/
25+
[v1.12-master]: ${prefix}/${version}/fundamentals/context/ -> ${base}/${version}/context/

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name = "golang"
22
title = "Go Driver"
33
toc_landing_pages = [
44
"/connect",
5+
"/context",
56
"/crud",
67
"/crud/update",
78
"/monitoring-and-logging",

source/archive-reference-files/fundamentals/context.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.. _golang-context:
21

32
=======
43
Context

source/archive-reference-files/usage-examples/findOne.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.. _golang-find-one:
21

32
===============
43
Find a Document

source/archive-reference-files/usage-examples/struct-tagging.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.. _golang-struct-tags-usage-example:
21

32
===============
43
Use Struct Tags

source/connect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Connection Guide
2828
Choose a Connection Target </connect/connection-targets>
2929
Connection Options </connect/specify-connection-options>
3030
Connection Troubleshooting </connect/connection-troubleshooting>
31+
Connect with AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>
3132

3233
Overview
3334
--------

source/context.txt

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

source/crud/configure.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ acknowledgement from the majority of replica set members.
179179
collOpts := options.Collection().SetWriteConcern(collWC)
180180
coll := client.Database("db").Collection("myColl", collOpts)
181181

182+
.. _golang-readconcern:
183+
182184
Read Concern
183185
~~~~~~~~~~~~
184186

source/crud/query/retrieve.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.. _golang-retrieve:
2+
.. _golang-find-one:
23

34
==============
45
Find Documents

source/data-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Specialized Data Formats
2626
:titlesonly:
2727
:maxdepth: 1
2828

29+
Use Struct Tags </data-formats/struct-tagging>
2930
BSON </data-formats/bson>
3031
Extended JSON </data-formats/extended-json>
3132

0 commit comments

Comments
 (0)