Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Member Author

@ronag ronag Jul 24, 2024

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