pomelo-upgrade
Version:
53 lines (47 loc) • 1.3 kB
JavaScript
;
/**
* Filter for timeout.
* Print a warn information when request timeout.
*/
const logger = require('pomelo-logger-upgrade').getLogger('pomelo', __filename);
const utils = require('../../util/utils');
const DEFAULT_TIMEOUT = 3000;
const DEFAULT_SIZE = 500;
module.exports = function(timeout, maxSize)
{
return new Filter(timeout || DEFAULT_TIMEOUT, maxSize || DEFAULT_SIZE);
};
const Filter = function(timeout, maxSize)
{
this.timeout = timeout;
this.maxSize = maxSize;
this.timeouts = {};
this.curId = 0;
};
Filter.prototype.before = function(msg, session, next)
{
const count = utils.size(this.timeouts);
if (count > this.maxSize)
{
logger.warn('timeout filter is out of range, current size is %s, max size is %s', count, this.maxSize);
next();
return;
}
this.curId++;
this.timeouts[this.curId] = setTimeout(function()
{
logger.error('request %j timeout.', msg.__route__);
}, this.timeout);
session.__timeout__ = this.curId;
next();
};
Filter.prototype.after = function(err, msg, session, resp, next)
{
const timeout = this.timeouts[session.__timeout__];
if (timeout)
{
clearTimeout(timeout);
delete this.timeouts[session.__timeout__];
}
next(err);
};