Skip to content

Commit

Permalink
fix: ignore new closed connection (#399)
Browse files Browse the repository at this point in the history
* fix: ignore new closed connection

* fix: update src/metrics.ts

Co-authored-by: Cayman <caymannava@gmail.com>
  • Loading branch information
twoeths and wemeetagain authored Jan 19, 2023
1 parent 4842680 commit 20d54f4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
* Registrar notifies an established connection with pubsub protocol
*/
private onPeerConnected(peerId: PeerId, connection: Connection): void {
if (!this.isStarted()) {
this.metrics?.newConnectionCount.inc({ status: connection.stat.status })
// libp2p may emit a closed connection and never issue peer:disconnect event
// see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/398
if (!this.isStarted() || connection.stat.status !== 'OPEN') {
return
}

Expand Down
6 changes: 6 additions & 0 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ export function getMetrics(
help: 'Total count of key collisions on fastmsgid cache put'
}),

newConnectionCount: register.gauge<{ status: string }>({
name: 'gossipsub_new_connection_total',
help: 'Total new connection by status',
labelNames: ['status']
}),

topicStrToLabel: topicStrToLabel,

toTopic(topicStr: TopicStr): TopicLabel {
Expand Down

0 comments on commit 20d54f4

Please sign in to comment.