Skip to content

Commit

Permalink
feat: track leaked libp2p connections (#6656)
Browse files Browse the repository at this point in the history
* feat: track leaked libp2p connections

* update var name

---------

Co-authored-by: Nico Flaig <nflaig@protonmail.com>
  • Loading branch information
twoeths and nflaig committed Apr 11, 2024
1 parent d88d671 commit 11aaaf0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/beacon-node/src/network/core/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export function createNetworkCoreMetrics(register: RegistryMetricCreator) {
buckets: [0.001, 0.01, 0.1, 1],
}),
},
leakedConnectionsCount: register.gauge({
name: "lodestar_peer_manager_leaked_connections_count",
help: "Total libp2p leaked connections detected by lodestar",
}),

discovery: {
peersToConnect: register.gauge({
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,12 @@ export class PeerManager {

// Prune connectedPeers map in case it leaks. It has happen in previous nodes,
// disconnect is not always called for all peers
if (this.connectedPeers.size > connectedPeers.length * 2) {
if (this.connectedPeers.size > connectedPeers.length * 1.1) {
const actualConnectedPeerIds = new Set(connectedPeers.map((peerId) => peerId.toString()));
for (const peerIdStr of this.connectedPeers.keys()) {
if (!actualConnectedPeerIds.has(peerIdStr)) {
this.connectedPeers.delete(peerIdStr);
this.metrics?.leakedConnectionsCount.inc();
}
}
}
Expand Down

0 comments on commit 11aaaf0

Please sign in to comment.