Skip to content

Commit

Permalink
feat: dispatcher _buildConnector() method
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Sep 13, 2024
1 parent c773728 commit 860b14b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
23 changes: 16 additions & 7 deletions lib/core/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,9 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
})
}

// Set TCP keep alive options on the socket here instead of in connect() for the case of assigning the socket
if (options.keepAlive == null || options.keepAlive) {
const keepAliveInitialDelay = options.keepAliveInitialDelay === undefined ? 60e3 : options.keepAliveInitialDelay
socket.setKeepAlive(true, keepAliveInitialDelay)
}

const clearConnectTimeout = setupConnectTimeout(new WeakRef(socket), { timeout, hostname, port })

socket
configureSocket(socket, options)
.setNoDelay(true)
.once(protocol === 'https:' ? 'secureConnect' : 'connect', function () {
queueMicrotask(clearConnectTimeout)
Expand Down Expand Up @@ -232,4 +226,19 @@ function onConnectTimeout (socket, opts) {
util.destroy(socket, new ConnectTimeoutError(message))
}

function configureSocket (socket, options) {
// Set TCP keep alive options on the socket here instead of in connect() for the case of assigning the socket
if (options.keepAlive == null || options.keepAlive) {
const keepAliveInitialDelay = options.keepAliveInitialDelay === undefined ? 60e3 : options.keepAliveInitialDelay

socket.setKeepAlive(true, keepAliveInitialDelay)
}

socket.setNoDelay(true)

return socket
}

module.exports = buildConnector

module.exports.configureSocket = configureSocket
3 changes: 1 addition & 2 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
InformationalError,
ClientDestroyedError
} = require('../core/errors.js')
const buildConnector = require('../core/connect.js')
const {
kUrl,
kServerName,
Expand Down Expand Up @@ -190,7 +189,7 @@ class Client extends DispatcherBase {
super()

if (typeof connect !== 'function') {
connect = buildConnector({
connect = this._buildConnector({
...tls,
maxCachedSessions,
allowH2,
Expand Down
6 changes: 6 additions & 0 deletions lib/dispatcher/dispatcher-base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const buildConnector = require('../core/connect.js')

const Dispatcher = require('./dispatcher')
const {
ClientDestroyedError,
Expand Down Expand Up @@ -153,6 +155,10 @@ class DispatcherBase extends Dispatcher {
return false
}
}

_buildConnector (options) {
return buildConnector(options)
}
}

module.exports = DispatcherBase
3 changes: 1 addition & 2 deletions lib/dispatcher/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
} = require('../core/errors')
const util = require('../core/util')
const { kUrl } = require('../core/symbols')
const buildConnector = require('../core/connect')

const kOptions = Symbol('options')
const kConnections = Symbol('connections')
Expand Down Expand Up @@ -52,7 +51,7 @@ class Pool extends PoolBase {
super()

if (typeof connect !== 'function') {
connect = buildConnector({
connect = this._buildConnector({
...tls,
maxCachedSessions,
allowH2,
Expand Down
5 changes: 2 additions & 3 deletions lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const Agent = require('./agent')
const Pool = require('./pool')
const DispatcherBase = require('./dispatcher-base')
const { InvalidArgumentError, RequestAbortedError, SecureProxyConnectionError } = require('../core/errors')
const buildConnector = require('../core/connect')

const kAgent = Symbol('proxy agent')
const kClient = Symbol('proxy client')
Expand Down Expand Up @@ -57,8 +56,8 @@ class ProxyAgent extends DispatcherBase {
this[kProxyHeaders]['proxy-authorization'] = `Basic ${Buffer.from(`${decodeURIComponent(username)}:${decodeURIComponent(password)}`).toString('base64')}`
}

const connect = buildConnector({ ...opts.proxyTls })
this[kConnectEndpoint] = buildConnector({ ...opts.requestTls })
const connect = this._buildConnector({ ...opts.proxyTls })
this[kConnectEndpoint] = this._buildConnector({ ...opts.requestTls })
this[kClient] = clientFactory(url, { connect })
this[kAgent] = new Agent({
...opts,
Expand Down

0 comments on commit 860b14b

Please sign in to comment.