|
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