Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 4, 2023
1 parent 99b3f7c commit 7b93202
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,31 @@ try {
}

let fastNow = Date.now()
const fastTimers = new Set()
const fastTimers = []
const fastNowInterval = setInterval(() => {
fastNow = Date.now()

// TODO (perf): This can probably be optimized.
for (const timer of fastTimers) {
let len = fastTimers.length
let idx = 0
while (idx < len) {
const timer = fastTimers[idx]

if (timer.timeoutExpires && fastNow >= timer.timeoutExpires) {
timer.timeoutExpires = 0
onParserTimeout(timer)
}

if (!timer.timeoutExpires) {
timer.timeoutExpires = null
if (len > 1) {
fastTimers[idx] = fastTimers.pop()
} else {
fastTimers.pop()
}
len = fastTimers.length
} else {
idx += 1
}
}
}, 1e3)
if (fastNowInterval.unref) {
Expand Down Expand Up @@ -437,7 +452,7 @@ class Parser {
this.ptr = this.llhttp.llhttp_alloc(constants.TYPE.RESPONSE)
this.client = client
this.socket = socket
this.timeoutExpires = 0
this.timeoutExpires = null
this.timeoutValue = 0
this.timeoutType = null
this.statusCode = null
Expand All @@ -463,17 +478,15 @@ class Parser {
if (value === this.timeoutValue) {
this.refreshTimeout()
} else if (value) {
if (!this.timeoutValue) {
fastTimers.add(this)
if (this.timeoutExpires === null) {
fastTimers.push(this)
}
this.timeoutExpires = fastNow + value
this.timeoutValue = value
} else {
fastTimers.delete(this)
this.timeoutExpires = 0
this.timeoutValue = 0
}

}

refreshTimeout () {
Expand All @@ -483,10 +496,6 @@ class Parser {
}

clearTimeout () {
if (this.timeoutValue) {
fastTimers.delete(this)
}

this.timeoutExpires = 0
this.timeoutValue = 0
}
Expand Down

0 comments on commit 7b93202

Please sign in to comment.