Skip to content

Commit dd2a8a2

Browse files
committed
chore: coalesce replicas by ipaddr
1 parent 401edeb commit dd2a8a2

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

stackslib/src/net/stackerdb/sync.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
7777
dbsync
7878
}
7979

80+
/// Coalesce a list of peers such that each one has a unique IP:port
81+
fn coalesce_peers_by_ipaddr(peers: Vec<NeighborAddress>) -> Vec<NeighborAddress> {
82+
// coalesce peers on the same host:port
83+
let mut same_host_port = HashSet::new();
84+
let unique_ip_peers: Vec<_> = peers
85+
.into_iter()
86+
.filter_map(|naddr| {
87+
if same_host_port.contains(&naddr.addrbytes.to_socketaddr(naddr.port)) {
88+
None
89+
} else {
90+
same_host_port.insert(naddr.addrbytes.to_socketaddr(naddr.port));
91+
Some(naddr)
92+
}
93+
})
94+
.collect();
95+
96+
unique_ip_peers
97+
}
98+
8099
/// Calculate the new set of replicas to contact.
81100
/// This is the same as the set that was connected on the last sync, plus any
82101
/// config hints and discovered nodes from the DB.
@@ -103,7 +122,10 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
103122
peers.extend(extra_peers);
104123
}
105124

106-
for peer in peers {
125+
peers.shuffle(&mut thread_rng());
126+
127+
let unique_ip_peers = Self::coalesce_peers_by_ipaddr(peers);
128+
for peer in unique_ip_peers {
107129
if connected_replicas.len() >= config.max_neighbors {
108130
break;
109131
}
@@ -575,7 +597,9 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
575597
.into_iter()
576598
.map(|neighbor| NeighborAddress::from_neighbor(&neighbor))
577599
.collect();
578-
self.replicas = replicas;
600+
601+
let unique_ip_peers = Self::coalesce_peers_by_ipaddr(replicas);
602+
self.replicas = unique_ip_peers.into_iter().collect();
579603
}
580604
debug!(
581605
"{:?}: connect_begin: establish StackerDB sessions to {} neighbors",

0 commit comments

Comments
 (0)