|
1 |
| -.. TODO |
| 1 | +.. _golang-server-selection: |
| 2 | + |
| 3 | +========================== |
| 4 | +Customize Cluster Settings |
| 5 | +========================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, read preference, write |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how the {+driver-short+} manages clusters. |
| 24 | + |
| 25 | +Specify Settings |
| 26 | +---------------- |
| 27 | + |
| 28 | +You can specify settings for your clusters by using either a connection string |
| 29 | +or by using the ``ClientOptions`` struct when creating a new ``Client`` |
| 30 | +instance. Select the :guilabel:`Connection String` or :guilabel:`ClientOptions` tab to see the |
| 31 | +available options. |
| 32 | + |
| 33 | +.. tabs:: |
| 34 | + |
| 35 | + .. tab:: Connection String |
| 36 | + :tabid: uri |
| 37 | + |
| 38 | + You can use the following parameters in your connection string to modify the |
| 39 | + driver's behavior when interacting with your MongoDB cluster: |
| 40 | + |
| 41 | + .. list-table:: |
| 42 | + :header-rows: 1 |
| 43 | + :stub-columns: 1 |
| 44 | + :widths: 40 60 |
| 45 | + |
| 46 | + * - Method |
| 47 | + - Description |
| 48 | + |
| 49 | + * - ``serverSelectionTimeoutMS`` |
| 50 | + - Specifies the maximum amount of time the driver will wait for a server to |
| 51 | + be available before throwing an error. |
| 52 | + |
| 53 | + *Default:* 30 seconds |
| 54 | + |
| 55 | + * - ``localThresholdMS`` |
| 56 | + - Specifies the maximum latency in milliseconds for selecting a server. |
| 57 | + |
| 58 | + *Default:* 15 milliseconds |
| 59 | + |
| 60 | + * - ``replicaSet`` |
| 61 | + - Specifies the name of the replica set to connect to. |
| 62 | + |
| 63 | + * - ``directConnection`` |
| 64 | + - Specifies whether to connect directly to a single server, bypassing the |
| 65 | + replica set or sharded cluster. |
| 66 | + |
| 67 | + *Default:* ``false`` |
| 68 | + |
| 69 | + * - ``loadBalanced`` |
| 70 | + - Specifies whether or not the driver is connecting to MongoDB using a |
| 71 | + load balancer. |
| 72 | + |
| 73 | + *Default:* ``false`` |
| 74 | + |
| 75 | + * - ``srvServiceName`` |
| 76 | + - Specifies the service name of the `SRV resource records |
| 77 | + <https://www.rfc-editor.org/rfc/rfc2782>`__ the driver retrieves to |
| 78 | + construct your :manual:`seed list |
| 79 | + </reference/glossary/#std-term-seed-list>`. You must use the |
| 80 | + :manual:`DNS Seed List Connection Format |
| 81 | + </reference/connection-string/#dns-seed-list-connection-format>` in |
| 82 | + your connection string to use this option. |
| 83 | + |
| 84 | + .. tab:: ClientOptions |
| 85 | + :tabid: options |
| 86 | + |
| 87 | + The following table describes some of the methods you can chain to your settings to |
| 88 | + modify the driver's behavior: |
| 89 | + |
| 90 | + .. list-table:: |
| 91 | + :header-rows: 1 |
| 92 | + :stub-columns: 1 |
| 93 | + :widths: 40 60 |
| 94 | + |
| 95 | + * - Field |
| 96 | + - Description |
| 97 | + |
| 98 | + * - ``SetServerSelectionTimeout()`` |
| 99 | + - Specifies the maximum amount of time the driver will wait for a server to |
| 100 | + be available before throwing an error. |
| 101 | + |
| 102 | + *Default:* 30 seconds |
| 103 | + |
| 104 | + * - ``SetLocalThreshold()`` |
| 105 | + - Specifies the maximum latency in milliseconds for selecting a server. |
| 106 | + |
| 107 | + *Default:* 15 milliseconds |
| 108 | + |
| 109 | + * - ``SetReplicaSet()`` |
| 110 | + - Specifies the name of the replica set to connect to. |
| 111 | + |
| 112 | + * - ``SetDirect()`` |
| 113 | + - Specifies whether to connect directly to a single server, bypassing the |
| 114 | + replica set or sharded cluster. |
| 115 | + |
| 116 | + *Default:* ``false`` |
| 117 | + |
| 118 | + * - ``SetLoadBalanced()`` |
| 119 | + - Specifies whether or not the driver is connecting to MongoDB using a |
| 120 | + load balancer. |
| 121 | + |
| 122 | + *Default:* ``false`` |
| 123 | + |
| 124 | + * - ``SetSRVServiceName()`` |
| 125 | + - Specifies a custom service name of the `SRV resource records |
| 126 | + <https://www.rfc-editor.org/rfc/rfc2782>`__ the driver retrieves to |
| 127 | + construct your :manual:`seed list |
| 128 | + </reference/glossary/#std-term-seed-list>`. To use a custom SRV |
| 129 | + service name in SRV discovery, you must call this function before you |
| 130 | + call ``ApplyURI()``. |
| 131 | + |
| 132 | + To learn more about the available methods, see the :ref:`golang-server-selection-resources` section. |
| 133 | + |
| 134 | +Example |
| 135 | +~~~~~~~ |
| 136 | + |
| 137 | +Select the :guilabel:`Connection String` or :guilabel:`ClientOptions` tab to |
| 138 | +see the corresponding example: |
| 139 | + |
| 140 | +.. tabs:: |
| 141 | + |
| 142 | + .. tab:: Connection String |
| 143 | + :tabid: uriExample |
| 144 | + |
| 145 | + The following code uses the connection string to configure the maximum |
| 146 | + server selection timeout to 10 seconds and the local threshold to 15 |
| 147 | + milliseconds: |
| 148 | + |
| 149 | + .. literalinclude:: /includes/connect/cluster-settings-uri.go |
| 150 | + :language: go |
| 151 | + :start-after: start-uri-variable |
| 152 | + :end-before: end-uri-variable |
| 153 | + :dedent: |
| 154 | + |
| 155 | + The following code creates a client and passes the connection string to the |
| 156 | + ``ApplyURI()`` method: |
| 157 | + |
| 158 | + .. literalinclude:: /includes/connect/cluster-settings-uri.go |
| 159 | + :language: go |
| 160 | + :start-after: start-apply-uri |
| 161 | + :end-before: end-apply-uri |
| 162 | + :dedent: |
| 163 | + |
| 164 | + .. tab:: ClientOptions |
| 165 | + :tabid: optionsExample |
| 166 | + |
| 167 | + The following code creates a client and sets the cluster options with a |
| 168 | + maximum server selection timeout of 10 seconds and a local threshold of |
| 169 | + 15 milliseconds: |
| 170 | + |
| 171 | + .. literalinclude:: /includes/connect/cluster-settings-client-options.go |
| 172 | + :language: go |
| 173 | + :start-after: start-client-options |
| 174 | + :end-before: end-client-options |
| 175 | + :dedent: |
| 176 | + |
| 177 | +.. _golang-server-selection-resources: |
| 178 | + |
| 179 | +API Documentation |
| 180 | +----------------- |
| 181 | + |
| 182 | +To learn more about the methods and types in this guide, see the following API documentation: |
| 183 | + |
| 184 | +- `Client <{+api+}/mongo#Client>`__ |
| 185 | +- `ClientOptions <{+api+}/mongo/options#ClientOptions>`__ |
| 186 | +- `ApplyURI() <{+api+}/mongo/options#ClientOptions.ApplyURI>`__ |
0 commit comments