Skip to content

Commit

Permalink
Add connections to stats (opened, closed, attempted) (#174)
Browse files Browse the repository at this point in the history
* Add connections to stats (opened, closed, attempted)

* Rename connections->connects

* Also keep track of server connections

* Fix server opened + no server attempted + split out client and server stats
  • Loading branch information
HDegroote authored Jun 28, 2024
1 parent 88362e5 commit 53b704c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,21 @@ module.exports = class Hyperswarm extends EventEmitter {
this.peers = new Map()
this.explicitPeers = new Set()
this.listening = null
this.stats = { updates: 0 }
this.stats = {
updates: 0,
connects: {
client: {
opened: 0,
closed: 0,
attempted: 0
},
server: {
// Note: there is no notion of 'attempts' for server connections
opened: 0,
closed: 0
}
}
}

this._discovery = new Map()
this._timer = new RetryTimer(this._requeue.bind(this), {
Expand Down Expand Up @@ -169,12 +183,16 @@ module.exports = class Hyperswarm extends EventEmitter {
})
this._allConnections.add(conn)

this.stats.connects.client.attempted++

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

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

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

this.connections.delete(conn)
this._allConnections.delete(conn)
this._clientConnections--
Expand Down Expand Up @@ -295,6 +315,9 @@ module.exports = class Hyperswarm extends EventEmitter {
return
}

// When reaching here, the connection will always be 'opened' next tick
this.stats.connects.server.opened++

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

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

this._maybeDeletePeer(peerInfo)

Expand Down
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function runTests () {
await import('./peer-join.js')
await import('./retry-timer.js')
await import('./suspend.js')
await import('./stats.js')
await import('./swarm.js')
await import('./update.js')

Expand Down
58 changes: 58 additions & 0 deletions test/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const test = require('brittle')
const createTestnet = require('hyperdht/testnet')

const Hyperswarm = require('..')

test('connectionsOpened and connectionsClosed stats', async (t) => {
const { bootstrap } = await createTestnet(3, t.teardown)

const swarm1 = new Hyperswarm({ bootstrap })
const swarm2 = new Hyperswarm({ bootstrap })

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

t.teardown(async () => {
await swarm1.destroy()
await swarm2.destroy()
})

swarm2.on('connection', (conn) => {
conn.on('error', noop)

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.client.closed, 1, 'closed connection is in stats')
})

conn.end()
})

swarm1.on('connection', (conn) => {
conn.on('error', () => noop)

conn.on('open', () => {
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.server.closed, 1, 'closed connections is in stats')
})

conn.end()
})

const topic = Buffer.alloc(32).fill('hello world')
await swarm1.join(topic, { server: true, client: false }).flushed()
swarm2.join(topic, { client: true, server: false })

await tClose
})

function noop () {}

0 comments on commit 53b704c

Please sign in to comment.