Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions lib/core/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const net = require('node:net')
const assert = require('node:assert')
const util = require('./util')
const { InvalidArgumentError, ConnectTimeoutError } = require('./errors')
const timers = require('../util/timers')

let tls // include tls conditionally since it is not always available

Expand Down Expand Up @@ -130,12 +131,12 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
socket.setKeepAlive(true, keepAliveInitialDelay)
}

const cancelTimeout = setupTimeout(() => onConnectTimeout(socket), timeout)
const connectTimeout = timers.setTimeout(onConnectTimeout, timeout, socket)

socket
.setNoDelay(true)
.once(protocol === 'https:' ? 'secureConnect' : 'connect', function () {
cancelTimeout()
timers.clearTimeout(connectTimeout)

if (callback) {
const cb = callback
Expand All @@ -144,7 +145,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
}
})
.on('error', function (err) {
cancelTimeout()
timers.clearTimeout(connectTimeout)

if (callback) {
const cb = callback
Expand All @@ -157,31 +158,6 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
}
}

function setupTimeout (onConnectTimeout, timeout) {
if (!timeout) {
return () => {}
}

let s1 = null
let s2 = null
const timeoutId = setTimeout(() => {
// setImmediate is added to make sure that we prioritize socket error events over timeouts
s1 = setImmediate(() => {
if (process.platform === 'win32') {
// Windows needs an extra setImmediate probably due to implementation differences in the socket logic
s2 = setImmediate(() => onConnectTimeout())
} else {
onConnectTimeout()
}
})
}, timeout)
return () => {
clearTimeout(timeoutId)
clearImmediate(s1)
clearImmediate(s2)
}
}

function onConnectTimeout (socket) {
let message = 'Connect Timeout Error'
if (Array.isArray(socket.autoSelectFamilyAttemptedAddresses)) {
Expand Down
14 changes: 14 additions & 0 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Parser {

this.bytesRead = 0

this.timeoutDeadline = 0
this.keepAlive = ''
this.contentLength = ''
this.connection = ''
Expand All @@ -170,6 +171,8 @@ class Parser {
timers.clearTimeout(this.timeout)
if (value) {
this.timeout = timers.setTimeout(onParserTimeout, value, this)
this.timeoutDeadline = this.timeoutValue ? Date.now() + this.timeoutValue : 0

// istanbul ignore else: only for jest
if (this.timeout.unref) {
this.timeout.unref()
Expand All @@ -182,10 +185,17 @@ class Parser {
// istanbul ignore else: only for jest
if (this.timeout.refresh) {
this.timeout.refresh()
this.timeoutDeadline = this.timeoutValue ? Date.now() + this.timeoutValue : 0
}
}
}

updateTimeout () {
if (this.timeoutDeadline && this.timeoutDeadline < Date.now()) {
onParserTimeout(this)
}
Comment on lines +194 to +196

@ronag ronag Jul 24, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will call Date.now() quite often... which is not optimal performance-wise. However, I don't see a smart way around it without using a worker as the point is to detect event loop lag before dispatching further events.

}

resume () {
if (this.socket.destroyed || !this.paused) {
return
Expand Down Expand Up @@ -615,6 +625,8 @@ class Parser {
function onParserTimeout (parser) {
const { socket, timeoutType, client } = parser

parser.timeoutDeadline = 0

/* istanbul ignore else */
if (timeoutType === TIMEOUT_HEADERS) {
if (!socket[kWriting] || socket.writableNeedDrain || client[kRunning] > 1) {
Expand Down Expand Up @@ -815,6 +827,8 @@ function resumeH1 (client) {
socket[kParser].setTimeout(headersTimeout, TIMEOUT_HEADERS)
}
}

socket[kParser].updateTimeout()
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

const TICK_MS = 499

let fastNow = Date.now()
let fastNow = 0
let fastNowTimeout

const fastTimers = []

function onTimeout () {
fastNow = Date.now()
fastNow += TICK_MS

let len = fastTimers.length
let idx = 0
Expand Down