@@ -73,6 +73,7 @@ mongoc_topology_reconcile (const mongoc_topology_t *topology,
7373 mongoc_server_description_t * sd ;
7474 mongoc_topology_scanner_node_t * ele , * tmp ;
7575
76+ BSON_ASSERT (topology -> single_threaded );
7677 servers = mc_tpld_servers (td );
7778 /* Add newly discovered nodes */
7879 for (size_t i = 0u ; i < servers -> items_len ; i ++ ) {
@@ -124,15 +125,16 @@ _mongoc_topology_scanner_setup_err_cb (uint32_t id,
124125{
125126 mongoc_topology_t * topology = BSON_ASSERT_PTR_INLINE (data );
126127
128+ BSON_ASSERT (topology -> single_threaded );
127129 if (_mongoc_topology_get_type (topology ) == MONGOC_TOPOLOGY_LOAD_BALANCED ) {
128130 /* In load balanced mode, scanning is only for connection establishment.
129131 * It must not modify the topology description. */
130132 } else {
131- /* We need to update the topology description */
132- mc_tpld_modification mod = mc_tpld_modify_begin (topology );
133+ // Use `mc_tpld_unsafe_get_mutable` to get a mutable topology description
134+ // without locking. This function only applies to single-threaded clients.
135+ mongoc_topology_description_t * td = mc_tpld_unsafe_get_mutable (topology );
133136 mongoc_topology_description_handle_hello (
134- mod .new_td , id , NULL /* hello reply */ , -1 /* rtt_msec */ , error );
135- mc_tpld_modify_commit (mod );
137+ td , id , NULL /* hello reply */ , -1 /* rtt_msec */ , error );
136138 }
137139}
138140
@@ -159,51 +161,47 @@ _mongoc_topology_scanner_cb (uint32_t id,
159161{
160162 mongoc_topology_t * const topology = BSON_ASSERT_PTR_INLINE (data );
161163 mongoc_server_description_t * sd ;
162- mc_tpld_modification tdmod ;
164+ mongoc_topology_description_t * td ;
163165
166+ BSON_ASSERT (topology -> single_threaded );
164167 if (_mongoc_topology_get_type (topology ) == MONGOC_TOPOLOGY_LOAD_BALANCED ) {
165168 /* In load balanced mode, scanning is only for connection establishment.
166169 * It must not modify the topology description. */
167170 return ;
168171 }
169172
170- tdmod = mc_tpld_modify_begin (topology );
173+ // Use `mc_tpld_unsafe_get_mutable` to get a mutable topology description
174+ // without locking. This function only applies to single-threaded clients.
175+ td = mc_tpld_unsafe_get_mutable (topology );
171176
172- sd = mongoc_topology_description_server_by_id (tdmod . new_td , id , NULL );
177+ sd = mongoc_topology_description_server_by_id (td , id , NULL );
173178
174179 if (!hello_response ) {
175180 /* Server monitoring: When a server check fails due to a network error
176181 * (including a network timeout), the client MUST clear its connection
177182 * pool for the server */
178183 _mongoc_topology_description_clear_connection_pool (
179- tdmod . new_td , id , & kZeroServiceId );
184+ td , id , & kZeroServiceId );
180185 }
181186
182187 /* Server Discovery and Monitoring Spec: "Once a server is connected, the
183188 * client MUST change its type to Unknown only after it has retried the
184189 * server once." */
185190 if (!hello_response && sd && sd -> type != MONGOC_SERVER_UNKNOWN ) {
186- _mongoc_topology_update_no_lock (
187- id , hello_response , rtt_msec , tdmod .new_td , error );
191+ _mongoc_topology_update_no_lock (id , hello_response , rtt_msec , td , error );
188192
189193 /* add another hello call to the current scan - the scan continues
190194 * until all commands are done */
191195 mongoc_topology_scanner_scan (topology -> scanner , sd -> id );
192196 } else {
193- _mongoc_topology_update_no_lock (
194- id , hello_response , rtt_msec , tdmod .new_td , error );
197+ _mongoc_topology_update_no_lock (id , hello_response , rtt_msec , td , error );
195198
196199 /* The processing of the hello results above may have added, changed, or
197200 * removed server descriptions. We need to reconcile that with our
198201 * monitoring agents
199202 */
200- mongoc_topology_reconcile (topology , tdmod .new_td );
201-
202- mongoc_cond_broadcast (& topology -> cond_client );
203+ mongoc_topology_reconcile (topology , td );
203204 }
204-
205-
206- mc_tpld_modify_commit (tdmod );
207205}
208206
209207static void
@@ -881,7 +879,8 @@ mongoc_topology_rescan_srv (mongoc_topology_t *topology)
881879static void
882880mongoc_topology_scan_once (mongoc_topology_t * topology , bool obey_cooldown )
883881{
884- mc_tpld_modification tdmod ;
882+ mongoc_topology_description_t * td ;
883+ BSON_ASSERT (topology -> single_threaded );
885884 if (mongoc_topology_should_rescan_srv (topology )) {
886885 /* Prior to scanning hosts, update the list of SRV hosts, if applicable.
887886 */
@@ -892,9 +891,10 @@ mongoc_topology_scan_once (mongoc_topology_t *topology, bool obey_cooldown)
892891 * description based on hello responses in connection handshakes, see
893892 * _mongoc_topology_update_from_handshake. retire scanner nodes for removed
894893 * members and create scanner nodes for new ones. */
895- tdmod = mc_tpld_modify_begin (topology );
896- mongoc_topology_reconcile (topology , tdmod .new_td );
897- mc_tpld_modify_commit (tdmod );
894+ // Use `mc_tpld_unsafe_get_mutable` to get a mutable topology description
895+ // without locking. This function only applies to single-threaded clients.
896+ td = mc_tpld_unsafe_get_mutable (topology );
897+ mongoc_topology_reconcile (topology , td );
898898
899899 mongoc_topology_scanner_start (topology -> scanner , obey_cooldown );
900900 mongoc_topology_scanner_work (topology -> scanner );
920920_mongoc_topology_do_blocking_scan (mongoc_topology_t * topology ,
921921 bson_error_t * error )
922922{
923+ BSON_ASSERT (topology -> single_threaded );
923924 _mongoc_handshake_freeze ();
924925
925926 mongoc_topology_scan_once (topology , true /* obey cooldown */ );
@@ -1162,9 +1163,11 @@ mongoc_topology_select_server_id (mongoc_topology_t *topology,
11621163
11631164 if (topology -> single_threaded ) {
11641165 if (!td .ptr -> opened ) {
1165- mc_tpld_modification tdmod = mc_tpld_modify_begin (topology );
1166- _mongoc_topology_description_monitor_opening (tdmod .new_td );
1167- mc_tpld_modify_commit (tdmod );
1166+ // Use `mc_tpld_unsafe_get_mutable` to get a mutable topology
1167+ // description without locking. This block only applies to
1168+ // single-threaded clients.
1169+ _mongoc_topology_description_monitor_opening (
1170+ mc_tpld_unsafe_get_mutable (topology ));
11681171 mc_tpld_renew_ref (& td , topology );
11691172 }
11701173
0 commit comments