node: Improve `maintain_connections` function
Ensure we don't try to connect to two addresses of the same node.
This commit is contained in:
parent
dc7d67c1e2
commit
27865a26b2
|
|
@ -1423,20 +1423,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a list of peers available to connect to.
|
/// Get a list of peers available to connect to.
|
||||||
fn available_peers(&mut self) -> Vec<(NodeId, KnownAddress)> {
|
fn available_peers(&mut self) -> HashMap<NodeId, Vec<KnownAddress>> {
|
||||||
let outbound = self
|
|
||||||
.sessions
|
|
||||||
.values()
|
|
||||||
.filter(|s| s.link.is_outbound())
|
|
||||||
.filter(|s| s.is_connected() || s.is_connecting())
|
|
||||||
.count();
|
|
||||||
|
|
||||||
let wanted = TARGET_OUTBOUND_PEERS.saturating_sub(outbound);
|
|
||||||
// Don't connect to more peers than needed.
|
|
||||||
if wanted == 0 {
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.addresses.entries() {
|
match self.addresses.entries() {
|
||||||
Ok(entries) => {
|
Ok(entries) => {
|
||||||
// Nb. we don't want to connect to any peers that already have a session with us,
|
// Nb. we don't want to connect to any peers that already have a session with us,
|
||||||
|
|
@ -1444,12 +1431,14 @@ where
|
||||||
entries
|
entries
|
||||||
.filter(|(nid, _)| !self.sessions.contains_key(nid))
|
.filter(|(nid, _)| !self.sessions.contains_key(nid))
|
||||||
.filter(|(nid, _)| nid != &self.node_id())
|
.filter(|(nid, _)| nid != &self.node_id())
|
||||||
.take(wanted)
|
.fold(HashMap::new(), |mut acc, (nid, addr)| {
|
||||||
.collect()
|
acc.entry(nid).or_insert_with(Vec::new).push(addr);
|
||||||
|
acc
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(target: "service", "Unable to lookup available peers in address book: {e}");
|
error!(target: "service", "Unable to lookup available peers in address book: {e}");
|
||||||
Vec::new()
|
HashMap::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1492,7 +1481,21 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maintain_connections(&mut self) {
|
fn maintain_connections(&mut self) {
|
||||||
|
trace!(target: "service", "Maintaining connections..");
|
||||||
|
|
||||||
let now = self.clock;
|
let now = self.clock;
|
||||||
|
let outbound = self
|
||||||
|
.sessions
|
||||||
|
.values()
|
||||||
|
.filter(|s| s.link.is_outbound())
|
||||||
|
.filter(|s| s.is_connected() || s.is_connecting())
|
||||||
|
.count();
|
||||||
|
let wanted = TARGET_OUTBOUND_PEERS.saturating_sub(outbound);
|
||||||
|
|
||||||
|
// Don't connect to more peers than needed.
|
||||||
|
if wanted == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Nb. We use the `MAX_RECONNECTION_DELTA` to know when it's ok to reconnect, because
|
// Nb. We use the `MAX_RECONNECTION_DELTA` to know when it's ok to reconnect, because
|
||||||
// these aren't persistent peers. They could go offline for a long time and we don't want to
|
// these aren't persistent peers. They could go offline for a long time and we don't want to
|
||||||
|
|
@ -1500,7 +1503,9 @@ where
|
||||||
for (id, ka) in self
|
for (id, ka) in self
|
||||||
.available_peers()
|
.available_peers()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.flat_map(|(nid, kas)| kas.into_iter().map(move |ka| (nid, ka)))
|
||||||
.filter(|(_, ka)| now - ka.last_attempt.unwrap_or_default() >= MAX_RECONNECTION_DELTA)
|
.filter(|(_, ka)| now - ka.last_attempt.unwrap_or_default() >= MAX_RECONNECTION_DELTA)
|
||||||
|
.take(wanted)
|
||||||
{
|
{
|
||||||
self.connect(id, ka.addr.clone());
|
self.connect(id, ka.addr.clone());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue