Skip to content

Commit

Permalink
Fix server opened + no server attempted + split out client and server…
Browse files Browse the repository at this point in the history
… stats
  • Loading branch information
HDegroote committed Jun 28, 2024
1 parent 40abbf2 commit e560af6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ module.exports = class Hyperswarm extends EventEmitter {
this.stats = {
updates: 0,
connects: {
opened: 0,
closed: 0,
attempted: 0
client: {
opened: 0,
closed: 0,
attempted: 0
},
server: {
// Note: there is no notion of 'attempts' for server connections
opened: 0,
closed: 0
}
}
}

Expand Down Expand Up @@ -176,15 +183,15 @@ module.exports = class Hyperswarm extends EventEmitter {
})
this._allConnections.add(conn)

this.stats.connects.attempted++
this.stats.connects.client.attempted++

this.connecting++
this._clientConnections++
let opened = false

conn.on('open', () => {
opened = true
this.stats.connects.opened++
this.stats.connects.client.opened++

this._connectDone()
this.connections.add(conn)
Expand All @@ -205,7 +212,7 @@ module.exports = class Hyperswarm extends EventEmitter {
})
conn.on('close', () => {
if (!opened) this._connectDone()
this.stats.connects.closed++
this.stats.connects.client.closed++

this.connections.delete(conn)
this._allConnections.delete(conn)
Expand Down Expand Up @@ -304,12 +311,8 @@ module.exports = class Hyperswarm extends EventEmitter {
return
}

// The _handleServerConnectionSwam path above calls _handleServerConnection
// again, so this is the moment where the conn is actually considered 'attempted'
this.stats.connects.attempted++
conn.on('open', () => {
this.stats.connects.opened++
})
// When reaching here, the connection will always be 'opened' next tick
this.stats.connects.server.opened++

const peerInfo = this._upsertPeer(conn.remotePublicKey, null)

Expand All @@ -321,7 +324,7 @@ module.exports = class Hyperswarm extends EventEmitter {
this.connections.delete(conn)
this._allConnections.delete(conn)
this._serverConnections--
this.stats.connects.closed++
this.stats.connects.server.closed++

this._maybeDeletePeer(peerInfo)

Expand Down
17 changes: 8 additions & 9 deletions test/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('connectionsOpened and connectionsClosed stats', async (t) => {
const swarm2 = new Hyperswarm({ bootstrap })

const tOpen = t.test('Open connection')
tOpen.plan(4)
tOpen.plan(3)
const tClose = t.test('Close connection')
tClose.plan(4)

Expand All @@ -22,12 +22,12 @@ test('connectionsOpened and connectionsClosed stats', async (t) => {
swarm2.on('connection', (conn) => {
conn.on('error', noop)

tOpen.is(swarm2.stats.connects.opened, 1, 'opened connection is in stats')
tOpen.is(swarm2.stats.connects.attempted, 1, 'attemped connection is in stats')
tClose.is(swarm2.stats.connects.closed, 0, 'sanity check')
tOpen.is(swarm2.stats.connects.client.opened, 1, 'opened connection is in stats')
tOpen.is(swarm2.stats.connects.client.attempted, 1, 'attemped connection is in stats')
tClose.is(swarm2.stats.connects.client.closed, 0, 'sanity check')

conn.on('close', () => {
tClose.is(swarm2.stats.connects.closed, 1, 'closed connection is in stats')
tClose.is(swarm2.stats.connects.client.closed, 1, 'closed connection is in stats')
})

conn.end()
Expand All @@ -37,13 +37,12 @@ test('connectionsOpened and connectionsClosed stats', async (t) => {
conn.on('error', () => noop)

conn.on('open', () => {
tOpen.is(swarm1.stats.connects.opened, 1, 'opened server connection is in stats')
tOpen.is(swarm1.stats.connects.attempted, 1, 'attempted connection is in status')
tClose.is(swarm1.stats.connects.closed, 0, 'Sanity checks')
tOpen.is(swarm1.stats.connects.server.opened, 1, 'opened server connection is in stats')
tClose.is(swarm1.stats.connects.server.closed, 0, 'Sanity check')
})

conn.on('close', () => {
tClose.is(swarm1.stats.connects.closed, 1, 'closed connections is in stats')
tClose.is(swarm1.stats.connects.server.closed, 1, 'closed connections is in stats')
})

conn.end()
Expand Down

0 comments on commit e560af6

Please sign in to comment.