@@ -613,12 +613,50 @@ mongoc_topology_apply_scanned_srv_hosts (mongoc_uri_t *uri,
613
613
return had_valid_hosts ;
614
614
}
615
615
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
+
616
651
/*
617
652
*--------------------------------------------------------------------------
618
653
*
619
654
* mongoc_topology_rescan_srv --
620
655
*
621
656
* 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).
622
660
*
623
661
* NOTE: this method expects @topology's mutex to be locked on entry.
624
662
*
@@ -633,18 +671,9 @@ mongoc_topology_rescan_srv (mongoc_topology_t *topology)
633
671
int64_t scan_time_ms ;
634
672
bool ret ;
635
673
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 ));
641
675
642
676
service = mongoc_uri_get_service (topology -> uri );
643
- if (!service ) {
644
- /* Only rescan if we have a mongodb+srv:// URI. */
645
- return ;
646
- }
647
-
648
677
scan_time_ms = topology -> srv_polling_last_scan_ms +
649
678
topology -> srv_polling_rescan_interval_ms ;
650
679
if (bson_get_monotonic_time () / 1000 < scan_time_ms ) {
@@ -723,8 +752,10 @@ mongoc_topology_rescan_srv (mongoc_topology_t *topology)
723
752
static void
724
753
mongoc_topology_scan_once (mongoc_topology_t * topology , bool obey_cooldown )
725
754
{
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
+ }
728
759
729
760
/* since the last scan, members may be added or removed from the topology
730
761
* description based on ismaster responses in connection handshakes, see
0 commit comments