* Tries to get an existing worker with capacity, or spawns a new one if the * pool is not full. Returns null when the pool is at capacity and all workers * are saturated (caller should enqueue).
()
| 1058 | * are saturated (caller should enqueue). |
| 1059 | */ |
| 1060 | async function acquireWorker(): Promise<WorkerInfo | null> { |
| 1061 | const existing = selectWorker() |
| 1062 | if (existing) return existing |
| 1063 | |
| 1064 | const activeWorkerCount = [...workers.values()].filter((w) => !w.retiring).length |
| 1065 | const currentPoolSize = activeWorkerCount + spawnInProgress |
| 1066 | if (currentPoolSize < POOL_SIZE) { |
| 1067 | try { |
| 1068 | return await spawnWorker() |
| 1069 | } catch (error) { |
| 1070 | logger.error('Failed to spawn worker', { error }) |
| 1071 | return null |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return null |
| 1076 | } |
| 1077 | |
| 1078 | function dispatchToWorker( |
| 1079 | workerInfo: WorkerInfo, |
no test coverage detected