0
我意识到这个话题已经说了很多,但我相信我正在关闭我的联系,并希望有一个新的眼睛告诉我,如果我失去了一些东西。套接字挂断/ ETIMEDOUT异常与nodeJS
- 创建包含该请求信息的对象,并传递到
throttle
throttle
把它在栈上,并启动间隔计时器它调用process_queue
每1200ms;get_info
作为请求回调传递。- 在某一点(变化)在执行过程中,插座挂断和连接ETIMEDOUT抛出异常,在这一点上,我重新排队未决请求,等待短的时间间隔,并再次启动process_queue。
这种方法在早期效果很好,但异常的发生似乎随着时间的推移而加速,而且真的,如果我正确关闭它们,它们不应该发生。
任何反馈,非常感谢。
var timer = null;
var takeabreather = null;
var stack = [];
var pendingqueue = [];
function throttle(item) {
stack.push(item);
pendingqueue.push(item);
if (timer === null) {
timer = setInterval(process_queue, 1200);
}
}
function process_queue() {
var item = stack.shift();
var req = http.request(item.opts, get_info);
req.on('error', function(e) {
logger.error('--- PROCESS_QUEUE: ERROR: ' + e);
req.end();
});
req.end(function(){
logger.debug('PROCESS_QUEUE: ENDING...');
})
// clear timer is there is no work left to do...
if (stack.length === 0) {
clearInterval(timer);
timer = null;
logger.info('PROCESS_QUEUE: queue is empty');
}
}
function get_info(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var parsed = JSON.parse(body);
var doc = {};
parsed.forEach(function (item) {
try {
doc.name = item.name;
}
catch (err) {
logger.error('--- GET_INFO ERROR: ', response.req.path, err);
}
});
// code to remove item from pending queue redacted //
logger.debug('--- GET_INFO END: ', response.req.path);
});
}
process.on('uncaughtException', function (e) {
logger.error('--- UNCAUGHT_EXCEPTION: ' + e);
clearInterval(timer);
timer = null;
if (takeabreather === null) {
logger.warn('--- REQUEUING...');
stack = pendingqueue;
logger.warn('--- TAKING A BREATHER...');
takeabreather = setTimeout(process_queue, 10000);
}
});