Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connections to stats (opened, closed, attempted) #174

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -291,6 +311,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 @@ -301,6 +324,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 () {}
Loading