Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
feat(connection-spy): add class for monitoring active connections
Browse files Browse the repository at this point in the history
This gives us more control over the process of monitoring active
and removed connections in a given topology. This also allows for
an event-driven approach to cleanup, whereas in the past we have
depended upon timeouts.

NODE-1132
  • Loading branch information
mbroadst committed Sep 16, 2017
1 parent ab3b70b commit 6dd6db3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/tests/functional/shared.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const EventEmitter = require('events');

function executeCommand(configuration, db, cmd, options, cb) {
var Pool = require('../../../lib/connection/pool'),
Expand Down Expand Up @@ -104,6 +105,38 @@ const delay = function(timeout) {
});
};

class ConnectionSpy extends EventEmitter {
constructor() {
super();
this.connections = {};
}

addConnection(id, connection) {
// console.log(`=== added connection ${id} :: ${connection.port}`);

this.connections[id] = connection;
this.emit('connectionAdded');
}

deleteConnection(id) {
// console.log(
// `=== deleted connection ${id} :: ${this.connections[id] ? this.connections[id].port : ''}`
// );

delete this.connections[id];
this.emit('connectionRemoved');

if (this.connectionCount() === 0) {
this.emit('drained');
}
}

connectionCount() {
return Object.keys(this.connections).length;
}
}

module.exports.executeCommand = executeCommand;
module.exports.locateAuthMethod = locateAuthMethod;
module.exports.delay = delay;
module.exports.ConnectionSpy = ConnectionSpy;

0 comments on commit 6dd6db3

Please sign in to comment.