()
| 495 | } |
| 496 | |
| 497 | private drain(): void { |
| 498 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 499 | // already on their way up — throttled so we never cold-start the whole pool |
| 500 | // at once. |
| 501 | while ( |
| 502 | this.queue.length > this.idle.length + this.pending.size && |
| 503 | this.workers.size < this.wantedSize() && |
| 504 | this.pending.size < MAX_CONCURRENT_SPAWN && |
| 505 | !this.destroyed && |
| 506 | this.healthy |
| 507 | ) { |
| 508 | this.spawnOne(); |
| 509 | } |
| 510 | // Dispatch queued jobs to idle workers. |
| 511 | while (this.idle.length && this.queue.length) { |
| 512 | let job: ParseJob | undefined; |
| 513 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 514 | if (!job || job.settled) break; |
| 515 | const w = this.idle.pop()!; |
| 516 | this.dispatch(w, job); |
| 517 | } |
| 518 | // Hang-prevention: if there's queued work but nothing can ever run it (no |
| 519 | // idle workers, none spawning, none alive), fail it instead of hanging |
| 520 | // forever. Reached only when the crash budget is exhausted or after destroy. |
| 521 | if (this.queue.length && this.idle.length === 0 && this.pending.size === 0 && this.workers.size === 0) { |
| 522 | const reason = this.destroyed ? 'parse pool destroyed' : 'parse pool exhausted its worker crash budget'; |
| 523 | for (const job of this.queue.splice(0)) this.settle(job, undefined, new Error(reason)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | private settle(job: ParseJob, result?: ExtractionResult, err?: Error): void { |
| 528 | if (job.settled) return; |
no test coverage detected