Skip to content

Commit

Permalink
fix memory leak when stopping/starting several times.
Browse files Browse the repository at this point in the history
  • Loading branch information
bevacqua committed Jun 1, 2016
1 parent cfc3b46 commit 1a1f679
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internals.Monitor.prototype.start = function (callback) {

var pmonitor = ProcessMonitor;
var os = System;
var network = new Network.Monitor(self._server);
var network = self.network = new Network.Monitor(self._server);

var asyncOps = {
osload: os.loadavg,
Expand Down Expand Up @@ -193,6 +193,11 @@ internals.Monitor.prototype.stop = function () {
this.removeListener('ops', state.opsHandler);
Wreck.removeListener('response', state.wreckHandler);

if (this.network) {
this.network.stop();
this.network = null;
}

internals.iterateOverEventHash(state.handlers, function (event, handler) {

self._server.removeListener(event, handler);
Expand Down
16 changes: 13 additions & 3 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ var Items = require('items');

var internals = {};

function noop () {
}

module.exports.Monitor = internals.NetworkMonitor = function (server) {

this._requests = {};
this._responseTimes = {};
this._server = server;

this._server.on('request-internal', this._onRequest.bind(this));
this._server.on('response', this._onResponse.bind(this));
};
var onReq = this._onRequest.bind(this);
var onRes = this._onResponse.bind(this);

this._server.on('request-internal', onReq);
this._server.on('response', onRes);

this.stop = function () {
this._server.removeListener('request-internal', onReq);
this._server.removeListener('response', onRes);
this.stop = noop;
};
};

internals.NetworkMonitor.prototype._onRequest = function (request, event, tags) {

Expand Down

0 comments on commit 1a1f679

Please sign in to comment.