Skip to content

Commit 0eef098

Browse files
indutnyMyles Borins
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 4d6e405 commit 0eef098

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

doc/api/net.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ The `connectListener` parameter will be added as a listener for the
392392
As [`socket.connect(options\[, connectListener\])`][`socket.connect(options, connectListener)`],
393393
with options either as either `{port: port, host: host}` or `{path: path}`.
394394

395+
### socket.connecting
396+
397+
If `true` - [`socket.connect(options\[, connectListener\])`][] was called and
398+
haven't yet finished. Will be set to `false` before emitting `connect` event
399+
and/or calling [`socket.connect(options\[, connectListener\])`][]'s callback.
400+
395401
### socket.destroy()
396402

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

lib/_tls_legacy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ CryptoStream.prototype._done = function() {
480480
// readyState is deprecated. Don't use it.
481481
Object.defineProperty(CryptoStream.prototype, 'readyState', {
482482
get: function() {
483-
if (this._connecting) {
483+
if (this.connecting) {
484484
return 'opening';
485485
} else if (this.readable && this.writable) {
486486
return 'open';

lib/_tls_wrap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ function TLSSocket(socket, options) {
284284

285285
this._init(socket, wrap);
286286

287-
// Make sure to setup all required properties like: `_connecting` before
287+
// Make sure to setup all required properties like: `connecting` before
288288
// starting the flow of the data
289289
this.readable = true;
290290
this.writable = true;
@@ -472,9 +472,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
472472
this._parent = socket;
473473

474474
// To prevent assertion in afterConnect() and properly kick off readStart
475-
this._connecting = socket._connecting || !socket._handle;
475+
this.connecting = socket.connecting || !socket._handle;
476476
socket.once('connect', function() {
477-
self._connecting = false;
477+
self.connecting = false;
478478
self.emit('connect');
479479
});
480480
}
@@ -486,7 +486,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
486486
});
487487
} else {
488488
assert(!socket);
489-
this._connecting = true;
489+
this.connecting = true;
490490
}
491491
};
492492

@@ -583,7 +583,7 @@ TLSSocket.prototype._finishInit = function() {
583583
};
584584

585585
TLSSocket.prototype._start = function() {
586-
if (this._connecting) {
586+
if (this.connecting) {
587587
this.once('connect', function() {
588588
this._start();
589589
});

lib/net.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const BYTES_READ = Symbol('bytesRead');
118118
function Socket(options) {
119119
if (!(this instanceof Socket)) return new Socket(options);
120120

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

368368

369+
Object.defineProperty(Socket.prototype, '_connecting', {
370+
get: function() {
371+
return this.connecting;
372+
}
373+
});
374+
375+
369376
Object.defineProperty(Socket.prototype, 'readyState', {
370377
get: function() {
371-
if (this._connecting) {
378+
if (this.connecting) {
372379
return 'opening';
373380
} else if (this.readable && this.writable) {
374381
return 'open';
@@ -396,7 +403,7 @@ Object.defineProperty(Socket.prototype, 'bufferSize', {
396403
Socket.prototype._read = function(n) {
397404
debug('_read');
398405

399-
if (this._connecting || !this._handle) {
406+
if (this.connecting || !this._handle) {
400407
debug('_read wait for connection');
401408
this.once('connect', () => this._read(n));
402409
} else if (!this._handle.reading) {
@@ -429,7 +436,7 @@ function maybeDestroy(socket) {
429436
if (!socket.readable &&
430437
!socket.writable &&
431438
!socket.destroyed &&
432-
!socket._connecting &&
439+
!socket.connecting &&
433440
!socket._writableState.length) {
434441
socket.destroy();
435442
}
@@ -466,7 +473,7 @@ Socket.prototype._destroy = function(exception, cb) {
466473
return;
467474
}
468475

469-
self._connecting = false;
476+
this.connecting = false;
470477

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

@@ -647,7 +654,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
647654
// If we are still connecting, then buffer this for later.
648655
// The Writable logic will buffer up any more writes while
649656
// waiting for this one to be done.
650-
if (this._connecting) {
657+
if (this.connecting) {
651658
this._pendingData = data;
652659
this._pendingEncoding = encoding;
653660
this.once('connect', function() {
@@ -802,7 +809,7 @@ function connect(self, address, port, addressType, localAddress, localPort) {
802809
// TODO return promise from Socket.prototype.connect which
803810
// wraps _connectReq.
804811

805-
assert.ok(self._connecting);
812+
assert.ok(self.connecting);
806813

807814
var err;
808815

@@ -912,7 +919,7 @@ Socket.prototype.connect = function(options, cb) {
912919

913920
this._unrefTimer();
914921

915-
this._connecting = true;
922+
this.connecting = true;
916923
this.writable = true;
917924

918925
if (pipe) {
@@ -949,7 +956,7 @@ function lookupAndConnect(self, options) {
949956
var addressType = exports.isIP(host);
950957
if (addressType) {
951958
process.nextTick(function() {
952-
if (self._connecting)
959+
if (self.connecting)
953960
connect(self, host, port, addressType, localAddress, localPort);
954961
});
955962
return;
@@ -984,7 +991,7 @@ function lookupAndConnect(self, options) {
984991
// It's possible we were destroyed while looking this up.
985992
// XXX it would be great if we could cancel the promise returned by
986993
// the look up.
987-
if (!self._connecting) return;
994+
if (!self.connecting) return;
988995

989996
if (err) {
990997
// net.createConnection() creates a net.Socket object and
@@ -1052,8 +1059,8 @@ function afterConnect(status, handle, req, readable, writable) {
10521059

10531060
debug('afterConnect');
10541061

1055-
assert.ok(self._connecting);
1056-
self._connecting = false;
1062+
assert.ok(self.connecting);
1063+
self.connecting = false;
10571064
self._sockname = null;
10581065

10591066
if (status == 0) {
@@ -1069,7 +1076,7 @@ function afterConnect(status, handle, req, readable, writable) {
10691076
self.read(0);
10701077

10711078
} else {
1072-
self._connecting = false;
1079+
self.connecting = false;
10731080
var details;
10741081
if (req.localAddress && req.localPort) {
10751082
details = req.localAddress + ':' + req.localPort;

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 21 additions & 0 deletions
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)