summaryrefslogtreecommitdiff
path: root/lib/net.js
diff options
context:
space:
mode:
authorAndreas Madsen <amwebdk@gmail.com>2017-11-22 18:41:00 +0100
committerAndreas Madsen <amwebdk@gmail.com>2017-12-19 18:04:52 +0100
commit3b8da4cbe8a7f36fcd8892c6676a55246ba8c3be (patch)
tree1afc84002d1716f4acd28126df214127e0262c3c /lib/net.js
parent0784b0440c05464f79b857f7d8698fcc953d3fb3 (diff)
downloadnode-new-3b8da4cbe8a7f36fcd8892c6676a55246ba8c3be.tar.gz
async_hooks: use scope for defaultTriggerAsyncId
Previously the getter would mutate the kDefaultTriggerAsncId value. This refactor changes the setter to bind the current kDefaultTriggerAsncId to a scope, such that the getter doesn't have to mutate its own value. PR-URL: https://github.com/nodejs/node/pull/17273 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/net.js')
-rw-r--r--lib/net.js91
1 files changed, 46 insertions, 45 deletions
diff --git a/lib/net.js b/lib/net.js
index 33c41d1085..ce87a60ce0 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -43,7 +43,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
const { PipeConnectWrap } = process.binding('pipe_wrap');
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
const { async_id_symbol } = process.binding('async_wrap');
-const { newUid, setDefaultTriggerAsyncId } = require('internal/async_hooks');
+const { newUid, defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const dns = require('dns');
@@ -277,6 +277,14 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
timers._unrefActive(s);
};
+
+function shutdownSocket(self, callback) {
+ var req = new ShutdownWrap();
+ req.oncomplete = callback;
+ req.handle = self._handle;
+ return self._handle.shutdown(req);
+}
+
// the user has called .end(), and all the bytes have been
// sent out to the other side.
function onSocketFinish() {
@@ -298,14 +306,9 @@ function onSocketFinish() {
if (!this._handle || !this._handle.shutdown)
return this.destroy();
- var req = new ShutdownWrap();
- req.oncomplete = afterShutdown;
- req.handle = this._handle;
- // node::ShutdownWrap isn't instantiated and attached to the JS instance of
- // ShutdownWrap above until shutdown() is called. So don't set the init
- // trigger id until now.
- setDefaultTriggerAsyncId(this[async_id_symbol]);
- var err = this._handle.shutdown(req);
+ var err = defaultTriggerAsyncIdScope(
+ this[async_id_symbol], [this, afterShutdown], shutdownSocket
+ );
if (err)
return this.destroy(errnoException(err, 'shutdown'));
@@ -945,23 +948,15 @@ function internalConnect(
req.localAddress = localAddress;
req.localPort = localPort;
- // node::TCPConnectWrap isn't instantiated and attached to the JS instance
- // of TCPConnectWrap above until connect() is called. So don't set the init
- // trigger id until now.
- setDefaultTriggerAsyncId(self[async_id_symbol]);
if (addressType === 4)
err = self._handle.connect(req, address, port);
else
err = self._handle.connect6(req, address, port);
-
} else {
const req = new PipeConnectWrap();
req.address = address;
req.oncomplete = afterConnect;
- // node::PipeConnectWrap isn't instantiated and attached to the JS instance
- // of PipeConnectWrap above until connect() is called. So don't set the
- // init trigger id until now.
- setDefaultTriggerAsyncId(self[async_id_symbol]);
+
err = self._handle.connect(req, address, afterConnect);
}
@@ -1030,7 +1025,9 @@ Socket.prototype.connect = function(...args) {
'string',
path);
}
- internalConnect(this, path);
+ defaultTriggerAsyncIdScope(
+ this[async_id_symbol], [this, path], internalConnect
+ );
} else {
lookupAndConnect(this, options);
}
@@ -1073,7 +1070,11 @@ function lookupAndConnect(self, options) {
if (addressType) {
nextTick(self[async_id_symbol], function() {
if (self.connecting)
- internalConnect(self, host, port, addressType, localAddress, localPort);
+ defaultTriggerAsyncIdScope(
+ self[async_id_symbol],
+ [self, host, port, addressType, localAddress, localPort],
+ internalConnect
+ );
});
return;
}
@@ -1097,33 +1098,33 @@ function lookupAndConnect(self, options) {
debug('connect: dns options', dnsopts);
self._host = host;
var lookup = options.lookup || dns.lookup;
- setDefaultTriggerAsyncId(self[async_id_symbol]);
- lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
- self.emit('lookup', err, ip, addressType, host);
+ defaultTriggerAsyncIdScope(self[async_id_symbol], [], function() {
+ lookup(host, dnsopts, function emitLookup(err, ip, addressType) {
+ self.emit('lookup', err, ip, addressType, host);
- // It's possible we were destroyed while looking this up.
- // XXX it would be great if we could cancel the promise returned by
- // the look up.
- if (!self.connecting) return;
+ // It's possible we were destroyed while looking this up.
+ // XXX it would be great if we could cancel the promise returned by
+ // the look up.
+ if (!self.connecting) return;
- if (err) {
- // net.createConnection() creates a net.Socket object and
- // immediately calls net.Socket.connect() on it (that's us).
- // There are no event listeners registered yet so defer the
- // error event to the next tick.
- err.host = options.host;
- err.port = options.port;
- err.message = err.message + ' ' + options.host + ':' + options.port;
- process.nextTick(connectErrorNT, self, err);
- } else {
- self._unrefTimer();
- internalConnect(self,
- ip,
- port,
- addressType,
- localAddress,
- localPort);
- }
+ if (err) {
+ // net.createConnection() creates a net.Socket object and
+ // immediately calls net.Socket.connect() on it (that's us).
+ // There are no event listeners registered yet so defer the
+ // error event to the next tick.
+ err.host = options.host;
+ err.port = options.port;
+ err.message = err.message + ' ' + options.host + ':' + options.port;
+ process.nextTick(connectErrorNT, self, err);
+ } else {
+ self._unrefTimer();
+ defaultTriggerAsyncIdScope(
+ self[async_id_symbol],
+ [self, ip, port, addressType, localAddress, localPort],
+ internalConnect
+ );
+ }
+ });
});
}