Skip to content

Commit 20d54f4

Browse files
twoethswemeetagain
andauthored
fix: ignore new closed connection (#399)
* fix: ignore new closed connection * fix: update src/metrics.ts Co-authored-by: Cayman <caymannava@gmail.com>
1 parent 4842680 commit 20d54f4

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
698698
* Registrar notifies an established connection with pubsub protocol
699699
*/
700700
private onPeerConnected(peerId: PeerId, connection: Connection): void {
701-
if (!this.isStarted()) {
701+
this.metrics?.newConnectionCount.inc({ status: connection.stat.status })
702+
// libp2p may emit a closed connection and never issue peer:disconnect event
703+
// see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/398
704+
if (!this.isStarted() || connection.stat.status !== 'OPEN') {
702705
return
703706
}
704707

src/metrics.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ export function getMetrics(
512512
help: 'Total count of key collisions on fastmsgid cache put'
513513
}),
514514

515+
newConnectionCount: register.gauge<{ status: string }>({
516+
name: 'gossipsub_new_connection_total',
517+
help: 'Total new connection by status',
518+
labelNames: ['status']
519+
}),
520+
515521
topicStrToLabel: topicStrToLabel,
516522

517523
toTopic(topicStr: TopicStr): TopicLabel {

0 commit comments

Comments
 (0)