summaryrefslogtreecommitdiff
path: root/lib/domain.js
diff options
context:
space:
mode:
authorvladimir <vlad2t@hotmail.com>2017-10-10 22:40:43 +0200
committerAnna Henningsen <anna@addaleax.net>2017-11-19 17:39:19 +0100
commit51e0948862f8920c0387f6702843e8fd79f24172 (patch)
tree4f2983449ee4e99cb0acdffad3c750bcf9736e01 /lib/domain.js
parent2d50b5e537dd94f238a499083cd234ceca3fff5f (diff)
downloadandroid-node-v8-51e0948862f8920c0387f6702843e8fd79f24172.tar.gz
android-node-v8-51e0948862f8920c0387f6702843e8fd79f24172.tar.bz2
android-node-v8-51e0948862f8920c0387f6702843e8fd79f24172.zip
domain: re-implement domain over async_hook
Domain core module has been re-implemented over async_hook. PR-URL: https://github.com/nodejs/node/pull/16222 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/domain.js')
-rw-r--r--lib/domain.js60
1 files changed, 44 insertions, 16 deletions
diff --git a/lib/domain.js b/lib/domain.js
index 676a9cb005..dc3c550866 100644
--- a/lib/domain.js
+++ b/lib/domain.js
@@ -28,7 +28,7 @@
const util = require('util');
const EventEmitter = require('events');
-const { inherits } = util;
+const { createHook } = require('async_hooks');
// communicate with events module, but don't require that
// module to have to load this one, since this module has
@@ -48,13 +48,54 @@ Object.defineProperty(process, 'domain', {
}
});
+const pairing = new Map();
+const asyncHook = createHook({
+ init(asyncId, type, triggerAsyncId, resource) {
+ if (process.domain !== null && process.domain !== undefined) {
+ // if this operation is created while in a domain, let's mark it
+ pairing.set(asyncId, process.domain);
+ resource.domain = process.domain;
+ if (resource.promise !== undefined &&
+ resource.promise instanceof Promise) {
+ // resource.promise instanceof Promise make sure that the
+ // promise comes from the same context
+ // see https://github.com/nodejs/node/issues/15673
+ resource.promise.domain = process.domain;
+ }
+ }
+ },
+ before(asyncId) {
+ const current = pairing.get(asyncId);
+ if (current !== undefined) { // enter domain for this cb
+ current.enter();
+ }
+ },
+ after(asyncId) {
+ const current = pairing.get(asyncId);
+ if (current !== undefined) { // exit domain for this cb
+ current.exit();
+ }
+ },
+ destroy(asyncId) {
+ pairing.delete(asyncId); // cleaning up
+ }
+});
+
// It's possible to enter one domain while already inside
// another one. The stack is each entered domain.
const stack = [];
exports._stack = stack;
+process._setupDomainUse(stack);
+
+class Domain extends EventEmitter {
-// let the process know we're using domains
-const _domain_flag = process._setupDomainUse(_domain, stack);
+ constructor() {
+ super();
+
+ this.members = [];
+ asyncHook.enable();
+ }
+}
exports.Domain = Domain;
@@ -64,19 +105,8 @@ exports.create = exports.createDomain = function() {
// the active domain is always the one that we're currently in.
exports.active = null;
-
-
-inherits(Domain, EventEmitter);
-
-function Domain() {
- EventEmitter.call(this);
-
- this.members = [];
-}
-
Domain.prototype.members = undefined;
-
// Called by process._fatalException in case an error was thrown.
Domain.prototype._errorHandler = function _errorHandler(er) {
var caught = false;
@@ -155,7 +185,6 @@ Domain.prototype.enter = function() {
// to push it onto the stack so that we can pop it later.
exports.active = process.domain = this;
stack.push(this);
- _domain_flag[0] = stack.length;
};
@@ -166,7 +195,6 @@ Domain.prototype.exit = function() {
// exit all domains until this one.
stack.splice(index);
- _domain_flag[0] = stack.length;
exports.active = stack[stack.length - 1];
process.domain = exports.active;