@@ -613,12 +613,50 @@ mongoc_topology_apply_scanned_srv_hosts (mongoc_uri_t *uri,
613613 return had_valid_hosts ;
614614}
615615
616+ /*
617+ *--------------------------------------------------------------------------
618+ *
619+ * mongoc_topology_should_rescan_srv --
620+ *
621+ * Checks whether it is valid to rescan SRV records on the topology.
622+ * Namely, that the topology type is Sharded or Unknown, and that
623+ * the topology URI was configured with SRV.
624+ *
625+ * If this returns false, caller can stop scanning SRV records
626+ * and does not need to try again in the future.
627+ *
628+ * NOTE: this method expects @topology's mutex to be locked on entry.
629+ *
630+ * --------------------------------------------------------------------------
631+ */
632+ bool
633+ mongoc_topology_should_rescan_srv (mongoc_topology_t * topology ) {
634+ const char * service ;
635+
636+ service = mongoc_uri_get_service (topology -> uri );
637+ if (!service ) {
638+ /* Only rescan if we have a mongodb+srv:// URI. */
639+ return false;
640+ }
641+
642+ if ((topology -> description .type != MONGOC_TOPOLOGY_SHARDED ) &&
643+ (topology -> description .type != MONGOC_TOPOLOGY_UNKNOWN )) {
644+ /* Only perform rescan for sharded topology. */
645+ return false;
646+ }
647+
648+ return true;
649+ }
650+
616651/*
617652 *--------------------------------------------------------------------------
618653 *
619654 * mongoc_topology_rescan_srv --
620655 *
621656 * Queries SRV records for new hosts in a mongos cluster.
657+ * Caller must call mongoc_topology_should_rescan_srv before calling
658+ * to ensure preconditions are met (while holding @topology's mutex
659+ * for the duration of both calls).
622660 *
623661 * NOTE: this method expects @topology's mutex to be locked on entry.
624662 *
@@ -633,18 +671,9 @@ mongoc_topology_rescan_srv (mongoc_topology_t *topology)
633671 int64_t scan_time_ms ;
634672 bool ret ;
635673
636- if ((topology -> description .type != MONGOC_TOPOLOGY_SHARDED ) &&
637- (topology -> description .type != MONGOC_TOPOLOGY_UNKNOWN )) {
638- /* Only perform rescan for sharded topology. */
639- return ;
640- }
674+ BSON_ASSERT (mongoc_topology_should_rescan_srv (topology ));
641675
642676 service = mongoc_uri_get_service (topology -> uri );
643- if (!service ) {
644- /* Only rescan if we have a mongodb+srv:// URI. */
645- return ;
646- }
647-
648677 scan_time_ms = topology -> srv_polling_last_scan_ms +
649678 topology -> srv_polling_rescan_interval_ms ;
650679 if (bson_get_monotonic_time () / 1000 < scan_time_ms ) {
@@ -723,8 +752,10 @@ mongoc_topology_rescan_srv (mongoc_topology_t *topology)
723752static void
724753mongoc_topology_scan_once (mongoc_topology_t * topology , bool obey_cooldown )
725754{
726- /* Prior to scanning hosts, update the list of SRV hosts, if applicable. */
727- mongoc_topology_rescan_srv (topology );
755+ if (mongoc_topology_should_rescan_srv (topology )) {
756+ /* Prior to scanning hosts, update the list of SRV hosts, if applicable. */
757+ mongoc_topology_rescan_srv (topology );
758+ }
728759
729760 /* since the last scan, members may be added or removed from the topology
730761 * description based on ismaster responses in connection handshakes, see
0 commit comments