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 1 commit
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
15 changes: 14 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,
connections: {
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.connections.attempted++

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

conn.on('open', () => {
opened = true
this.stats.connections.opened++
Copy link
Contributor

Choose a reason for hiding this comment

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

should also track server connections ya?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: could also track client- and server connections separately, but I put them together for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now it's tracking them separately (cleaner that way)


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.connections.closed++

this.connections.delete(conn)
this._allConnections.delete(conn)
this._clientConnections--
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
44 changes: 44 additions & 0 deletions test/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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(2)
const tClose = t.test('Close connection')
tClose.plan(2)

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

swarm2.on('connection', (conn) => {
conn.on('error', noop)
tOpen.is(swarm2.stats.connections.opened, 1, 'opened connection is in stats')
tOpen.is(swarm2.stats.connections.attempted, 1, 'attemped connection is in stats')
tClose.is(swarm2.stats.connections.closed, 0, 'sanity check')

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

conn.destroy()
})

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

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

function noop () {}
Loading