[英]How nodejs domains actually work behind the scenes for multiple requests?
My use case requires node.js domains to share information across server files at a request level. 我的用例要求node.js域在请求级别跨服务器文件共享信息。
Sample Implementation in express.js express.js中的示例实现
domain = require('domain');
app.use(function(req, res, next) {
var reqDomain = domain.create();
reqDomain.add(req);
reqDomain.add(res);
reqDomain.run(next);
});
More explanation at Nodejs Domains Explicit Binding 有关Nodejs域显式绑定的更多说明
In controller / service - process.domain will provide you above created domain And you can easily bind values to this domain. 在控制器/服务中 ,process.domain将为您提供以上创建的域,并且您可以轻松地将值绑定到该域。 For eg:
例如:
process.domain.obj = {};
This explanation is sufficient to understand the usage of Domains. 此说明足以理解域的用法。
Questions 问题
Is it safe to use domains for multiple requests ? 将域用于多个请求是否安全?
How to ensure process.domain is different for different requests and not the same ? 如何确保process.domain对于不同的请求是不同的并且不相同?
I would also like to know how such issues are handled in continuation local storage 我还想知道如何在连续本地存储中处理此类问题
First of all - domains are deprecated and will be removed in an upcoming release of NodeJS. 首先-不推荐使用域,并将在即将发布的NodeJS版本中将其删除。 I would not write new code using them.
我不会使用它们编写新代码。
Second of all - it's important to understand domains are not magic. 第二-重要的是要了解域不是魔术。 They're really a simple concept.
它们确实是一个简单的概念。 Basically, they:
基本上,他们:
Here's how one can implement domains, let's only implement it for setTimeout
for simplicity. 这是实现域的方法,为简单起见,我们只为
setTimeout
实现它。
const oldTimeout = setTimeout;
setTimeout = function(fn, ms) { // also ...args, but let's ignore that
var trackedDomain = domain;
oldTimeout(function() {
var oldDomain = domain; // preserve old context
domain = trackedDomain; // restore context to "global" variable
fn(); // call the function itself
domain = oldDomain; // restore old context
}, ms); // that's it!
};
Something like express
can just do domain = new RequestContext
at the start and then all methods called in the request would work since they're all wrapped like in the example above (since again, it's baked into node itself). 诸如
express
东西只能在开始时执行domain = new RequestContext
,然后在请求中调用的所有方法都将起作用,因为它们都像上面的示例一样被包装(因为再次被烘焙到节点本身中)。
They're being removed because of the implementation complexity they add and the fact they're leaky and error recovery has edge cases where it doesn't work. 由于它们添加的实现复杂性以及它们泄漏且错误恢复存在无法正常工作的极端情况,因此将它们删除。
You have alternatives, for example bluebird promises have .bind
which brings promise chains context which is a less leaky way to do this. 您有其他选择,例如,bluebird promises具有
.bind
,它带来了promise链上下文,这是一种不那么容易泄漏的方式。
That said, I would just avoid implicit global context altogether. 也就是说,我将完全避免隐式全局上下文。 It makes refactoring hard, it makes dependencies implicit and it makes code harder to reason about.
它使重构变得困难,使依赖关系隐式化,并使代码难以推理。 I'd just pass relevant context around to objects when I create them (dependency injection, in a nutshell) rather than set a global variable.
创建对象时,我只是将相关上下文传递给对象(简而言之,依赖注入),而不是设置全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.