Build Node.JS servers that don't fall over.
What happens when your service is overwhelmed with traffic? Your server can do one of two things:
This library helps you do the latter.
toobusy polls the node.js event loop and keeps track of "lag",
which is long requests wait in node's event queue to be processed.
When lag crosses a threshold, toobusy tells you that you're too busy.
At this point you can stop request processing early
(before you spend too much time on them and compound the problem),
and return a "Server Too Busy" response.
This allows your server to stay responsive under extreme load,
and continue serving as many requests as possible.
npm install toobusyvar toobusy = require('toobusy'),
express = require('express');
var app = express();
// middleware which blocks requests when we're too busy
app.use(function(req, res, next) {
if (toobusy()) {
res.send(503, "I'm busy right now, sorry.");
} else {
next();
}
});
app.get('/', function(req, res) {
// processing the request requires some work!
var i = 0;
while (i There is nothing new under the sun. (Ecclesiastes 1:9)
Though applying "event loop latency" to node.js was not directly inspired by anyone else's work,
this concept is not new. Here are references to others who apply the same technique:
* [Provos, Lever, and Tweedie 2000](http://www.kegel.com/c10k.html#tips) - "notes that dropping incoming connections when the server is overloaded improved the shape of the performance curve."
## license
[WTFPL](http://wtfpl.org)No open issues yet, or sync has not completed.