@@ -547,19 +547,6 @@ func (t *Topology) pollSRVRecords() {
547
547
t .pollHeartbeatTime .Store (false )
548
548
}
549
549
550
- // If t.cfg.srvMaxHosts is non-zero and is less than the number of hosts, randomly
551
- // select srvMaxHosts hosts from parsedHosts using the modern Fisher-Yates
552
- // algorithm.
553
- if t .cfg .srvMaxHosts != 0 && t .cfg .srvMaxHosts < len (parsedHosts ) {
554
- // TODO(GODRIVER-1876): Use rand#Shuffle after dropping Go 1.9 support.
555
- n := len (parsedHosts )
556
- for i := 0 ; i < n - 1 ; i ++ {
557
- j := i + random .Intn (n - i )
558
- parsedHosts [j ], parsedHosts [i ] = parsedHosts [i ], parsedHosts [j ]
559
- }
560
- parsedHosts = parsedHosts [:t .cfg .srvMaxHosts ]
561
- }
562
-
563
550
cont := t .processSRVResults (parsedHosts )
564
551
if ! cont {
565
552
break
@@ -598,11 +585,25 @@ func (t *Topology) processSRVResults(parsedHosts []string) bool {
598
585
t .fsm .removeServerByAddr (addr )
599
586
t .publishServerClosedEvent (s .address )
600
587
}
588
+
589
+ // Now that we've removed all the hosts that disappeared from the SRV record, we need to add any
590
+ // new hosts added to the SRV record. If adding all of the new hosts would increase the number
591
+ // of servers past srvMaxHosts, shuffle the list of added hosts.
592
+ if t .cfg .srvMaxHosts > 0 && len (t .servers )+ len (diff .Added ) > t .cfg .srvMaxHosts {
593
+ random .Shuffle (len (diff .Added ), func (i , j int ) {
594
+ diff .Added [i ], diff .Added [j ] = diff .Added [j ], diff .Added [i ]
595
+ })
596
+ }
597
+ // Add all added hosts until the number of servers reaches srvMaxHosts.
601
598
for _ , a := range diff .Added {
599
+ if t .cfg .srvMaxHosts > 0 && len (t .servers ) >= t .cfg .srvMaxHosts {
600
+ break
601
+ }
602
602
addr := address .Address (a ).Canonicalize ()
603
603
_ = t .addServer (addr )
604
604
t .fsm .addServer (addr )
605
605
}
606
+
606
607
//store new description
607
608
newDesc := description.Topology {
608
609
Kind : t .fsm .Kind ,
0 commit comments