()
| 428 | } |
| 429 | |
| 430 | private drain(): void { |
| 431 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 432 | // already on their way up (pending) — so we never spawn the whole pool for a |
| 433 | // single call whose eager worker just hasn't reported ready yet. |
| 434 | while ( |
| 435 | this.queue.length > this.idle.length + this.pendingWorkers.size && |
| 436 | this.workers.size < this.maxSize && |
| 437 | this.pendingWorkers.size < MAX_CONCURRENT_SPAWN && |
| 438 | this.healthy |
| 439 | ) { |
| 440 | this.spawnOne(); |
| 441 | } |
| 442 | while (this.idle.length && this.queue.length) { |
| 443 | // Skip jobs the backstop already answered. |
| 444 | let job: Job | undefined; |
| 445 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 446 | if (!job || job.settled) break; |
| 447 | const w = this.idle.pop()!; |
| 448 | this.inflight.set(w, job); |
| 449 | w.postMessage({ type: 'call', id: job.id, toolName: job.toolName, args: job.args }); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | private settle(job: Job, result: ToolResult): void { |
| 454 | if (job.settled) return; // already answered (by backstop or worker) |
no test coverage detected