Skip to content

Commit

Permalink
Convert to JS.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Reed committed May 29, 2013
1 parent f0608e8 commit 5224d8b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 124 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/node_modules
*~
/examples/node_modules
/build
npm-debug.log
npm-debug.log
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.3.0 -
* full JS implementation, removed c code & bindings
* example bugfixes

0.2.3 -
* node.js 0.10.0 support - uprade to latest bindings and include in .travis.yml

Expand Down
12 changes: 0 additions & 12 deletions binding.gyp

This file was deleted.

2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"description": "dependencies required by the toobusy examples",
"private": true,
"dependencies": {
"express": "2"
"express": "3"
}
}
4 changes: 2 additions & 2 deletions examples/standalone.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// first, we want to be able to get cpu usage stats in terms of percentage
var loaded = false;
var toobusy = require('../');
var toobusy = require('..');

var work = 524288;

function worky() {
var howBusy = toobusy();
if (howBusy) {
work /= 4;
console.log("I can't work! I'm too busy:", howBusy + "ms behind");
console.log("I can't work! I'm too busy:", toobusy.lag() + "ms behind");
}
work *= 2;
for (var i = 0; i < work;) i++;
Expand Down
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
var bindings = require('bindings')('toobusy.node')
module.exports = bindings.toobusy;
module.exports.shutdown = bindings.shutdown;
module.exports.maxLag = bindings.maxLag;
module.exports.lag = bindings.lag;
module.exports = require('./toobusy');
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "toobusy",
"description": "Don't fall over when your Node.JS server is too busy.",
"homepage": "https://github.com/lloyd/node-toobusy",
"version": "0.2.2",
"version": "0.3.0",
"dependencies": {
"bindings": "1.1.0"
},
"devDependencies": {
"should": "1.2.1",
Expand Down Expand Up @@ -32,7 +31,7 @@
],
"main": "./index.js",
"engines": {
"node": ">=0.8.0"
"node": ">=0.9.1"
},
"scripts": {
"test": "mocha tests"
Expand Down
99 changes: 0 additions & 99 deletions toobusy.cc

This file was deleted.

54 changes: 54 additions & 0 deletions toobusy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var STANDARD_HIGHWATER = 70;
var STANDARD_INTERVAL = 500;

// A dampening factor. When determining average calls per second or
// current lag, we weigh the current value against the previous value 2:1
// to smooth spikes.
var AVG_DECAY_FACTOR = 3;

var lastTime = new Date().valueOf(), now, lag, highWater = STANDARD_HIGHWATER, interval = STANDARD_INTERVAL, currentLag = 0;

var checkInterval = setInterval(function(){
now = new Date().valueOf();
lag = now - lastTime;
lag = (lag < interval) ? 0 : lag - interval;
currentLag = (lag + (currentLag * (AVG_DECAY_FACTOR - 1))) / AVG_DECAY_FACTOR;
lastTime = now;
}, interval);

// Don't keep process open just for this timer.
checkInterval.unref();

var toobusy = function(){
// If current lag is < 2x the highwater mark, we don't always call it 'too busy'. E.g. with a 50ms lag
// and a 40ms highWater (1.25x highWater), 25% of the time we will block. With 80ms lag and a 40ms highWater,
// we will always block.
var pctToBlock = (currentLag - highWater) / highWater;
var rand = Math.random();
return rand < pctToBlock;
};

toobusy.lag = function(){
return parseInt(currentLag, 10);
};

toobusy.maxLag = function(newLag){
if(!newLag) return highWater;

// If an arg was passed, try to set highWater.
if(Object.prototype.toString.call(newLag) !== "[object Number]"){
throw "Expected numeric first argument.";
}
newLag = parseInt(newLag, 10);
if(newLag < 10){
throw "Maximum lag should be greater than 10ms.";
}
highWater = newLag;
return highWater;
};

toobusy.shutdown = function(){
clearInterval(checkInterval);
};

module.exports = toobusy;

0 comments on commit 5224d8b

Please sign in to comment.