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 b202df3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
18 changes: 5 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 @@ -1108,6 +1106,10 @@ async function connect (client) {

client[kSocket] = socket

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

if (channels.connected.hasSubscribers) {
channels.connected.publish({
connectParams: {
Expand Down Expand Up @@ -1194,16 +1196,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 +1368,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
22 changes: 21 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,12 @@ class Request {
if (channels.trailers.hasSubscribers) {
channels.trailers.publish({ request: this, trailers })
}

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

return this[kHandler].onComplete(trailers)
}

Expand All @@ -265,6 +279,12 @@ class Request {
return
}
this.aborted = true

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

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
Binary file added test/request-timeout.js.10mb.txt
Binary file not shown.

0 comments on commit b202df3

Please sign in to comment.