Skip to content

Commit

Permalink
Count expected number of arrivals and stop the timer when reached
Browse files Browse the repository at this point in the history
Stop the timer when the expected number of arrivals is reached rather
than with another timeout.

This is to avoid triggering a Node.js bug that leads to using
100% CPU.

Ref: nodejs/node#9756
  • Loading branch information
hassy committed Dec 8, 2016
1 parent 113b1b4 commit 843fb26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
21 changes: 15 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Nanotimer = require('nanotimer');
var debug = require('debug')('arrivals');

module.exports = {
uniform: {
Expand All @@ -23,7 +24,9 @@ function createPoisson(mean, duration) {
}

function UniformProcess(tickInterval, duration) {
this._tickInterval = tickInterval;
debug(`tickInterval = ${tickInterval}, duration = ${duration}`);
this._tickInterval = Math.floor(tickInterval);
debug(`this._tickInterval set to ${this._tickInterval}`);
this._interval = null;
this._duration = duration || null; // ms
this._timer = new Nanotimer();
Expand All @@ -34,14 +37,20 @@ util.inherits(UniformProcess, EventEmitter);

UniformProcess.prototype.start = function() {
var self = this;
var arrivals = 0;
var maxArrivals = Infinity;
if (self._duration) {
maxArrivals = Math.floor(self._duration / self._tickInterval);
}
debug(`maxArrivals = ${maxArrivals}`);
self._interval = self._timer.setInterval(function() {
self.emit('arrival');
}, '', self._tickInterval + 'm');
if (self._duration) {
self._timer.setTimeout(function() {
arrivals++;
if (arrivals === maxArrivals) {
debug(`maxArrivals reached, stopping`);
self.stop();
}, '', (self._duration + self._tickInterval / 2) + 'm');
}
}
}, '', self._tickInterval + 'm');
return self;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "Hassy Veldstra <h@artillery.io>",
"license": "ISC",
"dependencies": {
"debug": "2.3.3",
"nanotimer": "0.3.14"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tape('Can create a uniform process', function(t) {
t.ok(true, 'Process started');
setTimeout(function() {
p.stop();
t.assert(count === 6, 'Correct number of arrivals');
t.assert(count === 6, `Correct number of arrivals - got ${count} - 1`);
t.end();
}, 2700);
});
Expand All @@ -66,7 +66,7 @@ tape('Can create a uniform process with fixed duration', function(t) {
});
p.once('finished', function() {
t.ok(true, ' got finished event');
t.assert(count === 7, 'Correct number of arrivals');
t.assert(count === 6, `Correct number of arrivals - got ${count} - 2`);
t.end();
});

Expand All @@ -84,7 +84,8 @@ tape('Expect number of arrival events get emitted', function(t) {
count++;
});
p.once('finished', function () {
t.assert(count === arrivalCount, 'Got the expect number of arrivals');
console.log(`count = ${count}, arrivalCount = ${arrivalCount}`);
t.assert(count === arrivalCount, 'Got expected number of arrivals');
t.end();
});
p.start();
Expand Down

0 comments on commit 843fb26

Please sign in to comment.