Home > Article > Web Front-end > Detailed explanation of asynchronous exception handling in js and using domian tutorial
This article mainly introduces Node.js asynchronous exception processing and domain module analysis, which has certain reference value. Interested friends can refer to it
Asynchronous exception handling
Characteristics of asynchronous exceptions
Due to the asynchronous nature of node's callbacks, try catch cannot be used to catch all Exception:
try { process.nextTick(function () { foo.bar(); }); } catch (err) { //can not catch it }
For web services, we really hope this:
//express风格的路由 app.get('/index', function (req, res) { try { //业务逻辑 } catch (err) { logger.error(err); res.statusCode = 500; return res.json({success: false, message: '服务器异常'}); } });
If try catch can catch all exceptions, we can prevent some unexpected errors in the code At the same time, it can record the error and return a 500 error to the caller in a friendly manner. Unfortunately, try catch cannot catch exceptions in asynchronous situations. So all we can do is:
app.get('/index', function (req, res) { // 业务逻辑 }); process.on('uncaughtException', function (err) { logger.error(err); });
At this time, although we can record the log of this error, and the process will not exit abnormally , but we have no way to find out Wrong requests are returned friendly, and can only be returned with a timeout.
domain
In node v0.8+, a module domain was released. This module does what try catch cannot: catch exceptions that occur in asynchronous callbacks. Ever since, our helpless example above seems to have a solution:var domain = require('domain'); //引入一个domain的中间件,将每一个请求都包裹在一个独立的domain中 //domain来处理异常 app.use(function (req,res, next) { var d = domain.create(); //监听domain的错误事件 d.on('error', function (err) { logger.error(err); res.statusCode = 500; res.json({sucess:false, messag: '服务器异常'}); d.dispose(); }); d.add(req); d.add(res); d.run(next); }); app.get('/index', function (req, res) { //处理业务 });We introduce domain in the form of middleware to handle asynchronous exceptions. Of course, although the domain has caught the exception, the stack loss caused by the exception will still lead to memory leaks. Therefore, when this happens, you still need to restart the process. Interested students can check out the domain middleware. pieces.
Weird failure
Our test was all normal. When it was officially used in the production environment, we found that the domain suddenly failed! It didn't catch the asynchronous exception, which eventually caused the process to exit abnormally. After some investigation, it was finally found that it was caused by the introduction of redis to storesession.
var http = require('http'); var connect = require('connect'); var RedisStore = require('connect-redis')(connect); var domainMiddleware = require('domain-middleware'); var server = http.createServer(); var app = connect(); app.use(connect.session({ key: 'key', secret: 'secret', store: new RedisStore(6379, 'localhost') })); //domainMiddleware的使用可以看前面的链接 app.use(domainMiddleware({ server: server, killTimeout: 30000 }));At this time, when an exception occurred in our business logic code, we found that it was not captured by the domain! After some attempts, the problem was finally located:
var domain = require('domain'); var redis = require('redis'); var cache = redis.createClient(6379, 'localhost'); function error() { cache.get('a', function () { throw new Error('something wrong'); }); } function ok () { setTimeout(function () { throw new Error('something wrong'); }, 100); } var d = domain.create(); d.on('error', function (err) { console.log(err); }); d.run(ok); //domain捕获到异常 d.run(error); //异常被抛出Strange! They are both asynchronous calls. Why is the former captured but not the latter?
Domain Analysis
Looking back, let’s take a look at what domain does to allow us to capture asynchronous requests (the code comes from node v0.10.4, this part May be under rapid change optimization).node eventLoopMechanism
Before looking at the principle of Domain, we must first understand nextTick and _tickCall## Two methods of #back. function laterCall() {
console.log('print me later');
}
process.nextTick(laterCallback);
console.log('print me first');
Anyone who has written node in the above code is familiar with it. The function of nextTick is to put laterCallback in the next event loop for execution. The _tickCallback method is a non-public method. This method is the entry
that is called to continue the next event loop after the current time loop ends. In other words, node maintains a queue for the event loop, nextTick enters the queue, and _tickCallback dequeues.
Implementation of domainAfter understanding the event loop mechanism of node, let’s take a look at what domain does.
Domain itself is actually an EventEmitter object, which delivers captured errors through events. In this way, when we study it, we simplify it to two points:
When is the error event of the domain triggered:
The process threw an exception, which was not caught by any try catch. At this time, the processFatal of the entire process will be triggered. If it is in the domain package, the error event will be triggered on the domain. Otherwise, the uncaughtException event will be triggered on the process.
How domain is passed in multiple different event loops:
of process.domain will be pointed to this domain instance. When throws an exception in this event loop When processFatal is called and process.domain is found to exist, the error event will be triggered on the domain.
//简化后的domain传递部分代码 function nextDomainTick(callback) { nextTickQueue.push({callback: callback, domain: process.domain}); } function _tickDomainCallback() { var tock = nextTickQueue.pop(); //设置process.domain = tock.domain tock.domain && tock.domain.enter(); callback(); //清除process.domain tock.domain && tock.domain.exit(); } };
这个是其在多个事件循环中传递domain的关键:nextTick入队的时候,记录下当前的domain,当这个被加入队列中的事件循环被_tickCallback启动执行的时候,将新的事件循环的process.domain置为之前记录的domain。这样,在被domain所包裹的代码中,不管如何调用process.nextTick, domain将会一直被传递下去。
当然,node的异步还有两种情况,一种是event形式。因此在EventEmitter的构造函数有如下代码:
if (exports.usingDomains) { // if there is an active domain, then attach to it. domain = domain || require('domain'); if (domain.active && !(this instanceof domain.Domain)) { this.domain = domain.active; } }
实例化EventEmitter的时候,将会把这个对象和当前的domain绑定,当通过emit触发这个对象上的事件时,像_tickCallback执行的时候一样,回调函数将会重新被当前的domain包裹住。
而另一种情况,是setTimeout和setInterval,同样的,在timer的源码中,我们也可以发现这样的一句代码:
if (process.domain) timer.domain = process.domain;
跟EventEmmiter一样,之后这些timer的回调函数也将被当前的domain包裹住了。
node通过在nextTick, timer, event三个关键的地方插入domain的代码,让它们得以在不同的事件循环中传递。
更复杂的domain
有些情况下,我们可能会遇到需要更加复杂的domain使用。
domain嵌套:我们可能会外层有domain的情况下,内层还有其他的domain,使用情景可以在文档中找到
// create a top-level domain for the server var serverDomain = domain.create(); serverDomain.run(function() { // server is created in the scope of serverDomain http.createServer(function(req, res) { // req and res are also created in the scope of serverDomain // however, we'd prefer to have a separate domain for each request. // create it first thing, and add req and res to it. var reqd = domain.create(); reqd.add(req); reqd.add(res); reqd.on('error', function(er) { console.error('Error', er, req.url); try { res.writeHead(500); res.end('Error occurred, sorry.'); } catch (er) { console.error('Error sending 500', er, req.url); } }); }).listen(1337); });
为了实现这个功能,其实domain还会偷偷的自己维持一个domain的stack,有兴趣的童鞋可以在这里看到。
回头解决疑惑
回过头来,我们再来看刚才遇到的问题:为什么两个看上去都是同样的异步调用,却有一个domain无法捕获到异常?理解了原理之后不难想到,肯定是调用了redis的那个异步调用在抛出错误的这个事件循环内,是不在domain的范围之内的。我们通过一段更加简短的代码来看看,到底在哪里出的问题。
var domain = require('domain'); var EventEmitter = require('events').EventEmitter; var e = new EventEmitter(); var timer = setTimeout(function () { e.emit('data'); }, 10); function next() { e.once('data', function () { throw new Error('something wrong here'); }); } var d = domain.create(); d.on('error', function () { console.log('cache by domain'); }); d.run(next);
此时我们同样发现,错误不会被domain捕捉到,原因很清晰了:timer和e两个关键的对象在初始化的时候都时没有在domain的范围之内,因此,当在next函数中监听的事件被触发,执行抛出异常的回调函数时,其实根本就没有处于domain的包裹中,当然就不会被domain捕获到异常了!
其实node针对这种情况,专门设计了一个API:domain.add。它可以将domain之外的timer和event对象,添加到当前domain中去。对于上面那个例子:
d.add(timer); //or d.add(e);
将timer或者e任意一个对象添加到domain上,就可以让错误被domain捕获了。
再来看最开始redis导致domain无法捕捉到异常的问题。我们是不是也有办法可以解决呢?
其实对于这种情况,还是没有办法实现最佳的解决方案的。现在对于非预期的异常产生的时候,我们只能够让当前请求超时,然后让这个进程停止服务,之后重新启动。graceful模块配合cluster就可以实现这个解决方案。
domain十分强大,但不是万能的。希望在看过这篇文章之后,大家能够正确的使用domian,避免踩坑。
【相关推荐】
1. 免费js在线视频教程
3. php.cn独孤九贱(3)-JavaScript视频教程
The above is the detailed content of Detailed explanation of asynchronous exception handling in js and using domian tutorial. For more information, please follow other related articles on the PHP Chinese website!