pomelo
Version:
Pomelo is a fast, scalable game server framework for [node.js](http://nodejs.org). It provides the basic development framework and many related components, including libraries and tools. Pomelo is also suitable for real-time web applications; its distri
33 lines (29 loc) • 732 B
JavaScript
/**
* Filter for toobusy.
* if the process is toobusy, just skip the new request
*/
var conLogger = require('pomelo-logger').getLogger('con-log', __filename);
var toobusy = null;
var DEFAULT_MAXLAG = 70;
module.exports = function(maxLag) {
return new Filter(maxLag || DEFAULT_MAXLAG);
};
var Filter = function(maxLag) {
try {
toobusy = require('toobusy');
} catch(e) {
}
if(!!toobusy) {
toobusy.maxLag(maxLag);
}
};
Filter.prototype.before = function(msg, session, next) {
if (!!toobusy && toobusy()) {
conLogger.warn('[toobusy] reject request msg: ' + msg);
var err = new Error('Server toobusy!');
err.code = 500;
next(err);
} else {
next();
}
};