Skip to content

Commit

Permalink
Fix tulios#1556: avoid infinite setTimeout loop with no inflight requ…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
sergeyevstifeev committed Feb 27, 2024
1 parent ff3b111 commit c9fc6e8
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/network/requestQueue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const PRIVATE = {
}

const REQUEST_QUEUE_EMPTY = 'requestQueueEmpty'
const CHECK_PENDING_REQUESTS_INTERVAL = 10

module.exports = class RequestQueue extends EventEmitter {
/**
Expand Down Expand Up @@ -304,20 +303,26 @@ module.exports = class RequestQueue extends EventEmitter {
* the pending request queue eventually.
*/
scheduleCheckPendingRequests() {
// If we're throttled: Schedule checkPendingRequests when the throttle
// should be resolved. If there is already something scheduled we assume that that
// If there is already something scheduled we assume that that
// will be fine, and potentially fix up a new timeout if needed at that time.
if (this.throttleCheckTimeoutId) {
return
}

if (this.pending.length === 0) {
return
}

// If we're throttled: Schedule checkPendingRequests when the throttle
// should be resolved.
const timeUntilUnthrottled = this.throttledUntil - Date.now()
// Note that if we're merely "overloaded" by having too many inflight requests
// we will anyways check the queue when one of them gets fulfilled.
let scheduleAt = this.throttledUntil - Date.now()
if (!this.throttleCheckTimeoutId) {
if (this.pending.length > 0) {
scheduleAt = scheduleAt > 0 ? scheduleAt : CHECK_PENDING_REQUESTS_INTERVAL
}
this.throttleCheckTimeoutId = setTimeout(() => {
this.throttleCheckTimeoutId = null
this.checkPendingRequests()
}, scheduleAt)
}
const scheduleAt = Math.max(timeUntilUnthrottled, 0)

this.throttleCheckTimeoutId = setTimeout(() => {
this.throttleCheckTimeoutId = null
this.checkPendingRequests()
}, scheduleAt)
}
}

0 comments on commit c9fc6e8

Please sign in to comment.