Skip to content

Commit

Permalink
Added onMeasuredLag event
Browse files Browse the repository at this point in the history
  • Loading branch information
nachooya committed Oct 19, 2017
1 parent 852ed24 commit 50b14f0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 0.6.2
* Added `onMeasuredLag` event. Thanks @nachooya

### 0.6.1
* Added metricsPrev paramater to `onLag` event. Thanks @nachooya

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ toobusy.onLag(function(currentLag, metrics, metricsPrev) {
console.log("Metrics during interval: ", metrics);
console.log("Metrics previous interval: ", metricsPrev);
});

toobusy.onMeasuredLag(function(currentLag, maxLag, interval) {
console.log("Measured Lag during last interval: '+currentLag);
})
```
The default maxLag value is 70ms, and the default check interval is 500ms.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "toobusy-why",
"description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies and metrics support!",
"homepage": "https://github.com/nachooya/node-toobusy",
"version": "0.6.1",
"version": "0.6.2",
"dependencies": {},
"devDependencies": {
"mocha": "1.7.0",
Expand Down
23 changes: 23 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,27 @@ describe('started', function() {
});
});

describe('onMeasuredLag event', function() {
it('should emit onMeasuredLag event every configured interval', function(done) {

var timesCalled = 5;
var meanLag = 0;
toobusy.maxLag(10);
toobusy.interval(100);

toobusy.onMeasuredLag(function(currentLag, maxLag, interval) {
timesCalled--;
(currentLag).should.be.above(0);
(maxLag).should.be.equal(10);
(interval).should.be.equal(100);
meanLag += currentLag;
if (timesCalled === 0) {
console.log ('meanLag: '+(meanLag/5));
toobusy.shutdown();
done();
}
});

});
});

11 changes: 11 additions & 0 deletions toobusy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var events = require('events');
var STANDARD_HIGHWATER = 70;
var STANDARD_INTERVAL = 500;
var LAG_EVENT = "LAG_EVENT";
var MEASURED_LAG_EVENT= "MEASURED_LAG_EVENT";

// A dampening factor. When determining average calls per second or
// current lag, we weigh the current value against the previous value 2:1
Expand Down Expand Up @@ -144,6 +145,14 @@ toobusy.onLag = function (fn, threshold) {
eventEmitter.on(LAG_EVENT, fn);
};

/**
* Registers an event listener for measured lag events,
* @param {Function} fn Function of form onLag(value: number, maxLag: number, interval: number) => void
*/
toobusy.onMeasuredLag = function (fn) {
eventEmitter.on(MEASURED_LAG_EVENT, fn);
};

toobusy.metric = function (name, value) {

if (!metrics[name]) metrics[name] = 0;
Expand All @@ -167,6 +176,8 @@ function start() {
eventEmitter.emit(LAG_EVENT, currentLag, metrics, metricsPrev);
}

eventEmitter.emit(MEASURED_LAG_EVENT, currentLag, lagEventThreshold, interval);

metricsPrev = metrics;
metrics = {};

Expand Down

0 comments on commit 50b14f0

Please sign in to comment.