Skip to content

Commit cbbe95e

Browse files
indutnyFishrock123
authored andcommitted
net: introduce Socket#connecting property
There is no official way to figure out if the socket that you have on hand is still connecting to the remote host. Introduce `Socket#connecting`, which is essentially an unprefixed `_connecting` property that we already had. PR-URL: #6404 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent fb6753c commit cbbe95e

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

doc/api/net.md

+6
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ The `connectListener` parameter will be added as a listener for the
400400
As [`socket.connect(options\[, connectListener\])`][`socket.connect(options, connectListener)`],
401401
with options either as either `{port: port, host: host}` or `{path: path}`.
402402

403+
### socket.connecting
404+
405+
If `true` - [`socket.connect(options\[, connectListener\])`][] was called and
406+
haven't yet finished. Will be set to `false` before emitting `connect` event
407+
and/or calling [`socket.connect(options\[, connectListener\])`][]'s callback.
408+
403409
### socket.destroy()
404410

405411
Ensures that no more I/O activity happens on this socket. Only necessary in

lib/_tls_legacy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ CryptoStream.prototype._done = function() {
477477
// readyState is deprecated. Don't use it.
478478
Object.defineProperty(CryptoStream.prototype, 'readyState', {
479479
get: function() {
480-
if (this._connecting) {
480+
if (this.connecting) {
481481
return 'opening';
482482
} else if (this.readable && this.writable) {
483483
return 'open';

lib/_tls_wrap.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ function TLSSocket(socket, options) {
272272

273273
this._init(socket, wrap);
274274

275-
// Make sure to setup all required properties like: `_connecting` before
275+
// Make sure to setup all required properties like: `connecting` before
276276
// starting the flow of the data
277277
this.readable = true;
278278
this.writable = true;
@@ -466,9 +466,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
466466
this._parent = socket;
467467

468468
// To prevent assertion in afterConnect() and properly kick off readStart
469-
this._connecting = socket._connecting || !socket._handle;
469+
this.connecting = socket.connecting || !socket._handle;
470470
socket.once('connect', function() {
471-
self._connecting = false;
471+
self.connecting = false;
472472
self.emit('connect');
473473
});
474474
}
@@ -480,7 +480,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
480480
});
481481
} else {
482482
assert(!socket);
483-
this._connecting = true;
483+
this.connecting = true;
484484
}
485485
};
486486

@@ -581,7 +581,7 @@ TLSSocket.prototype._finishInit = function() {
581581
};
582582

583583
TLSSocket.prototype._start = function() {
584-
if (this._connecting) {
584+
if (this.connecting) {
585585
this.once('connect', function() {
586586
this._start();
587587
});

lib/net.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const BYTES_READ = Symbol('bytesRead');
119119
function Socket(options) {
120120
if (!(this instanceof Socket)) return new Socket(options);
121121

122-
this._connecting = false;
122+
this.connecting = false;
123123
this._hadError = false;
124124
this._handle = null;
125125
this._parent = null;
@@ -202,7 +202,7 @@ Socket.prototype._unrefTimer = function unrefTimer() {
202202
// so that only the writable side will be cleaned up.
203203
function onSocketFinish() {
204204
// If still connecting - defer handling 'finish' until 'connect' will happen
205-
if (this._connecting) {
205+
if (this.connecting) {
206206
debug('osF: not yet connected');
207207
return this.once('connect', onSocketFinish);
208208
}
@@ -367,9 +367,16 @@ Socket.prototype.address = function() {
367367
};
368368

369369

370+
Object.defineProperty(Socket.prototype, '_connecting', {
371+
get: function() {
372+
return this.connecting;
373+
}
374+
});
375+
376+
370377
Object.defineProperty(Socket.prototype, 'readyState', {
371378
get: function() {
372-
if (this._connecting) {
379+
if (this.connecting) {
373380
return 'opening';
374381
} else if (this.readable && this.writable) {
375382
return 'open';
@@ -397,7 +404,7 @@ Object.defineProperty(Socket.prototype, 'bufferSize', {
397404
Socket.prototype._read = function(n) {
398405
debug('_read');
399406

400-
if (this._connecting || !this._handle) {
407+
if (this.connecting || !this._handle) {
401408
debug('_read wait for connection');
402409
this.once('connect', () => this._read(n));
403410
} else if (!this._handle.reading) {
@@ -430,7 +437,7 @@ function maybeDestroy(socket) {
430437
if (!socket.readable &&
431438
!socket.writable &&
432439
!socket.destroyed &&
433-
!socket._connecting &&
440+
!socket.connecting &&
434441
!socket._writableState.length) {
435442
socket.destroy();
436443
}
@@ -465,7 +472,7 @@ Socket.prototype._destroy = function(exception, cb) {
465472
return;
466473
}
467474

468-
this._connecting = false;
475+
this.connecting = false;
469476

470477
this.readable = this.writable = false;
471478

@@ -648,7 +655,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
648655
// If we are still connecting, then buffer this for later.
649656
// The Writable logic will buffer up any more writes while
650657
// waiting for this one to be done.
651-
if (this._connecting) {
658+
if (this.connecting) {
652659
this._pendingData = data;
653660
this._pendingEncoding = encoding;
654661
this.once('connect', function() {
@@ -803,7 +810,7 @@ function connect(self, address, port, addressType, localAddress, localPort) {
803810
// TODO return promise from Socket.prototype.connect which
804811
// wraps _connectReq.
805812

806-
assert.ok(self._connecting);
813+
assert.ok(self.connecting);
807814

808815
var err;
809816

@@ -913,7 +920,7 @@ Socket.prototype.connect = function(options, cb) {
913920

914921
this._unrefTimer();
915922

916-
this._connecting = true;
923+
this.connecting = true;
917924
this.writable = true;
918925

919926
if (pipe) {
@@ -952,7 +959,7 @@ function lookupAndConnect(self, options) {
952959
var addressType = exports.isIP(host);
953960
if (addressType) {
954961
process.nextTick(function() {
955-
if (self._connecting)
962+
if (self.connecting)
956963
connect(self, host, port, addressType, localAddress, localPort);
957964
});
958965
return;
@@ -980,7 +987,7 @@ function lookupAndConnect(self, options) {
980987
// It's possible we were destroyed while looking this up.
981988
// XXX it would be great if we could cancel the promise returned by
982989
// the look up.
983-
if (!self._connecting) return;
990+
if (!self.connecting) return;
984991

985992
if (err) {
986993
// net.createConnection() creates a net.Socket object and
@@ -1048,8 +1055,8 @@ function afterConnect(status, handle, req, readable, writable) {
10481055

10491056
debug('afterConnect');
10501057

1051-
assert.ok(self._connecting);
1052-
self._connecting = false;
1058+
assert.ok(self.connecting);
1059+
self.connecting = false;
10531060
self._sockname = null;
10541061

10551062
if (status == 0) {
@@ -1065,7 +1072,7 @@ function afterConnect(status, handle, req, readable, writable) {
10651072
self.read(0);
10661073

10671074
} else {
1068-
self._connecting = false;
1075+
self.connecting = false;
10691076
var details;
10701077
if (req.localAddress && req.localPort) {
10711078
details = req.localAddress + ':' + req.localPort;

test/parallel/test-net-connect-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tcp.listen(common.PORT, function() {
4040
connectHappened = true;
4141
});
4242

43-
console.log('_connecting = ' + socket._connecting);
43+
console.log('connecting = ' + socket.connecting);
4444

4545
assert.equal('opening', socket.readyState);
4646

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
const server = net.createServer((conn) => {
7+
conn.end();
8+
server.close();
9+
}).listen(common.PORT, () => {
10+
const client = net.connect(common.PORT, () => {
11+
assert.strictEqual(client.connecting, false);
12+
13+
// Legacy getter
14+
assert.strictEqual(client._connecting, false);
15+
client.end();
16+
});
17+
assert.strictEqual(client.connecting, true);
18+
19+
// Legacy getter
20+
assert.strictEqual(client._connecting, true);
21+
});

0 commit comments

Comments
 (0)