domain.dispose()究竟在nodejs中做了什么?有钩子吗?

时间:2023-01-24 23:07:31

Reading the documentation at http://nodejs.org/api/domain.html makes it a little vague: "makes a best effort attempt to clean up any and all IO that is associated with the domain". It mentions that timers are shutdown, which isn't exactly IO. It would be very nice to know the comprehensive list of things domain.dispose does. Does anyone have that list?

阅读http://nodejs.org/api/domain.html上的文档使其有点模糊:“尽最大努力尝试清理与域相关的任何和所有IO”。它提到计时器已关闭,这不完全是IO。知道domain.dispose所做的全面的事情列表会很高兴。有人有这个清单吗?

Also, is there any way to hook into that functionality - ie allow some custom clean up code to be called when domain.dispose() is run?

此外,有没有办法挂钩该功能 - 即允许在运行domain.dispose()时调用一些自定义清理代码?

1 个解决方案

#1


5  

The dispose function calls the exit and dispose functions, removes all listeners, removes all error handlers, and attempts to kill all members of the domain. The function the checks if the domain has a parent, and if it does, then it is removed from the domain. The domain is then set for garbage collection, and the marked as disposed.

dispose函数调用exit和dispose函数,删除所有侦听器,删除所有错误处理程序,并尝试终止域的所有成员。检查域是否具有父级的函数,如果是,则从域中删除它。然后将域设置为垃圾收集,并将其标记为已处置。

From the Node documentation:

从Node文档:

Once the domain is disposed the dispose event will emit.

一旦域被处置,dispose事件将发出。

I would go more in-depth on the topic, but the Node source is already nicely annotated.

我会更深入地讨论这个主题,但Node源已经很好地注释了。

The timer you are talking about would be here, where the members of the domain are being iterated through.

你正在谈论的计时器将在这里,域的成员正在迭代。

this.members.forEach(function(m) {
  // if it's a timeout or interval, cancel it.
  clearTimeout(m);
});

Here's from the Node source:

这是来自节点源:

Domain.prototype.dispose = function() {
  if (this._disposed) return;

  // if we're the active domain, then get out now.
  this.exit();

  this.emit('dispose');

  // remove error handlers.
  this.removeAllListeners();
  this.on('error', function() {});

  // try to kill all the members.
  // XXX There should be more consistent ways
  // to shut down things!
  this.members.forEach(function(m) {
    // if it's a timeout or interval, cancel it.
    clearTimeout(m);

    // drop all event listeners.
    if (m instanceof EventEmitter) {
      m.removeAllListeners();
      // swallow errors
      m.on('error', function() {});
    }

    // Be careful!
    // By definition, we're likely in error-ridden territory here,
    // so it's quite possible that calling some of these methods
    // might cause additional exceptions to be thrown.
    endMethods.forEach(function(method) {
      if (typeof m[method] === 'function') {
        try {
          m[method]();
        } catch (er) {}
      }
    });

  });

  // remove from parent domain, if there is one.
  if (this.domain) this.domain.remove(this);

  // kill the references so that they can be properly gc'ed.
  this.members.length = 0;

  // finally, mark this domain as 'no longer relevant'
  // so that it can't be entered or activated.
  this._disposed = true;
};

#1


5  

The dispose function calls the exit and dispose functions, removes all listeners, removes all error handlers, and attempts to kill all members of the domain. The function the checks if the domain has a parent, and if it does, then it is removed from the domain. The domain is then set for garbage collection, and the marked as disposed.

dispose函数调用exit和dispose函数,删除所有侦听器,删除所有错误处理程序,并尝试终止域的所有成员。检查域是否具有父级的函数,如果是,则从域中删除它。然后将域设置为垃圾收集,并将其标记为已处置。

From the Node documentation:

从Node文档:

Once the domain is disposed the dispose event will emit.

一旦域被处置,dispose事件将发出。

I would go more in-depth on the topic, but the Node source is already nicely annotated.

我会更深入地讨论这个主题,但Node源已经很好地注释了。

The timer you are talking about would be here, where the members of the domain are being iterated through.

你正在谈论的计时器将在这里,域的成员正在迭代。

this.members.forEach(function(m) {
  // if it's a timeout or interval, cancel it.
  clearTimeout(m);
});

Here's from the Node source:

这是来自节点源:

Domain.prototype.dispose = function() {
  if (this._disposed) return;

  // if we're the active domain, then get out now.
  this.exit();

  this.emit('dispose');

  // remove error handlers.
  this.removeAllListeners();
  this.on('error', function() {});

  // try to kill all the members.
  // XXX There should be more consistent ways
  // to shut down things!
  this.members.forEach(function(m) {
    // if it's a timeout or interval, cancel it.
    clearTimeout(m);

    // drop all event listeners.
    if (m instanceof EventEmitter) {
      m.removeAllListeners();
      // swallow errors
      m.on('error', function() {});
    }

    // Be careful!
    // By definition, we're likely in error-ridden territory here,
    // so it's quite possible that calling some of these methods
    // might cause additional exceptions to be thrown.
    endMethods.forEach(function(method) {
      if (typeof m[method] === 'function') {
        try {
          m[method]();
        } catch (er) {}
      }
    });

  });

  // remove from parent domain, if there is one.
  if (this.domain) this.domain.remove(this);

  // kill the references so that they can be properly gc'ed.
  this.members.length = 0;

  // finally, mark this domain as 'no longer relevant'
  // so that it can't be entered or activated.
  this._disposed = true;
};