Skip to content

Commit

Permalink
Shuffle peers
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 16, 2024
1 parent 9cb382c commit c14fc97
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,13 @@ func (as *AmbientAutoNAT) getPeerToProbe() peer.ID {
}
}

n := len(peers)
for i, j := rand.Intn(n), 0; j < n; i, j = (i+1)%n, j+1 {
p := peers[i]
// Shuffle peers
for n := len(peers); n > 0; n-- {
randIndex := rand.Intn(n)
peers[n-1], peers[randIndex] = peers[randIndex], peers[n-1]
}

for _, p := range peers {
info := as.host.Peerstore().PeerInfo(p)
// Exclude peers which don't support the autonat protocol.
if proto, err := as.host.Peerstore().SupportsProtocols(p, AutoNATProto); len(proto) == 0 || err != nil {
Expand Down

0 comments on commit c14fc97

Please sign in to comment.