Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0d098b

Browse filesBrowse files
ShogunPandatargos
authored andcommitted
net: rework autoSelectFamily implementation
PR-URL: #46587 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fc319d6 commit e0d098b
Copy full SHA for e0d098b
Expand file treeCollapse file tree

16 files changed

+250
-128
lines changed
Open diff view settings
Collapse file

‎doc/api/net.md‎

Copy file name to clipboardExpand all lines: doc/api/net.md
+43-21Lines changed: 43 additions & 21 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ For TCP connections, available `options` are:
939939
* `autoSelectFamilyAttemptTimeout` {number}: The amount of time in milliseconds to wait
940940
for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option.
941941
If set to a positive integer less than `10`, then the value `10` will be used instead.
942-
**Default:** `250`.
942+
**Default:** initially `250`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`][]
943943

944944
For [IPC][] connections, available `options` are:
945945

@@ -1526,26 +1526,6 @@ immediately initiates connection with
15261526
[`socket.connect(port[, host][, connectListener])`][`socket.connect(port)`],
15271527
then returns the `net.Socket` that starts the connection.
15281528

1529-
## `net.setDefaultAutoSelectFamily(value)`
1530-
1531-
<!-- YAML
1532-
added: v19.4.0
1533-
-->
1534-
1535-
Sets the default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1536-
1537-
* `value` {boolean} The new default value. The initial default value is `false`.
1538-
1539-
## `net.getDefaultAutoSelectFamily()`
1540-
1541-
<!-- YAML
1542-
added: v19.4.0
1543-
-->
1544-
1545-
Gets the current default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
1546-
1547-
* Returns: {boolean} The current default value of the `autoSelectFamily` option.
1548-
15491529
## `net.createServer([options][, connectionListener])`
15501530

15511531
<!-- 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
Collapse file

‎lib/_tls_wrap.js‎

Copy file name to clipboardExpand all lines: lib/_tls_wrap.js
+44-26Lines changed: 44 additions & 26 deletions
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

Collapse file

‎lib/internal/net.js‎

Copy file name to clipboardExpand all lines: lib/internal/net.js
+1-1Lines changed: 1 addition & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.