简体   繁体   English

nodejs域实际上如何在后台处理多个请求?

[英]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 问题

  1. Is it safe to use domains for multiple requests ? 将域用于多个请求是否安全?

  2. 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 我还想知道如何在连续本地存储中处理此类问题

A warning 一个警告

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. 我不会使用它们编写新代码。

How domains work? 域是如何工作的?

Second of all - it's important to understand domains are not magic. 第二-重要的是要了解域不是魔术。 They're really a simple concept. 它们确实是一个简单的概念。 Basically, they: 基本上,他们:

  • Wrap every async call in the platform (the entire NodeJS APIs). 在平台(整个NodeJS API)中包装每个异步调用。
  • Set a "global" variable for the duration of the call. 在通话期间设置一个“全局”变量。
  • If another async call is being made during that call - keep note to set the global variable to the same value when you enter it. 如果在该调用期间进行了另一个异步调用-请注意在输入全局变量时将其设置为相同的值。
  • That's it. 而已。

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 ,然后在请求中调用的所有方法都将起作用,因为它们都像上面的示例一样被包装(因为再次被烘焙到节点本身中)。

That sounds swell, why remove them? 听起来不错,为什么要删除它们?

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. 由于它们添加的实现复杂性以及它们泄漏且错误恢复存在无法正常工作的极端情况,因此将它们删除。

So what do I do? 那我该怎么办?

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.

相关问题 数组在幕后如何工作? - How do arrays work, behind the scenes? JavaScript 承诺如何在幕后工作 - How JavaScript promises work behind the scenes 在JavaScript中,forEach回调如何在后台运行? 有收益声明吗? - How does the forEach callback work behind the scenes in javascript? Is there a yield statement? a.map 中的异步等待如何在幕后工作? - How does async await inside a .map work behind the scenes? JQuery的.click()如何在幕后工作? - How does JQuery's .click() work behind the scenes? 如何安装多个域的多个nodejs应用程序? - how to install multiple nodejs app with multiple domains? 通过import语句加载的ECMA-Script 2015(及更高版本)模块实际上在后台如何工作? - How actually ECMA-Script 2015 (and above) module loading through import statement works behind the scenes? Javascript 的 URL() 构造函数如何在幕后工作(getter/setter 怪异) - How does Javascript's URL() constructor work behind the scenes (getter/setter weirdness) NodeJS Express - 幕后GET/POST/PATCH/DELETE的区别 - NodeJS Express - Difference between GET/POST/PATCH/DELETE behind the scenes angularjs,$ scope参数在后台如何工作? - angularjs, how $scope parameter works behind the scenes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM