Skip to content

Commit

Permalink
fix: Improve timeout handling during event loop lag
Browse files Browse the repository at this point in the history
Make timeouts behave more closer that what is expected
during event loop lag.
  • Loading branch information
ronag committed Jul 24, 2024
1 parent c1f7d2a commit db66601
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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)
}
}

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

0 comments on commit db66601

Please sign in to comment.