diff --git a/index.js b/index.js index 4a6bf79..566dab9 100644 --- a/index.js +++ b/index.js @@ -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 + } } } @@ -176,7 +183,7 @@ module.exports = class Hyperswarm extends EventEmitter { }) this._allConnections.add(conn) - this.stats.connects.attempted++ + this.stats.connects.client.attempted++ this.connecting++ this._clientConnections++ @@ -184,7 +191,7 @@ module.exports = class Hyperswarm extends EventEmitter { conn.on('open', () => { opened = true - this.stats.connects.opened++ + this.stats.connects.client.opened++ this._connectDone() this.connections.add(conn) @@ -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) @@ -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) @@ -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) diff --git a/test/stats.js b/test/stats.js index ed89ca5..c7da246 100644 --- a/test/stats.js +++ b/test/stats.js @@ -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) @@ -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() @@ -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()