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 3 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
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ 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: {
opened: 0,
closed: 0,
attempted: 0
mafintosh marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

this.stats.connects.attempted++

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

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

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

this.connections.delete(conn)
this._allConnections.delete(conn)
this._clientConnections--
Expand Down Expand Up @@ -291,6 +304,13 @@ 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', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check: is the conn guaranteed to not yet be opened? (else I need to special-case that)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its always opened at this state so this event shouldnt be used, you use just increment it sync (its a server conn)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I removed the 'attempted' counter, and now always increment 'opened' here

this.stats.connects.opened++
})

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

this.connections.add(conn)
Expand All @@ -301,6 +321,7 @@ module.exports = class Hyperswarm extends EventEmitter {
this.connections.delete(conn)
this._allConnections.delete(conn)
this._serverConnections--
this.stats.connects.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
59 changes: 59 additions & 0 deletions test/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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(4)
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.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')

conn.on('close', () => {
tClose.is(swarm2.stats.connects.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.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')
})

conn.on('close', () => {
tClose.is(swarm1.stats.connects.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