From e12f5002260dd7a9357b9538145b2b6abf631ce4 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 22 Jun 2017 19:45:40 -0300 Subject: [PATCH 01/20] Adding smoothingFactor to README.md --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0d4afa1..7300b33 100644 --- a/README.md +++ b/README.md @@ -73,9 +73,10 @@ process.on('SIGINT', function() { The library exposes a few knobs: -`maxLag` - This number represents the maximum amount of time in milliseconds that the event queue is behind, -before we consider the process *too busy*. -`interval` - The check interval for measuring event loop lag, in ms. +**maxLag** - This number represents the maximum amount of time in milliseconds that the event queue is behind, +before we consider the process *too busy*. +**interval** - The check interval for measuring event loop lag, in ms. +**smoothingFactor** - The smoothing factor used to calculate currentLag, with the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing): `currentLag = smoothingFactor * lag + (1 - smoothingFactor) * currentLag;` ```javascript var toobusy = require('toobusy-js'); @@ -87,6 +88,10 @@ toobusy.maxLag(10); // but may cause the check to be too sensitive. toobusy.interval(250); +// Set smoothing factor to a lower value. This will make it less sensible +// to spikes. Default is 1/3. +toobusy.smoothingFactor(1/4); + // Get current maxLag or interval setting by calling without parameters. var currentMaxLag = toobusy.maxLag(), interval = toobusy.interval(); From d6bce3d4c4614b1fb1841fd3df487e8a90ac8785 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Wed, 12 Jul 2017 16:01:06 -0300 Subject: [PATCH 02/20] Fix license link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7300b33..3caf110 100644 --- a/README.md +++ b/README.md @@ -127,4 +127,4 @@ this concept is not new. Here are references to others who apply the same techn ## license -[WTFPL](http://wtfpl.org) +[WTFPL](http://wtfpl.net) From d19797d3111efdd8347895920fbd8bd2707fe2b2 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Wed, 12 Jul 2017 18:06:49 -0300 Subject: [PATCH 03/20] Isolate logic of currentLag in an overwritable function --- toobusy.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/toobusy.js b/toobusy.js index 1f6f7ac..5b1ae87 100644 --- a/toobusy.js +++ b/toobusy.js @@ -142,6 +142,19 @@ toobusy.onLag = function (fn, threshold) { eventEmitter.on(LAG_EVENT, fn); }; +/** + * Calculates the new value that will be assigned to currentLag + * Overwrite this function if you need a different behavior + * @param {number} lag The last measured lag + * @param {number} cLag The currentLag value + * @param {number} sFactor The smoothingFactor value + * @return {number} The calculated value + */ +toobusy.lagFunction = function (lag, cLag, sFactor) { + // Dampen lag. See SMOOTHING_FACTOR initialization at the top of this file. + return sFactor * lag + (1 - sFactor) * cLag; +} + /** * Private - starts checking lag. */ @@ -150,8 +163,7 @@ function start() { var now = Date.now(); var lag = now - lastTime; lag = Math.max(0, lag - interval); - // Dampen lag. See SMOOTHING_FACTOR initialization at the top of this file. - currentLag = smoothingFactor * lag + (1 - smoothingFactor) * currentLag; + currentLag = toobusy.lagFunction(lag, currentLag, smoothingFactor); lastTime = now; if (lagEventThreshold !== -1 && currentLag > lagEventThreshold) { From c8956cf1a2a47bca6319dafc4536129c115a0d68 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Wed, 12 Jul 2017 19:02:17 -0300 Subject: [PATCH 04/20] New smoothing factor to be used when lag is falling --- tests.js | 64 ++++++++++++++++++++++++++++++++++++++---------------- toobusy.js | 64 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/tests.js b/tests.js index 19548eb..8f0c366 100644 --- a/tests.js +++ b/tests.js @@ -91,6 +91,9 @@ describe('toobusy()', function() { }); describe('lag events', function () { + //Sometimes the default 2s timeout is hit on this suite, raise to 10s. + this.timeout(10 * 1000); + it('should not emit lag events if the lag is less than the configured threshold', testLagEvent(100, 50, false)); it('should emit lag events if the lag is greater than the configured threshold', @@ -140,6 +143,7 @@ describe('smoothingFactor', function() { this.timeout(10 * 1000); beforeEach(function() { + toobusy.reset(); toobusy.maxLag(10); toobusy.interval(250); }); @@ -147,26 +151,23 @@ describe('smoothingFactor', function() { toobusy.maxLag(70); toobusy.interval(500); }); - it('should default to 1/3', function(done) { - (toobusy.smoothingFactor()).should.equal(1/3); - done(); - }); - it('should throw an exception for invalid values', function(done) { - (function() { toobusy.smoothingFactor(0); }).should.throw; - (function() { toobusy.smoothingFactor(2); }).should.throw; - (function() { toobusy.smoothingFactor(-1); }).should.throw; - (function() { toobusy.smoothingFactor(1); }).should.not.throw; - done(); + + setupSmoothingFactorTests({ + function: toobusy.smoothingFactorOnRise, + suiteName: 'on rise', + default: 1/3 }); - it('should be configurable', function(done) { - (toobusy.smoothingFactor(0.9)).should.equal(0.9); - (toobusy.smoothingFactor(0.1)).should.equal(0.1); - (toobusy.smoothingFactor()).should.equal(0.1); - done(); + + setupSmoothingFactorTests({ + function: toobusy.smoothingFactorOnFall, + suiteName: 'on fall', + default: 1 - 1/3 }); - it('should allow no dampening', function(done) { + + it('should allow no dampening', function (done) { var cycles_to_toobusy = 0; - toobusy.smoothingFactor(1); // no dampening + toobusy.smoothingFactorOnRise(1); // no dampening + toobusy.smoothingFactorOnFall(1); // no dampening function load() { if (toobusy()) { @@ -180,9 +181,9 @@ describe('smoothingFactor', function() { load(); }); - it('should respect larger dampening factors', function(done) { + it('should respect larger dampening factors', function (done) { var cycles_to_toobusy = 0; - toobusy.smoothingFactor(0.05); + toobusy.smoothingFactorOnRise(0.05); function load() { if (toobusy()) { @@ -196,8 +197,33 @@ describe('smoothingFactor', function() { load(); }); + }); +function setupSmoothingFactorTests(options) { + var smoothingFunc = options.function; + + describe(options.suiteName, function() { + it('should default to ' + options.default, function(done) { + (smoothingFunc()).should.equal(options.default); + done(); + }); + it('should throw an exception for invalid values', function(done) { + (function() { smoothingFunc(0); }).should.throw; + (function() { smoothingFunc(2); }).should.throw; + (function() { smoothingFunc(-1); }).should.throw; + (function() { smoothingFunc(1); }).should.not.throw; + done(); + }); + it('should be configurable', function(done) { + (smoothingFunc(0.9)).should.equal(0.9); + (smoothingFunc(0.1)).should.equal(0.1); + (smoothingFunc()).should.equal(0.1); + done(); + }); + }); +}; + describe('started', function() { it('should return false after shutdown', function(done) { toobusy.shutdown(); diff --git a/toobusy.js b/toobusy.js index 5b1ae87..ffff48c 100644 --- a/toobusy.js +++ b/toobusy.js @@ -22,7 +22,8 @@ var SMOOTHING_FACTOR = 1/3; var lastTime = Date.now(); var highWater = STANDARD_HIGHWATER; var interval = STANDARD_INTERVAL; -var smoothingFactor = SMOOTHING_FACTOR; +var smoothingFactorOnRise = SMOOTHING_FACTOR; +var smoothingFactorOnFall = 1 - SMOOTHING_FACTOR; var currentLag = 0; var checkInterval; var lagEventThreshold = -1; @@ -40,6 +41,17 @@ var toobusy = function(){ return Math.random() < pctToBlock; }; +/** + * Resets variables to their default values + * Affected variables: highWater, interval, smoothingFactorOnRise and smoothingFactorOnFall + */ +toobusy.reset = function() { + highWater = STANDARD_HIGHWATER; + interval = STANDARD_INTERVAL; + smoothingFactorOnRise = SMOOTHING_FACTOR; + smoothingFactorOnFall = 1 - SMOOTHING_FACTOR; +}; + /** * Sets or gets the current check interval. * If you want more sensitive checking, set a faster (lower) interval. A lower maxLag can also create a more @@ -92,7 +104,24 @@ toobusy.maxLag = function(newLag){ }; /** - * Set or get the smoothing factor. Default is 0.3333.... + * Private function used to set the two smoothing factors + * @param {number} newFactor + * @param {boolean} onFallSelector Optional parameter used to selected which factor to set + */ +function setSmoothingFactor(newFactor, onFallSelector){ + if (typeof newFactor !== "number") throw new Error("NewFactor must be a number."); + if(newFactor <= 0 || newFactor > 1) throw new Error("Smoothing factor should be in range ]0,1]."); + + if (onFallSelector) { + smoothingFactorOnFall = newFactor; + } else { + smoothingFactorOnRise = newFactor; + } +}; + +/** + * Set or get the smoothing factor on rise. Default is 0.3333.... + * This is the factor applied when the measured lag is higher than currentLag * * The smoothing factor per the standard exponential smoothing formula "αtn + (1-α)tn-1" * See: https://en.wikipedia.org/wiki/Exponential_smoothing @@ -100,14 +129,23 @@ toobusy.maxLag = function(newLag){ * @param {Number} [newFactor] New smoothing factor. * @return {Number} New or existing smoothing factor. */ -toobusy.smoothingFactor = function(newFactor){ - if(!newFactor) return smoothingFactor; - - if (typeof newFactor !== "number") throw new Error("NewFactor must be a number."); - if(newFactor <= 0 || newFactor > 1) throw new Error("Smoothing factor should be in range ]0,1]."); +toobusy.smoothingFactorOnRise = function(newFactor) { + if(!newFactor) return smoothingFactorOnRise; + setSmoothingFactor(newFactor); + return smoothingFactorOnRise; +}; - smoothingFactor = newFactor; - return smoothingFactor; +/** + * Set or get the smoothing factor on fall. Default is 0.6666.... + * This is the factor applied when the measured lag is lower than currentLag + * + * @param {Number} [newFactor] New smoothing factor. + * @return {Number} New or existing smoothing factor. + */ +toobusy.smoothingFactorOnFall = function(newFactor) { + if(!newFactor) return smoothingFactorOnFall; + setSmoothingFactor(newFactor, true); + return smoothingFactorOnFall; }; /** @@ -150,9 +188,9 @@ toobusy.onLag = function (fn, threshold) { * @param {number} sFactor The smoothingFactor value * @return {number} The calculated value */ -toobusy.lagFunction = function (lag, cLag, sFactor) { - // Dampen lag. See SMOOTHING_FACTOR initialization at the top of this file. - return sFactor * lag + (1 - sFactor) * cLag; +toobusy.lagFunction = function (lag, cLag, sFactorRise, sFactorFall) { + var factor = lag > cLag ? sFactorRise : sFactorFall; + return factor * lag + (1 - factor) * cLag; } /** @@ -163,7 +201,7 @@ function start() { var now = Date.now(); var lag = now - lastTime; lag = Math.max(0, lag - interval); - currentLag = toobusy.lagFunction(lag, currentLag, smoothingFactor); + currentLag = toobusy.lagFunction(lag, currentLag, smoothingFactorOnRise, smoothingFactorOnFall); lastTime = now; if (lagEventThreshold !== -1 && currentLag > lagEventThreshold) { From e115277546b6e70212c5a3634e2c3c33c347317c Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 11:31:21 -0300 Subject: [PATCH 05/20] Updating README.md --- README.md | 29 ++++++++++++++++++----------- package.json | 9 +++++---- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3caf110..404726f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ and continue serving as many requests as possible. ## installation ``` -npm install toobusy-js +npm install node-toobusy ``` @@ -60,13 +60,6 @@ app.get('/', function(req, res) { }); var server = app.listen(3000); - -process.on('SIGINT', function() { - server.close(); - // calling .shutdown allows your process to exit normally - toobusy.shutdown(); - process.exit(); -}); ``` ## tunable parameters @@ -76,7 +69,10 @@ The library exposes a few knobs: **maxLag** - This number represents the maximum amount of time in milliseconds that the event queue is behind, before we consider the process *too busy*. **interval** - The check interval for measuring event loop lag, in ms. -**smoothingFactor** - The smoothing factor used to calculate currentLag, with the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing): `currentLag = smoothingFactor * lag + (1 - smoothingFactor) * currentLag;` +**smoothingFactor** - When a new lag is measured, we smooth its value using the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing). +There are two factors available, the smoothingFactorOnRise, which is used when the new lag is higher than currentLag, and the smoothingFactorOnFall, which is used when the new lag is lower than currentLag. +It's a good idea to keep the factor on fall higher than on rise, to make the currentLag recover faster after spikes. +**lagFunction** - This is the function used to calculate currentLag. You can overwrite it if you need a different behavior. The parameters passed to it are: `lag`, `currentLag`, `smoothingFactorOnRise` and `smoothingFactorOnFall`. ```javascript var toobusy = require('toobusy-js'); @@ -88,9 +84,20 @@ toobusy.maxLag(10); // but may cause the check to be too sensitive. toobusy.interval(250); -// Set smoothing factor to a lower value. This will make it less sensible +// Set smoothing factor on rise to a lower value. This will make it less sensible // to spikes. Default is 1/3. -toobusy.smoothingFactor(1/4); +toobusy.smoothingFactorOnRise(1/4); + +// Set smoothing factor on fall to a higher value. This will make it recover faster +// after spikes. Default is 2/3. +toobusy.smoothingFactorOnFall(3/4); + +// You can overwrite this function to change the way currentLag is calculated. +// This is the default implementation. +toobusy.lagFunction = function(lag, cLag, , sFactorRise, sFactorFall) { + var factor = lag > cLag ? sFactorRise : sFactorFall; + return factor * lag + (1 - factor) * cLag; +} // Get current maxLag or interval setting by calling without parameters. var currentMaxLag = toobusy.maxLag(), interval = toobusy.interval(); diff --git a/package.json b/package.json index e49e3b2..10478ae 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "toobusy-js", + "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", "homepage": "https://github.com/STRML/node-toobusy", - "version": "0.5.1", + "version": "0.6.0", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", @@ -10,10 +10,11 @@ "should": "1.2.1" }, "maintainers": [ - "Samuel Reed " + "Samuel Reed ", + "Luis Helder " ], "license": "WTFPL", - "repository": "STRML/node-toobusy", + "repository": "luislhl/node-toobusy", "engines": { "node": ">=0.9.1" }, From 1477c17791c5ac8ac98b09ecef831effcc6a7c1d Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 11:31:33 -0300 Subject: [PATCH 06/20] 0.6.1 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 10478ae..e76611a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", "homepage": "https://github.com/STRML/node-toobusy", - "version": "0.6.0", + "version": "0.6.1", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", @@ -11,7 +11,7 @@ }, "maintainers": [ "Samuel Reed ", - "Luis Helder " + "Luis Helder " ], "license": "WTFPL", "repository": "luislhl/node-toobusy", From 6ea7ac19bc49d98967ebfff09f4bd98573967f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 14:09:31 -0300 Subject: [PATCH 07/20] Update package.json --- package.json | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e76611a..8610fa0 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", - "homepage": "https://github.com/STRML/node-toobusy", - "version": "0.6.1", + "homepage": "https://github.com/luislhl/node-toobusy#readme", + "version": "0.6.2", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", @@ -14,11 +14,24 @@ "Luis Helder " ], "license": "WTFPL", - "repository": "luislhl/node-toobusy", + "repository": { + "type": "git", + "url": "https://github.com/luislhl/node-toobusy.git" + }, "engines": { "node": ">=0.9.1" }, "scripts": { + "prepublishOnly": "npm test", "test": "mocha tests" - } + }, + "keywords": [ + "toobusy", + "node-toobusy", + "event", + "loop", + "lag", + "maxlag" + ], + "tonicExampleFilename": "examples/example.js", } From 19b50589f5a8bbb2db89def1c93f866cb0d3ad7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 14:19:17 -0300 Subject: [PATCH 08/20] set example --- README.md | 12 ++++++------ examples/example.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 examples/example.js diff --git a/README.md b/README.md index 404726f..e45f462 100644 --- a/README.md +++ b/README.md @@ -67,11 +67,11 @@ var server = app.listen(3000); The library exposes a few knobs: **maxLag** - This number represents the maximum amount of time in milliseconds that the event queue is behind, -before we consider the process *too busy*. -**interval** - The check interval for measuring event loop lag, in ms. -**smoothingFactor** - When a new lag is measured, we smooth its value using the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing). -There are two factors available, the smoothingFactorOnRise, which is used when the new lag is higher than currentLag, and the smoothingFactorOnFall, which is used when the new lag is lower than currentLag. -It's a good idea to keep the factor on fall higher than on rise, to make the currentLag recover faster after spikes. +before we consider the process *too busy*. +**interval** - The check interval for measuring event loop lag, in ms. +**smoothingFactor** - When a new lag is measured, we smooth its value using the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing). +There are two factors available, the smoothingFactorOnRise, which is used when the new lag is higher than currentLag, and the smoothingFactorOnFall, which is used when the new lag is lower than currentLag. +It's a good idea to keep the factor on fall higher than on rise, to make the currentLag recover faster after spikes. **lagFunction** - This is the function used to calculate currentLag. You can overwrite it if you need a different behavior. The parameters passed to it are: `lag`, `currentLag`, `smoothingFactorOnRise` and `smoothingFactorOnFall`. ```javascript @@ -94,7 +94,7 @@ toobusy.smoothingFactorOnFall(3/4); // You can overwrite this function to change the way currentLag is calculated. // This is the default implementation. -toobusy.lagFunction = function(lag, cLag, , sFactorRise, sFactorFall) { +toobusy.lagFunction = function(lag, cLag, sFactorRise, sFactorFall) { var factor = lag > cLag ? sFactorRise : sFactorFall; return factor * lag + (1 - factor) * cLag; } diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..36c90dc --- /dev/null +++ b/examples/example.js @@ -0,0 +1,37 @@ +var toobusy = require('toobusy-js'); + +// Set maximum lag to an aggressive value. +toobusy.maxLag(10); + +// Set check interval to a faster value. This will catch more latency spikes +// but may cause the check to be too sensitive. +toobusy.interval(250); + +// Set smoothing factor on rise to a lower value. This will make it less sensible +// to spikes. Default is 1/3. +toobusy.smoothingFactorOnRise(1/4); + +// Set smoothing factor on fall to a higher value. This will make it recover faster +// after spikes. Default is 2/3. +toobusy.smoothingFactorOnFall(3/4); + +// You can overwrite this function to change the way currentLag is calculated. +// This is the default implementation. +toobusy.lagFunction = function(lag, currentLag, smoothingUp, smoothingDown) { + var factor = lag > currentLag ? smoothingUp : smoothingDown; + return factor * lag + (1 - factor) * currentLag; +} + +// Get current maxLag or interval setting by calling without parameters. +var currentMaxLag = toobusy.maxLag(), interval = toobusy.interval(); + +toobusy.onLag(function(currentLag) { + console.log("Event loop lag detected! Latency: " + currentLag + "ms"); +}); + +// check if node is too busy +if (toobusy()) { + console.warn("TooBusy!"); +} + +console.log("Current adjusted event lag", toobusy.lag(), "ms"); From b8d7d50c1e3fc7bbe81eef581eadec7305462acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 14:46:42 -0300 Subject: [PATCH 09/20] fix package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8610fa0..ffd844f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", "homepage": "https://github.com/luislhl/node-toobusy#readme", - "version": "0.6.2", + "version": "0.6.1", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", @@ -33,5 +33,5 @@ "lag", "maxlag" ], - "tonicExampleFilename": "examples/example.js", + "tonicExampleFilename": "examples/example.js" } From a0db7b9fa2a0e5c7831e58faf9572a49ed3cf721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 14:56:41 -0300 Subject: [PATCH 10/20] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e45f462..02f75b2 100644 --- a/README.md +++ b/README.md @@ -67,12 +67,12 @@ var server = app.listen(3000); The library exposes a few knobs: **maxLag** - This number represents the maximum amount of time in milliseconds that the event queue is behind, -before we consider the process *too busy*. -**interval** - The check interval for measuring event loop lag, in ms. -**smoothingFactor** - When a new lag is measured, we smooth its value using the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing). -There are two factors available, the smoothingFactorOnRise, which is used when the new lag is higher than currentLag, and the smoothingFactorOnFall, which is used when the new lag is lower than currentLag. -It's a good idea to keep the factor on fall higher than on rise, to make the currentLag recover faster after spikes. -**lagFunction** - This is the function used to calculate currentLag. You can overwrite it if you need a different behavior. The parameters passed to it are: `lag`, `currentLag`, `smoothingFactorOnRise` and `smoothingFactorOnFall`. +before we consider the process *too busy*. +**interval** - The check interval for measuring event loop lag, in ms. +**smoothingFactor** - When a new lag is measured, we smooth its value using the standard [exponential smoothing formula](https://en.wikipedia.org/wiki/Exponential_smoothing). +There are two factors available, the smoothingFactorOnRise, which is used when the new lag is higher than currentLag, and the smoothingFactorOnFall, which is used when the new lag is lower than currentLag. +It's a good idea to keep the factor on fall higher than on rise, to make the currentLag recover faster after spikes. +**lagFunction** - This is the function used to calculate currentLag. You can overwrite it if you need a different behavior. The parameters passed to it are: `lag`, `currentLag`, `smoothingFactorOnRise` and `smoothingFactorOnFall`. ```javascript var toobusy = require('toobusy-js'); From e54a2b7a563861656d69f42fc59914630df6f873 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 15:03:48 -0300 Subject: [PATCH 11/20] Update Changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b3cb81..5d53922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +### 0.6.2 + * New example and improvements in package.json. Thanks @khalidsalomao + +### 0.6.1 + * New smoothing factor to be used when lag is falling + * Isolate logic of currentLag in an overwritable function + ### 0.5.1 * Set `onLag` default threshould to `maxLag()`. Thanks @flentini From 4e3c5c6b4b21531fc21ee0939210205b181298d0 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 15:04:42 -0300 Subject: [PATCH 12/20] 0.6.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ffd844f..4b9bb05 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", "homepage": "https://github.com/luislhl/node-toobusy#readme", - "version": "0.6.1", + "version": "0.6.2", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", From e46b4887bc569f44bf7715f3479f97d56b2bcfc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 19:08:51 -0300 Subject: [PATCH 13/20] Update example.js --- examples/example.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example.js b/examples/example.js index 36c90dc..b7a7ffe 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,4 +1,4 @@ -var toobusy = require('toobusy-js'); +var toobusy = require('node-toobusy'); // Set maximum lag to an aggressive value. toobusy.maxLag(10); From 9bd9d215ad8c25ca8aaa220d46406d2821a127c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 19:13:09 -0300 Subject: [PATCH 14/20] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 02f75b2..59629f5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Is Your Node Process Too Busy? -`toobusy-js` is a fork of lloyd's [node-toobusy](http://github.com/lloyd/node-toobusy) that removes native dependencies +`node-toobusy` is a fork of lloyd's [node-toobusy](http://github.com/lloyd/node-toobusy) that removes native dependencies in favor of using the `unref` introduced in [node 0.9.1](http://blog.nodejs.org/2012/08/28/node-v0-9-1-unstable/). This package is a simpler install without native dependencies, but requires node >= 0.9.1. @@ -38,7 +38,7 @@ npm install node-toobusy ## usage ```javascript -var toobusy = require('toobusy-js'), +var toobusy = require('node-toobusy'), express = require('express'); var app = express(); @@ -75,7 +75,7 @@ It's a good idea to keep the factor on fall higher than on rise, to make the cur **lagFunction** - This is the function used to calculate currentLag. You can overwrite it if you need a different behavior. The parameters passed to it are: `lag`, `currentLag`, `smoothingFactorOnRise` and `smoothingFactorOnFall`. ```javascript -var toobusy = require('toobusy-js'); +var toobusy = require('node-toobusy'); // Set maximum lag to an aggressive value. toobusy.maxLag(10); @@ -120,7 +120,7 @@ The default of 70 should get you started. ## Events -As of `0.5.0`, `toobusy-js` exposes an `onLag` method. Pass it a callback to be notified when +As of `0.5.0`, `node-toobusy` exposes an `onLag` method. Pass it a callback to be notified when a slow event loop tick has been detected. ## references From 26bb070a9388aea72d3a7cc732b22eb46127765e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khalid=20Salom=C3=A3o?= Date: Thu, 13 Jul 2017 19:13:32 -0300 Subject: [PATCH 15/20] Update package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 4b9bb05..4524a4c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "event", "loop", "lag", - "maxlag" + "maxlag", + "busy" ], "tonicExampleFilename": "examples/example.js" } From c61d79edb66408e666876a984747b6cdef512415 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 19:44:16 -0300 Subject: [PATCH 16/20] 0.6.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4524a4c..ebe3770 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-toobusy", "description": "Don't fall over when your Node.JS server is too busy. Now without native dependencies!", "homepage": "https://github.com/luislhl/node-toobusy#readme", - "version": "0.6.2", + "version": "0.6.3", "dependencies": {}, "devDependencies": { "mocha": "1.7.0", From 4b2c3201641b4d2f2e419ee0780f5809f1bcd167 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 19:45:14 -0300 Subject: [PATCH 17/20] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d53922..91857a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### 0.6.3 + * Update example.js. Thanks @khalidsalomao + ### 0.6.2 * New example and improvements in package.json. Thanks @khalidsalomao From 87cfd99ac66ed8c0730c2cecedbc1c4d19ab2abf Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 19:56:20 -0300 Subject: [PATCH 18/20] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ab6236f..4532d48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ node_js: notifications: email: - - sam@tixelated.com + - luislhl@gmail.com From c8235380cb5cc035c3dda2cef75d56a9a7df342f Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 19:58:20 -0300 Subject: [PATCH 19/20] Update travis repo url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 59629f5..45bdd71 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://secure.travis-ci.org/STRML/node-toobusy.png)](http://travis-ci.org/STRML/node-toobusy) +[![Build Status](https://secure.travis-ci.org/luislhl/node-toobusy.png)](http://travis-ci.org/luislhl/node-toobusy) # Is Your Node Process Too Busy? From c15994fe3b8526dbd033713d82f148a311849a23 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 13 Jul 2017 20:08:55 -0300 Subject: [PATCH 20/20] Add npm badge to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 45bdd71..37366e8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ [![Build Status](https://secure.travis-ci.org/luislhl/node-toobusy.png)](http://travis-ci.org/luislhl/node-toobusy) +[![npm version](https://badge.fury.io/js/node-toobusy.svg)](https://www.npmjs.com/package/node-toobusy) # Is Your Node Process Too Busy?