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 30fe86e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
kKeepAliveDefaultTimeout: Symbol('default keep alive timeout'),
kKeepAliveMaxTimeout: Symbol('max keep alive timeout'),
kKeepAliveTimeoutThreshold: Symbol('keep alive timeout threshold'),
kKeepAliveDeadline: Symbol('keep alive deadline'),
kKeepAliveTimeoutValue: Symbol('keep alive timeout'),
kKeepAlive: Symbol('keep alive'),
kHeadersTimeout: Symbol('headers timeout'),
Expand Down
9 changes: 8 additions & 1 deletion lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const {
kMaxResponseSize,
kOnError,
kResume,
kHTTPContext
kHTTPContext,
kKeepAliveDeadline
} = require('../core/symbols.js')

const constants = require('../llhttp/constants.js')
Expand Down Expand Up @@ -644,6 +645,7 @@ async function connectH1 (client, socket) {
socket[kReset] = false
socket[kBlocking] = false
socket[kParser] = new Parser(client, socket, llhttpInstance)
socket[kKeepAliveDeadline] = 0

addListener(socket, 'error', function (err) {
const parser = this[kParser]
Expand Down Expand Up @@ -805,6 +807,7 @@ function resumeH1 (client) {
if (client[kSize] === 0) {
if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_IDLE)
socket[kKeepAliveDeadline] = Date.now() + client[kKeepAliveTimeoutValue]
}
} else if (client[kRunning] > 0 && socket[kParser].statusCode < 200) {
if (socket[kParser].timeoutType !== TIMEOUT_HEADERS) {
Expand All @@ -815,6 +818,10 @@ function resumeH1 (client) {
socket[kParser].setTimeout(headersTimeout, TIMEOUT_HEADERS)
}
}

if (socket[kKeepAliveDeadline] && socket[kKeepAliveDeadline] > Date.now()) {
util.destroy(socket, new InformationalError('socket idle timeout'))
}
}
}

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 30fe86e

Please sign in to comment.