Skip to content

Commit 267439f

Browse files
ShogunPandaruyadorno
authored andcommitted
net: rework autoSelectFamily implementation
PR-URL: #46587 Backport-PR-URL: #49183 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0beb5ab commit 267439f

16 files changed

+247
-128
lines changed

doc/api/net.md

+43-21
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ For TCP connections, available `options` are:
935935
* `autoSelectFamilyAttemptTimeout` {number}: The amount of time in milliseconds to wait
936936
for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option.
937937
If set to a positive integer less than `10`, then the value `10` will be used instead.
938-
**Default:** `250`.
938+
**Default:** initially `250`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`][]
939939

940940
For [IPC][] connections, available `options` are:
941941

@@ -1520,26 +1520,6 @@ immediately initiates connection with
15201520
[`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`],
15211521
then returns the `net.Socket` that starts the connection.
15221522

1523-
## `net.setDefaultAutoSelectFamily(value)`
1524-
1525-
<!-- YAML
1526-
added: REPLACEME
1527-
-->
1528-
1529-
Sets the default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1530-
1531-
* `value` {boolean} The new default value. The initial default value is `false`.
1532-
1533-
## `net.getDefaultAutoSelectFamily()`
1534-
1535-
<!-- YAML
1536-
added: REPLACEME
1537-
-->
1538-
1539-
Gets the current default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1540-
1541-
* Returns: {boolean} The current default value of the `autoSelectFamily` option.
1542-
15431523
## `net.createServer([options][, connectionListener])`
15441524

15451525
<!-- YAML
@@ -1640,6 +1620,47 @@ Use `nc` to connect to a Unix domain socket server:
16401620
$ nc -U /tmp/echo.sock
16411621
```
16421622

1623+
## `net.getDefaultAutoSelectFamily()`
1624+
1625+
<!-- YAML
1626+
added: v19.4.0
1627+
-->
1628+
1629+
Gets the current default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1630+
1631+
* Returns: {boolean} The current default value of the `autoSelectFamily` option.
1632+
1633+
## `net.setDefaultAutoSelectFamily(value)`
1634+
1635+
<!-- YAML
1636+
added: v19.4.0
1637+
-->
1638+
1639+
Sets the default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1640+
1641+
* `value` {boolean} The new default value. The initial default value is `false`.
1642+
1643+
## `net.getDefaultAutoSelectFamilyAttemptTimeout()`
1644+
1645+
<!-- YAML
1646+
added: REPLACEME
1647+
-->
1648+
1649+
Gets the current default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][].
1650+
1651+
* Returns: {number} The current default value of the `autoSelectFamilyAttemptTimeout` option.
1652+
1653+
## `net.setDefaultAutoSelectFamilyAttemptTimeout(value)`
1654+
1655+
<!-- YAML
1656+
added: REPLACEME
1657+
-->
1658+
1659+
Sets the default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][].
1660+
1661+
* `value` {number} The new default value, which must be a positive number. If the number is less than `10`,
1662+
the value `10` is used insted The initial default value is `250`.
1663+
16431664
## `net.isIP(input)`
16441665

16451666
<!-- YAML
@@ -1725,6 +1746,7 @@ net.isIPv6('fhqwhgads'); // returns false
17251746
[`net.createConnection(port, host)`]: #netcreateconnectionport-host-connectlistener
17261747
[`net.createServer()`]: #netcreateserveroptions-connectionlistener
17271748
[`net.setDefaultAutoSelectFamily(value)`]: #netsetdefaultautoselectfamilyvalue
1749+
[`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`]: #netsetdefaultautoselectfamilyattempttimeoutvalue
17281750
[`new net.Socket(options)`]: #new-netsocketoptions
17291751
[`readable.setEncoding()`]: stream.md#readablesetencodingencoding
17301752
[`server.close()`]: #serverclosecallback

lib/_tls_wrap.js

+44-26
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const EE = require('events');
5454
const net = require('net');
5555
const tls = require('tls');
5656
const common = require('_tls_common');
57-
const { kWrapConnectedHandle } = require('internal/net');
57+
const { kReinitializeHandle } = require('internal/net');
5858
const JSStreamSocket = require('internal/js_stream_socket');
5959
const { Buffer } = require('buffer');
6060
let debug = require('internal/util/debuglog').debuglog('tls', (fn) => {
@@ -633,14 +633,27 @@ TLSSocket.prototype._wrapHandle = function(wrap, handle) {
633633
return res;
634634
};
635635

636-
TLSSocket.prototype[kWrapConnectedHandle] = function(handle) {
637-
this._handle = this._wrapHandle(null, handle);
636+
TLSSocket.prototype[kReinitializeHandle] = function reinitializeHandle(handle) {
637+
const originalServername = this._handle.getServername();
638+
const originalSession = this._handle.getSession();
639+
640+
this.handle = this._wrapHandle(null, handle);
638641
this.ssl = this._handle;
642+
643+
net.Socket.prototype[kReinitializeHandle].call(this, this.handle);
639644
this._init();
640645

641646
if (this._tlsOptions.enableTrace) {
642647
this._handle.enableTrace();
643648
}
649+
650+
if (originalSession) {
651+
this.setSession(originalSession);
652+
}
653+
654+
if (originalServername) {
655+
this.setServername(originalServername);
656+
}
644657
};
645658

646659
// This eliminates a cyclic reference to TLSWrap
@@ -679,6 +692,30 @@ TLSSocket.prototype._destroySSL = function _destroySSL() {
679692
this[kIsVerified] = false;
680693
};
681694

695+
function keylogNewListener(event) {
696+
if (event !== 'keylog')
697+
return;
698+
699+
// Guard against enableKeylogCallback after destroy
700+
if (!this._handle) return;
701+
this._handle.enableKeylogCallback();
702+
703+
// Remove this listener since it's no longer needed.
704+
this.removeListener('newListener', keylogNewListener);
705+
}
706+
707+
function newListener(event) {
708+
if (event !== 'session')
709+
return;
710+
711+
// Guard against enableSessionCallbacks after destroy
712+
if (!this._handle) return;
713+
this._handle.enableSessionCallbacks();
714+
715+
// Remove this listener since it's no longer needed.
716+
this.removeListener('newListener', newListener);
717+
}
718+
682719
// Constructor guts, arbitrarily factored out.
683720
let warnOnTlsKeylog = true;
684721
let warnOnTlsKeylogError = true;
@@ -704,18 +741,9 @@ TLSSocket.prototype._init = function(socket, wrap) {
704741

705742
// Only call .onkeylog if there is a keylog listener.
706743
ssl.onkeylog = onkeylog;
707-
this.on('newListener', keylogNewListener);
708744

709-
function keylogNewListener(event) {
710-
if (event !== 'keylog')
711-
return;
712-
713-
// Guard against enableKeylogCallback after destroy
714-
if (!this._handle) return;
715-
this._handle.enableKeylogCallback();
716-
717-
// Remove this listener since it's no longer needed.
718-
this.removeListener('newListener', keylogNewListener);
745+
if (this.listenerCount('newListener', keylogNewListener) === 0) {
746+
this.on('newListener', keylogNewListener);
719747
}
720748

721749
if (options.isServer) {
@@ -750,18 +778,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
750778
ssl.onnewsession = onnewsessionclient;
751779

752780
// Only call .onnewsession if there is a session listener.
753-
this.on('newListener', newListener);
754-
755-
function newListener(event) {
756-
if (event !== 'session')
757-
return;
758-
759-
// Guard against enableSessionCallbacks after destroy
760-
if (!this._handle) return;
761-
this._handle.enableSessionCallbacks();
762-
763-
// Remove this listener since it's no longer needed.
764-
this.removeListener('newListener', newListener);
781+
if (this.listenerCount('newListener', newListener) === 0) {
782+
this.on('newListener', newListener);
765783
}
766784
}
767785

lib/internal/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function makeSyncWrite(fd) {
6767
}
6868

6969
module.exports = {
70-
kWrapConnectedHandle: Symbol('wrapConnectedHandle'),
70+
kReinitializeHandle: Symbol('reinitializeHandle'),
7171
isIP,
7272
isIPv4,
7373
isIPv6,

0 commit comments

Comments
 (0)