Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pending connections count to metrics #2713

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 42 additions & 32 deletions packages/libp2p/src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
private readonly deny: Multiaddr[]
private readonly maxIncomingPendingConnections: number
private incomingPendingConnections: number
private outboundPendingConnections: number
private readonly maxConnections: number

public readonly dialQueue: DialQueue
Expand Down Expand Up @@ -200,6 +201,7 @@
this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
this.deny = (init.deny ?? []).map(ma => multiaddr(ma))

this.outboundPendingConnections = 0
this.incomingPendingConnections = 0
this.maxIncomingPendingConnections = init.maxIncomingPendingConnections ?? defaultOptions.maxIncomingPendingConnections

Expand Down Expand Up @@ -261,7 +263,9 @@
calculate: () => {
const metric = {
inbound: 0,
outbound: 0
'inbound pending': this.incomingPendingConnections,
outbound: 0,
'outbound pending': this.outboundPendingConnections

Check warning on line 268 in packages/libp2p/src/connection-manager/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/connection-manager/index.ts#L266-L268

Added lines #L266 - L268 were not covered by tests
}

for (const conns of this.connections.values()) {
Expand Down Expand Up @@ -462,48 +466,54 @@

options.signal?.throwIfAborted()

const { peerId } = getPeerAddress(peerIdOrMultiaddr)
try {
this.outboundPendingConnections++

if (peerId != null && options.force !== true) {
this.log('dial %p', peerId)
const existingConnection = this.getConnections(peerId)
.find(conn => conn.limits == null)
const { peerId } = getPeerAddress(peerIdOrMultiaddr)

if (existingConnection != null) {
this.log('had an existing non-limited connection to %p', peerId)
if (peerId != null && options.force !== true) {
this.log('dial %p', peerId)
const existingConnection = this.getConnections(peerId)
.find(conn => conn.limits == null)

options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
return existingConnection
if (existingConnection != null) {
this.log('had an existing non-limited connection to %p', peerId)

options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
return existingConnection
}
}
}

const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
...options,
priority: options.priority ?? DEFAULT_DIAL_PRIORITY
})
let peerConnections = this.connections.get(connection.remotePeer)
const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
...options,
priority: options.priority ?? DEFAULT_DIAL_PRIORITY
})
let peerConnections = this.connections.get(connection.remotePeer)

if (peerConnections == null) {
peerConnections = []
this.connections.set(connection.remotePeer, peerConnections)
}
if (peerConnections == null) {
peerConnections = []
this.connections.set(connection.remotePeer, peerConnections)
}

// we get notified of connections via the Upgrader emitting "connection"
// events, double check we aren't already tracking this connection before
// storing it
let trackedConnection = false
// we get notified of connections via the Upgrader emitting "connection"
// events, double check we aren't already tracking this connection before
// storing it
let trackedConnection = false

for (const conn of peerConnections) {
if (conn.id === connection.id) {
trackedConnection = true
for (const conn of peerConnections) {
if (conn.id === connection.id) {
trackedConnection = true
}
}
}

if (!trackedConnection) {
peerConnections.push(connection)
}
if (!trackedConnection) {
peerConnections.push(connection)
}

return connection
return connection
} finally {
this.outboundPendingConnections--
}
}

async closeConnections (peerId: PeerId, options: AbortOptions = {}): Promise<void> {
Expand Down
Loading