2

如果错误,我想在请求中 res.end('server error') 。我的代码:

http.createServer(function(req,res){
  process.on('uncaughtException', function() {
    res.end('server error')
  })
  handlers(req,res)
}).listen(1337)

我的决定有什么问题?

4

2 回答 2

4

您将收到如下错误/警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:389:29)
    at Server.<anonymous> (/Users/taf2/work/phonetrac/services/ctm-pool/foo.js:2:11)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1572:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
    at Socket.ondata (http.js:1468:22)
    at TCP.onread (net.js:374:27)

我将假设您正在执行上述操作,因为您注意到当您开始捕获 uncaughtException 时,客户端连接不再被关闭,因此,如果出现错误,客户端连接将永远不会被释放。例如,如果您将 node.js 端点作为客户端请求嵌入到脚本标记中,例如

<script src="http://127.0.0.1:1337/yourendpoint.js"></script>

这将挂起您的服务的消费者...希望您使用异步技术嵌入脚本...

无论如何,只要您处理释放事件侦听器,就可以使用您的技术处理错误。

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  this.end();
  this.emit("cleanup");
}

require('http').createServer(function(req, res) {
  var errorHandler = handleError.bind(res)
  process.on("uncaughtException", errorHandler);
  res.on("cleanup", function() { process.removeListener("uncaughtException", errorHandler); });

  this.is.an.error;

  res.end();
  res.emit("cleanup");
}).listen(1337);

尝试一下,我们没有泄漏,我们的错误被处理并且连接被关闭,让我们的客户可以自由地转移到下一个错误:

ab -n 100 -c 10 http://127.0.0.1:1337/

但是,如果您的服务器执行以下复杂操作,此解决方案将在并发下崩溃:

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  this.end();
  this.emit("error:cleanup");
}

require('http').createServer(function(req, res) {
  var errorHandler = handleError.bind(res)
  process.on("uncaughtException", errorHandler);
  res.on("error:cleanup", function() { process.removeListener("uncaughtException", errorHandler); });

  setTimeout(function() {
    this.is.an.error;
    res.end();
    res.emit("cleanup");
  },1000);

}).listen(1337);

这里的问题是所有错误都会触发 uncaughtException 并且它们会重叠。这意味着要捕获这样的全局错误,最好只有一个 process.on("uncaughtException" 处理程序。

相反,在上述情况下,您需要:

  • 有一个独立的 try { } catch {} 块——这很难做到。
  • 允许异常未被捕获并使进程崩溃,使用 monit 或类似的东西重新启动进程。
  • 您可以尝试使用带有 setInternval 清理功能的数组来过滤掉关闭的连接。

以下是使用 setInterval。

var connectionIndex = 0;
var connections = [];

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  connections.forEach(function(conn) {
    conn.end();
  });
}

var server = require('http').createServer(function(req, res) {
  connections.push(res.connection);

  setTimeout(function() {
    this.is.an.error;
    res.end();
  },100);

});

setInterval(function() {
  console.log("conn check: %d", connections.length);
  connections = connections.filter(function(conn) {
    return !conn.destroyed;
  });
},1000);

process.on("uncaughtException", handleError);

server.listen(1337);

我有点喜欢最后一个解决方案,但我敢肯定它可能会咬人,所以使用第二个解决方案有一个监控服务来重新启动你的服务器可能是最好的。

于 2012-05-30T02:37:37.220 回答
-1
process.on("uncaughtException", handleError);
于 2012-09-18T00:09:40.233 回答