Skip to content

Commit

Permalink
fix: only ref socket when in use
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Apr 11, 2023
1 parent b20405e commit 6eda454
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
17 changes: 4 additions & 13 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const {
kConnected,
kConnecting,
kNeedDrain,
kNoRef,
kKeepAliveDefaultTimeout,
kHostHeader,
kPendingIdx,
Expand Down Expand Up @@ -1091,7 +1090,6 @@ async function connect (client) {

assert(socket)

socket[kNoRef] = false
socket[kWriting] = false
socket[kReset] = false
socket[kBlocking] = false
Expand All @@ -1107,6 +1105,9 @@ async function connect (client) {
.on('close', onSocketClose)

client[kSocket] = socket
if (typeof client[kSocket].unref === 'function') {
client[kSocket].unref()
}

if (channels.connected.hasSubscribers) {
channels.connected.publish({
Expand Down Expand Up @@ -1194,16 +1195,6 @@ function _resume (client, sync) {
const socket = client[kSocket]

if (socket && !socket.destroyed) {
if (client[kSize] === 0) {
if (!socket[kNoRef] && socket.unref) {
socket.unref()
socket[kNoRef] = true
}
} else if (socket[kNoRef] && socket.ref) {
socket.ref()
socket[kNoRef] = false
}

if (client[kSize] === 0) {
if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_IDLE)
Expand Down Expand Up @@ -1376,7 +1367,7 @@ function write (client, request) {
errorRequest(client, request, err || new RequestAbortedError())

util.destroy(socket, new InformationalError('aborted'))
})
}, socket)
} catch (err) {
errorRequest(client, request, err)
}
Expand Down
20 changes: 19 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/
const invalidPathRegex = /[^\u0021-\u00ff]/

const kHandler = Symbol('handler')
const kSocket = Symbol('socket')

const channels = {}

Expand Down Expand Up @@ -191,6 +192,7 @@ class Request {

this.servername = util.getServerName(this.host)

this[kSocket] = null
this[kHandler] = handler

if (channels.create.hasSubscribers) {
Expand All @@ -214,10 +216,16 @@ class Request {
}
}

onConnect (abort) {
onConnect (abort, socket) {
assert(!this.aborted)
assert(!this.completed)

this[kSocket] = socket

if (typeof this[kSocket].ref === 'function') {
this[kSocket].ref()
}

return this[kHandler].onConnect(abort)
}

Expand Down Expand Up @@ -253,6 +261,11 @@ class Request {
if (channels.trailers.hasSubscribers) {
channels.trailers.publish({ request: this, trailers })
}

if (typeof this[kSocket].unref === 'function') {
this[kSocket].unref()
}

return this[kHandler].onComplete(trailers)
}

Expand All @@ -261,10 +274,15 @@ class Request {
channels.error.publish({ request: this, error })
}

if (typeof this[kSocket].unref === 'function') {
this[kSocket].unref()
}

if (this.aborted) {
return
}
this.aborted = true

return this[kHandler].onError(error)
}

Expand Down
1 change: 0 additions & 1 deletion lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = {
kServerName: Symbol('server name'),
kLocalAddress: Symbol('local address'),
kHost: Symbol('host'),
kNoRef: Symbol('no ref'),
kBodyUsed: Symbol('used'),
kRunning: Symbol('running'),
kBlocking: Symbol('blocking'),
Expand Down

0 comments on commit 6eda454

Please sign in to comment.