Skip to content

Commit b3272cf

Browse files
2colorachingbrain
andauthored
feat: add pending connections count to metrics (#2713)
Allows graphing pending (e.g. pre-upgrade) connections alongside fully open connections. --------- Co-authored-by: Daniel N <[email protected]> Co-authored-by: achingbrain <[email protected]>
1 parent 859f535 commit b3272cf

File tree

1 file changed

+42
-32
lines changed
  • packages/libp2p/src/connection-manager

1 file changed

+42
-32
lines changed

packages/libp2p/src/connection-manager/index.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
162162
private readonly deny: Multiaddr[]
163163
private readonly maxIncomingPendingConnections: number
164164
private incomingPendingConnections: number
165+
private outboundPendingConnections: number
165166
private readonly maxConnections: number
166167

167168
public readonly dialQueue: DialQueue
@@ -200,6 +201,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
200201
this.allow = (init.allow ?? []).map(ma => multiaddr(ma))
201202
this.deny = (init.deny ?? []).map(ma => multiaddr(ma))
202203

204+
this.outboundPendingConnections = 0
203205
this.incomingPendingConnections = 0
204206
this.maxIncomingPendingConnections = init.maxIncomingPendingConnections ?? defaultOptions.maxIncomingPendingConnections
205207

@@ -261,7 +263,9 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
261263
calculate: () => {
262264
const metric = {
263265
inbound: 0,
264-
outbound: 0
266+
'inbound pending': this.incomingPendingConnections,
267+
outbound: 0,
268+
'outbound pending': this.outboundPendingConnections
265269
}
266270

267271
for (const conns of this.connections.values()) {
@@ -462,48 +466,54 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
462466

463467
options.signal?.throwIfAborted()
464468

465-
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
469+
try {
470+
this.outboundPendingConnections++
466471

467-
if (peerId != null && options.force !== true) {
468-
this.log('dial %p', peerId)
469-
const existingConnection = this.getConnections(peerId)
470-
.find(conn => conn.limits == null)
472+
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
471473

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

475-
options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
476-
return existingConnection
479+
if (existingConnection != null) {
480+
this.log('had an existing non-limited connection to %p', peerId)
481+
482+
options.onProgress?.(new CustomProgressEvent('dial-queue:already-connected'))
483+
return existingConnection
484+
}
477485
}
478-
}
479486

480-
const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
481-
...options,
482-
priority: options.priority ?? DEFAULT_DIAL_PRIORITY
483-
})
484-
let peerConnections = this.connections.get(connection.remotePeer)
487+
const connection = await this.dialQueue.dial(peerIdOrMultiaddr, {
488+
...options,
489+
priority: options.priority ?? DEFAULT_DIAL_PRIORITY
490+
})
491+
let peerConnections = this.connections.get(connection.remotePeer)
485492

486-
if (peerConnections == null) {
487-
peerConnections = []
488-
this.connections.set(connection.remotePeer, peerConnections)
489-
}
493+
if (peerConnections == null) {
494+
peerConnections = []
495+
this.connections.set(connection.remotePeer, peerConnections)
496+
}
490497

491-
// we get notified of connections via the Upgrader emitting "connection"
492-
// events, double check we aren't already tracking this connection before
493-
// storing it
494-
let trackedConnection = false
498+
// we get notified of connections via the Upgrader emitting "connection"
499+
// events, double check we aren't already tracking this connection before
500+
// storing it
501+
let trackedConnection = false
495502

496-
for (const conn of peerConnections) {
497-
if (conn.id === connection.id) {
498-
trackedConnection = true
503+
for (const conn of peerConnections) {
504+
if (conn.id === connection.id) {
505+
trackedConnection = true
506+
}
499507
}
500-
}
501508

502-
if (!trackedConnection) {
503-
peerConnections.push(connection)
504-
}
509+
if (!trackedConnection) {
510+
peerConnections.push(connection)
511+
}
505512

506-
return connection
513+
return connection
514+
} finally {
515+
this.outboundPendingConnections--
516+
}
507517
}
508518

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

0 commit comments

Comments
 (0)