()
| 520 | } |
| 521 | |
| 522 | private drain(): void { |
| 523 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 524 | // already on their way up — throttled so we never cold-start the whole pool |
| 525 | // at once. |
| 526 | while ( |
| 527 | this.queue.length > this.idle.length + this.pending.size && |
| 528 | this.workers.size < this.wantedSize() && |
| 529 | this.pending.size < MAX_CONCURRENT_SPAWN && |
| 530 | !this.destroyed && |
| 531 | this.healthy |
| 532 | ) { |
| 533 | this.spawnOne(); |
| 534 | } |
| 535 | // Dispatch queued jobs to idle workers. |
| 536 | while (this.idle.length && this.queue.length) { |
| 537 | let job: ParseJob | undefined; |
| 538 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 539 | if (!job || job.settled) break; |
| 540 | const w = this.idle.pop()!; |
| 541 | this.dispatch(w, job); |
| 542 | } |
| 543 | // Hang-prevention: if there's queued work but nothing can ever run it (no |
| 544 | // idle workers, none spawning, none alive), fail it instead of hanging |
| 545 | // forever. Reached only when the crash budget is exhausted or after destroy. |
| 546 | if (this.queue.length && this.idle.length === 0 && this.pending.size === 0 && this.workers.size === 0) { |
| 547 | const reason = this.destroyed ? 'parse pool destroyed' : 'parse pool exhausted its worker crash budget'; |
| 548 | for (const job of this.queue.splice(0)) this.settle(job, undefined, new Error(reason)); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | private settle(job: ParseJob, result?: ExtractionResult, err?: Error): void { |
| 553 | if (job.settled) return; |
no test coverage detected